couchdb-worker
Advanced tools
Comparing version 3.1.2 to 3.2.0
57
index.js
@@ -14,14 +14,32 @@ /* | ||
options.follow = options.follow || {}; | ||
options.status = options.status || {}; | ||
options.status = options.status || false; | ||
options.lock = options.lock || false; | ||
// defaults | ||
options.follow.include_docs = true; | ||
options.status.db = options.status.db || options.db; | ||
options.status.id = options.status.id || 'worker-status/' + options.id; | ||
options.status.prefix = options.status.prefix || 'worker-lock/' + options.id + '/'; | ||
if (options.status === true) { | ||
options.status = {}; | ||
} | ||
if (typeof options.status === 'object') { | ||
options.status.db = options.status.db || options.db; | ||
options.status.id = options.status.id || 'worker-status/' + options.id; | ||
options.status.prefix = options.status.prefix || 'worker-lock/' + options.id + '/'; | ||
} | ||
if (options.lock === true) { | ||
options.lock = {}; | ||
} | ||
if (typeof options.lock === 'object') { | ||
options.lock.db = options.lock.db || options.status.db; | ||
options.lock.prefix = options.lock.prefix || 'worker-lock/' + options.id + '/'; | ||
} | ||
// nano modifies the options object, so this is needed. | ||
if (typeof options.status.db === 'object') { | ||
if (options.status && typeof options.status.db === 'object') { | ||
options.status.db = require('util')._extend({}, options.db); | ||
} | ||
if (options.lock && typeof options.lock.db === 'object') { | ||
options.lock.db = require('util')._extend({}, options.db); | ||
} | ||
@@ -39,9 +57,16 @@ | ||
// status database connector | ||
var statusDb = require('nano')(options.status.db); | ||
var statusDb = options.status && require('nano')(options.status.db); | ||
// lock database connector | ||
var lockDb = options.lock && require('nano')(options.lock.db); | ||
// changes feed | ||
var feed = db.follow(options.follow); | ||
// capture a document | ||
function capture(doc, done) { | ||
statusDb.insert({}, options.status.prefix + doc._id, done); | ||
if (!lockDb) { | ||
return done(null); | ||
} | ||
lockDb.insert({}, options.lock.prefix + doc._id, done); | ||
} | ||
@@ -51,3 +76,7 @@ | ||
function release(lock, done) { | ||
statusDb.destroy(lock.id, lock.rev, function(err) { | ||
if (!lockDb) { | ||
return done(null); | ||
} | ||
lockDb.destroy(lock.id, lock.rev, function(err) { | ||
if (!err) { | ||
@@ -58,3 +87,3 @@ return done(); | ||
if (err.error === 'conflict') { | ||
statusDb.get(lock._id, function(err, doc) { | ||
lockDb.get(lock._id, function(err, doc) { | ||
if (err) { | ||
@@ -78,3 +107,3 @@ return feed.emit('worker:release-error', err, lock); | ||
// discard lock | ||
var match = doc._id.match(options.status.prefix); | ||
var match = options.lock && doc._id.match(options.lock.prefix); | ||
if (match && match.index === 0) { | ||
@@ -96,2 +125,6 @@ return true; | ||
function storeStatus() { | ||
if (!statusDb) { | ||
return; | ||
} | ||
if (feed.dead) { | ||
@@ -185,2 +218,6 @@ return; | ||
feed.start = function() { | ||
if (!statusDb) { | ||
return feed.follow(); | ||
} | ||
statusDb.get(statusDoc._id, function(err, doc) { | ||
@@ -187,0 +224,0 @@ if (!err && doc) { |
{ | ||
"name": "couchdb-worker", | ||
"description": "CouchDB worker module that manages state", | ||
"version": "3.1.2", | ||
"version": "3.2.0", | ||
"homepage": "https://github.com/jo/couchdb-worker", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -31,6 +31,8 @@ # couchdb-worker | ||
* `follow` | [follow](https://github.com/iriscouch/follow) options | ||
* `status` | status options (optional) | ||
* `status` | status options (optional). Default is `false`. | ||
* `status.db` | [nano](https://github.com/dscape/nano) options for status database connection. Default is to use the `db` connection. | ||
* `status.id` | id for status document. Default is `worker-status/<id>`. | ||
* `status.prefix` | prefix for lock document ids. Default is `worker-lock/<id>/`. | ||
* `lock` | lock options (optional). Default is `false`. | ||
* `lock.db` | [nano](https://github.com/dscape/nano) options for lock database connection. Default is to use the `status.db` connection. | ||
* `lock.prefix` | prefix for lock document ids. Default is `worker-lock/<id>/`. | ||
@@ -44,13 +46,4 @@ ## `process(doc, done)` | ||
## Lock | ||
To prevent two same workers from processing the same document twice, | ||
couchdb-worker keeps a lock on the document. | ||
This is achieved by putting an empty doc inside the `status.db` while processing. | ||
It will be deleted when done. | ||
The id of that lock document is calculated by appending the documents id to `status.prefix`. | ||
## Status | ||
couchdb-worker maintains a status document, where some stats are stored: | ||
couchdb-worker can maintain a status document, where some stats are stored: | ||
@@ -70,2 +63,15 @@ ```json | ||
Its disabled by default as of `3.2.0`. To enable, set `status: true`. | ||
## Lock | ||
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 `lock.prefix`. | ||
Its disabled by default as of `3.2.0`. To enable locking, set `lock: true`. | ||
## Examples | ||
@@ -84,2 +90,13 @@ ```js | ||
}, | ||
status: { | ||
db: { | ||
url: 'http://localhost:5984/worker-stats', | ||
request_defaults: { | ||
auth: { | ||
user: 'me', | ||
pass: 'secret' | ||
} | ||
} | ||
} | ||
}, | ||
follow: { | ||
@@ -144,2 +161,3 @@ since: 42, | ||
## Release History | ||
* `3.2.0`: configurable status and lock behaviour | ||
* `3.1.1`: fix issue with db objects | ||
@@ -146,0 +164,0 @@ * `3.1.0`: process function receives db object |
@@ -159,3 +159,3 @@ 'use strict'; | ||
w.on('stop', test.done); | ||
w.on('worker:complete', function(err, doc) { | ||
w.on('worker:complete', function(doc) { | ||
test.ok(true, 'worker:complete event should have been fired'); | ||
@@ -195,3 +195,3 @@ test.equal(typeof doc, 'object', 'doc should be an object'); | ||
} | ||
var w = worker({ db: this.url, id: 'myworker', process: process }); | ||
var w = worker({ db: this.url, status: true, id: 'myworker', process: process }); | ||
var feed = this.db.follow({ include_docs: true }); | ||
@@ -198,0 +198,0 @@ feed.on('change', function(change) { |
19583
392
170