From b53060fec3803b0a6dc47c24515dfc576c448f22 Mon Sep 17 00:00:00 2001 From: "Johannes J. Schmidt" Date: Sun, 15 Dec 2013 22:10:25 +0100 Subject: [PATCH] use couchmagick-listen --- README.md | 46 ++++++++++-- bin/couchmagick | 3 - index.js | 196 +++++++++++++----------------------------------- package.json | 10 ++- 4 files changed, 99 insertions(+), 156 deletions(-) delete mode 100755 bin/couchmagick mode change 100644 => 100755 index.js diff --git a/README.md b/README.md index 66c2d15..c8efdf8 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,10 @@ via npm: npm install couchmagick -g ``` -Configuration -------------- -Add couchmagick to the `os_daemons`: +Daemon Configuration +-------------------- +Add couchmagick to `os_daemons` config section: + ```ini [os_daemons] couchmagick = couchmagick @@ -22,12 +23,43 @@ Now CouchDB takes care of the couchmagick process. ```ini [couchmagick] -user = mein-user -password = secure -dbs = photos, _users -filter = 'myapp/myfilter' +user = mein-user ; optional +password = secure ; optional +``` + +Imagemagick Configuration +------------------------- +Add a `couchmagick` property to a design document. couchmagick will process all +databases which have such a design document. +```json +{ + "_id": "_design/my-couchmagick-config", + "_rev": "1-a653b27246b01cf9204fa9f5dee7cc64", + "couchmagick": { + "filter": "function(doc) { return doc.type === 'post'; }", + "versions": { + "thumbnail": { + "filter": "function(doc, name) { return doc.display && doc.display.indexOf('overview') > -1; }", + "format": "jpg", + "id": "{id}/thumbnail", + "name": "{basename}-thumbnail.jpg", + "args": [ + "-", + "-resize", "x100", + "-quality", "75", + "-colorspace", "sRGB", + "-strip", + "jpg:-" + ] + } + } + } +} ``` +See [couchmagick-stream](https://github.com/null2/couchmagick-stream) for available options; + + Contributing ------------ Lint your code via `npm run jshint`. diff --git a/bin/couchmagick b/bin/couchmagick deleted file mode 100755 index 7714cb3..0000000 --- a/bin/couchmagick +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env node - -require('..')(); diff --git a/index.js b/index.js old mode 100644 new mode 100755 index 1f784d1..48574f8 --- a/index.js +++ b/index.js @@ -1,159 +1,69 @@ +#!/usr/bin/env node /* couchmagick * (c) 2013 Johannes J. Schmidt, null2 GmbH, Berlin */ -'use strict'; - var pkg = require('./package.json'); -var couchdbWorker = require('couchdb-worker'); -var url = require('url'); - -// logging -function info(msg) { - process.stdout.write(JSON.stringify(["log", msg]) + '\n'); -} -function error(msg) { - process.stdout.write(JSON.stringify(["log", msg, {"level": "error"}]) + '\n'); -} -function debug(msg) { - process.stdout.write(JSON.stringify(["log", msg, {"level": "debug"}]) + '\n'); -} - -// configuration -// [httpd] -// {"bind_address": "127.0.0.1", "port": "5984"} -// [couchmagick] -// {"dbs": "myfotos"} -var config = {}; -function parseConfig(data) { - debug('parsing config: ' + data); - - try { - data = JSON.parse(data); - } catch(e) { - error('Error parsing config: ' + e); - } - - // [httpd] - if (data.port) { - config.port = data.port; - } - if (data.bind_address) { - config.bind_address = data.bind_address; - } - - // [couchmagick] - if (data.dbs) { - config.dbs = data.dbs.split(/,\s+/); - } - if (data.user) { - config.user = data.user; - } - if (data.password) { - config.password = data.password; - } - if (data.filter) { - config.filter = data.filter; - } - - return config; -} - -// process function -function convert(doc, db, next) { - info('processing doc ' + doc._id + '...'); - next(null); -} - -// run workers -function run(config) { - if (!config.bind_address) { - return; - } - if (!config.dbs) { - return; - } - info('using config: ' + JSON.stringify(config)); - - var server = url.format({ +var url = require('url'); +var es = require('event-stream'); +var async = require('async'); +var nano = require('nano'); +var magick = require('couchmagick-listen'); +var daemon = require('couch-daemon'); + +var couchmagick = daemon(process.stdin, process.stdout, function() { + process.exit(0); +}); + +var noop = function() {}; + +couchmagick.get({ + address: 'httpd.bind_address', + port: 'httpd.port', + // auth: { + // username: pkg.name + '.username', + // password: pkg.name + '.password' + // } +}, function(config) { + + console.log(config); + + var auth = config.auth && config.auth.username && config.auth.password ? + [config.auth.username, config.auth.password].join(':') : + null; + + var couch = url.format({ protocol: 'http', - hostname: config.bind_address, - port: config.port + hostname: config.address, + port: config.port, + auth: auth }); - debug('using server: ' + server); - - var auth; - if (config.user && config.password) { - auth = { - user: config.user, - password: config.password - }; - } - - var followOptions = {}; - if (config.filter) { - followOptions.filter = config.filter; - } - - config.dbs.forEach(function(db) { - debug('starting worker for ' + db); - - var options = { - id: pkg.name, - db: { - url: url.resolve(server, db), - auth: auth - }, - follow: followOptions, - process: convert - }; - - debug('using worker options: ' + JSON.stringify(options)); - try { - var worker = couchdbWorker(options); + var options = { + limit: 100, + feed: 'continuous', + timeout: 1000 + }; - worker.on('error', function(err) { - error('Serious error: ' + err); - }); + function listen(db, next) { + couchmagick.info('Listening on ' + db); - worker.on('worker:complete', function(doc) { - debug('Completed: ' + doc._id); - }); + var stream = magick(url.resolve(couch, db), options); - worker.on('worker:error', function(err, doc) { - info('Error processing ' + doc._id + ' :' + err); - }); + // stream.pipe(couchmagick.info()); + // stream.on('error', couchmagick.error().write); - worker.start(); - } catch(e) { - error('Error starting worker: ' + e); - } - }); -} - - -module.exports = function() { - process.stdin.on('data', function(data) { - if (typeof data !== 'string') { - data = data.toString(); - } - - data.replace(/\s+$/, '').split(/\n+/).forEach(parseConfig); - - run(config); - }); + stream.on('end', next); + } - process.stdin.on('end', function () { - process.exit(0); + console.log('start list dbs'); + nano(couch).db.list(function(err, dbs) { + async.eachSeries(dbs, listen, function() { + couchmagick.info('done.'); + process.exit(0); + }); }); - // restart on config change - process.stdout.write(JSON.stringify(["register", "httpd"]) + '\n'); - process.stdout.write(JSON.stringify(["register", pkg.name]) + '\n'); - - // get config - process.stdout.write(JSON.stringify(["get", "httpd"]) + '\n'); - process.stdout.write(JSON.stringify(["get", pkg.name]) + '\n'); -}; + // TODO: listen to db changes +}); diff --git a/package.json b/package.json index d53ef2f..de47930 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "couchmagick", - "version": "0.0.1", + "version": "0.1.0", "description": "Run ImageMagicks `convert` on CouchDB documents.", "main": "index.js", "preferGlobal": true, - "bin": "bin/couchmagick", + "bin": "index.js", "repository": { "type": "git", "url": "https://github.com/null2/couchmagick" @@ -27,7 +27,11 @@ }, "homepage": "https://github.com/null2/couchmagick", "dependencies": { - "couchdb-worker": "~3.1.1" + "async": "~0.2.9", + "event-stream": "~3.0.20", + "couchmagick-listen": "~1.0.5", + "couch-daemon": "~1.1.2", + "nano": "~4.2.1" }, "devDependencies": { "jshint": "~2.3.0"