use couchmagick-listen

pull/20/head
Johannes J. Schmidt 11 years ago
parent 30c1b47d40
commit b53060fec3

@ -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`.

@ -1,3 +0,0 @@
#!/usr/bin/env node
require('..')();

@ -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
});

@ -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"

Loading…
Cancel
Save