CouchDB worker module that manages state
Abstract CouchDB worker module
This is depricated in favour of couch-daemon
Install the module with:
npm install couchdb-worker
var config =id: 'my-worker'db: ''doccomputed_value = Mathrandom;dbinsertdoc done;;require'couchdb-worker'configstart;
The object returned by
worker.listen() is a
Feed object, which is an EventEmitter.
See follow for documentation.
id| Unique identifier for the worker.
process| Processor function. Receives
db| nano options
follow| follow options
status| status options (optional). Default is
status.db| nano options for status database connection. Default is to use the
status.id| id for status document. Default is
lock| lock options (optional). Default is
lock.db| nano options for lock database connection. Default is to use the
lock.prefix| prefix for lock document ids. Default is
This is where you do your work. It receives a
doc, which is the current document,
as well as a
done callback function, which must be invoked when the work is done.
done callback accepts itself an
where you can inform couchdb-worker about any errors.
couchdb-worker can maintain a status document, where some stats are stored:
Its disabled by default as of
3.2.0. To enable, set
To prevent two same workers from processing the same document twice, couchdb-worker can keep a lock on the document.
This is achieved by putting an empty doc inside the
lock.db while processing.
It will be deleted when done.
The id of that lock document is calculated by appending the documents id to
Its disabled by default as of
3.2.0. To enable locking, set
var worker = require'couchdb-worker'id: 'my-worker'db:url: ''request_defaults:auth:user: 'me'pass: 'secret'status:db:url: ''request_defaults:auth:user: 'me'pass: 'secret'follow:since: 42heartbeat: 1000filter: 'myddoc/myfilter'query_params:worker: 'my-worker'app: '1234'doccomputed_value = Mathrandom;dbinsertdoc done;;// listen to some eventsworkeron'error'console.error'Since Follow always retries on errors, this must be serious';;workeron'worker:complete'console.log'worker completed: ' doc;;workeron'worker:error'console.log'worker error: ' err doc;;// start workworkerstart;// you can pause the workerworkerpause;// and resume...workerresume;// and finally stop it.workerstop;
To run the tests, run
The tests run agains a CouchDB server, and they create random databases of the form
The default url is
which can be changed by setting the
COUCH_URL environment variable, eg:
COUCH_URL= npm test
In lieu of a formal styleguide, take care to maintain the existing coding style.
Add unit tests for any new or changed functionality.
Lint your code using
npm run jshint.
couchdb-worker follows semver-ftw. Dont think 1.0.0 means production ready yet. There were some breaking changes, so had to move up the major version.
3.2.0: configurable status and lock behaviour
3.1.1: fix issue with db objects
3.1.0: process function receives db object
3.0.0: return function (
2.0.0: do not store worker status in documents, store lock in extra documents
1.0.0: complete rewrite and new (functional) API using nano (and follow) - currently no attachment support
0.x: object oriented version with attachment support - The
0.xline continues on the v0 branch
Copyright (c) 2012-2013 Johannes J. Schmidt, null2 GmbH
Licensed under the MIT license.