From feff5e39898e07183549e7945b67f37ad3398957 Mon Sep 17 00:00:00 2001 From: "Johannes J. Schmidt" Date: Wed, 18 Dec 2013 15:55:30 +0100 Subject: [PATCH] run in a main loop --- README.md | 11 +++++++---- index.js | 46 ++++++++++++++++++++++++++++------------------ package.json | 4 ++-- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index ce708db..eeef279 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,13 @@ Now CouchDB takes care of the couchmagick process. ```ini [couchmagick] -user = mein-user ; optional -password = secure ; optional -timeout = 10000 ; in ms. Default is 1000 -limit = 1000 ; batch size. Default is 100 +; Optional username and password +username = mein-user +password = secure +; Timeout in ms. Default is 10000 +timeout = 1000 +; Batch size. Default is 100 +limit = 1000 ``` Imagemagick Configuration diff --git a/index.js b/index.js index 883d41e..ceea13c 100755 --- a/index.js +++ b/index.js @@ -19,36 +19,43 @@ var couchmagick = daemon(process.stdin, process.stdout, function() { var noop = function() {}; couchmagick.get({ + // Connection address: 'httpd.bind_address', port: 'httpd.port', - limit: pkg.name + '.limit', - timeout: pkg.name + '.timeout', auth: { username: pkg.name + '.username', password: pkg.name + '.password' - } + }, + + // Batching + limit: pkg.name + '.limit', + timeout: pkg.name + '.timeout' }, function(err, config) { if (err) { return process.exit(0); } - var auth = config.auth && config.auth.username && config.auth.password ? - [config.auth.username, config.auth.password].join(':') : - null; + // defaults + config.timeout = config.timeout || 10000; + config.limit = config.limit || 100; + + couchmagick.info('using config ' + JSON.stringify(config).replace(/"password":".*?"/, '"password":"***"')); + var couch = url.format({ protocol: 'http', hostname: config.address, port: config.port, - auth: auth + auth: config.auth && config.auth.username && config.auth.password ? [ config.auth.username, config.auth.password ].join(':') : null }); var options = { - limit: config.limit || 100, + limit: config.limit, feed: 'continuous', - timeout: config.timeout || 1000 + timeout: config.timeout }; + function listen(db, next) { couchmagick.info('Listening on ' + db); @@ -61,20 +68,23 @@ couchmagick.get({ stream.on('end', next); } - nano(couch).db.list(function(err, dbs) { + // main loop ;) + function run(err) { if (err) { - console.log(err); - couchmagick.error('Can not get _all_dbs: ' + err.description); - - return process.exit(0); + process.exit(0); } - async.eachSeries(dbs, listen, function() { - couchmagick.info('done.'); - process.exit(0); + nano(couch).db.list(function(err, dbs) { + if (err) { + couchmagick.error('Can not get _all_dbs: ' + err.description); + return process.exit(0); + } + + async.eachSeries(dbs, listen, run); }); - }); + } + run(); // TODO: listen to db changes }); diff --git a/package.json b/package.json index 4027cc9..5eebaf1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "couchmagick", - "version": "1.1.3", + "version": "1.1.4", "description": "Run ImageMagicks `convert` on CouchDB documents.", "main": "index.js", "preferGlobal": true, @@ -30,7 +30,7 @@ "async": "~0.2.9", "event-stream": "~3.0.20", "couchmagick-listen": "~1.0.6", - "couch-daemon": "~1.1.4", + "couch-daemon": "~1.2.0", "nano": "~4.2.1" }, "devDependencies": {