couchdb-worker
Advanced tools
Comparing version 0.6.0 to 1.0.1
{ | ||
"name": "couchdb-worker", | ||
"version": "0.6.0", | ||
"description": "This is an abstract CouchDB worker that manages worker status and handle attachment processing.", | ||
"keywords": ["CouchDB", "couchdb-worker", "attachments", "worker", "abstract"], | ||
"description": "CouchDB worker module that manages state", | ||
"version": "1.0.1", | ||
"homepage": "https://github.com/jo/couchdb-worker", | ||
"bugs": { | ||
"url" : "http://github.com/jo/couchdb-worker/issues", | ||
"email" : "schmidt@netzmerk.com" | ||
}, | ||
"author": { | ||
@@ -18,16 +13,30 @@ "name": "Johannes J. Schmidt", | ||
"type": "git", | ||
"url": "https://github.com/jo/couchdb-worker" | ||
"url": "git://github.com/jo/couchdb-worker.git" | ||
}, | ||
"dependencies": { | ||
"request": "*", | ||
"underscore": "*" | ||
"bugs": { | ||
"url": "https://github.com/jo/couchdb-worker/issues" | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/jo/couchdb-worker/blob/master/LICENSE-MIT" | ||
} | ||
], | ||
"main": "lib/couchdb-worker", | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
}, | ||
"scripts": { | ||
"test": "grunt nodeunit" | ||
}, | ||
"devDependencies": { | ||
"request": "*", | ||
"mocha": "*" | ||
"grunt-contrib-jshint": "~0.1.1", | ||
"grunt-contrib-nodeunit": "~0.1.2", | ||
"grunt-contrib-watch": "~0.2.0", | ||
"grunt": "~0.4.0" | ||
}, | ||
"main": "index", | ||
"scripts": { | ||
"test": "mocha" | ||
"keywords": ["couchdb", "couchdb-worker", "worker"], | ||
"dependencies": { | ||
"nano": "~3.3.8" | ||
} | ||
} |
272
README.md
@@ -1,181 +0,135 @@ | ||
# CouchDB Worker | ||
# couchdb-worker | ||
A worker module that manages state. | ||
Abstract CouchDB worker module | ||
## Getting Started | ||
Install the module with: `npm install couchdb-worker` | ||
## Installation | ||
```javascript | ||
var worker = require('couchdb-worker'); | ||
var myWorker = worker.listen({ | ||
id: 'my-worker', | ||
db: 'http://localhost:5984/mydb', | ||
process: function(doc, done) { | ||
doc.computed_value = Math.random(); | ||
done(null); | ||
} | ||
}); | ||
myWorker.on('error', function(err) { | ||
console.error('Since Follow always retries on errors, this must be serious'); | ||
}); | ||
myWorker.on('worker:triggered', function(doc) { | ||
console.log('worker triggered: ', doc); | ||
}); | ||
myWorker.on('worker:completed', function(doc) { | ||
console.log('worker completed: ', doc); | ||
}); | ||
myWorker.on('worker:committed', function(doc) { | ||
console.log('worker committed: ', doc); | ||
}); | ||
myWorker.on('worker:error', function(err, doc) { | ||
console.log('worker error: ', err, doc); | ||
}); | ||
// myWorker.status(); | ||
myWorker.pause(); | ||
myWorker.resume(); | ||
myWorker.stop(); | ||
``` | ||
## Documentation | ||
The object returned by `worker.listen()` is a `Feed` object, which is an EventEmitter. | ||
See [follow](https://github.com/iriscouch/follow) for documentation. | ||
npm install couchdb-worker | ||
## Options | ||
* `id` | Unique identifier for the worker. | ||
* `process` | Processor function. Receives `doc` and `done` arguments. | ||
* `db` | [nano](https://github.com/dscape/nano) options | ||
* `follow` | [follow](https://github.com/iriscouch/follow) options | ||
## `process(doc, done)` | ||
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. | ||
## Create a new Worker | ||
You can now modify the `doc`. It will be saved by couchdb-worker. | ||
new Worker(config, db) | ||
The `done` callback accepts itself two arguments: an `error` property, | ||
where you can inform couchdb-worker about any errors (it will also be stored inside the document) | ||
and a `next` callback function which is called when the modified document is saved. | ||
## Status | ||
* couchdb-worker stores its state inside the document in an object called `worker_status`. | ||
* Each worker manages its own state inside this object, eg `worker_status.myworker`. | ||
* The state can be `triggered`, `error` or `complete`. | ||
* Only one worker can run at a time on one document. | ||
* You can store your own worker status information (a retry count for example) | ||
inside the `worker_status` object. | ||
* If the processing failed, `worker_status.myworker.error` will contain the error. | ||
Basically you define an object with two functions: _check_ and _process_: | ||
A status object can be | ||
var Worker = require("couchdb-worker"); | ||
```javascript | ||
{ | ||
worker_status: { | ||
myworker: { | ||
status: 'complete' | ||
} | ||
} | ||
} | ||
``` | ||
new Worker({ | ||
name: 'myworker', | ||
server: "http://127.0.0.1:5984", | ||
processor: { | ||
check: function(doc) { | ||
return doc.type === 'post'; | ||
}, | ||
process: function(doc, done_func) { | ||
// Do work and call done_func callback with an error object | ||
// and the attributes to be merged into the doc. | ||
// You have access to the config document, see below. | ||
done_func(null, { | ||
foo: this.config.big_animal ? 'Bär' : 'bar' | ||
}); | ||
} | ||
## Examples | ||
```javascript | ||
var worker = require('couchdb-worker'); | ||
worker.listen({ | ||
id: 'my-worker', | ||
db: { | ||
url: 'http://localhost:5984/mydb', | ||
request_defaults: { | ||
auth: { | ||
user: 'me', | ||
pass: 'secret' | ||
} | ||
}, 'mydb'); | ||
This example processor inserts the property _foo_ with the value `bar` or `Bär`, | ||
depending on the config document, into every document. | ||
The processing takes place in the `process` function, | ||
The return value of the `check` function decides wheather the worker is interested in a doc or not. | ||
## Run the Worker | ||
npm start | ||
## Per Database Configuration | ||
Configuration is done in a worker configuration document inside the target database. | ||
The worker looks only processes if there exists such a configuration file. | ||
A Worker configuration document might look like this: | ||
{ | ||
"_id": "worker-config/myworker", | ||
"_rev": "1-a653b27246b01cf9204fa9f5dee7cc64", | ||
"big_animal": true | ||
} | ||
You can update the config live so that all future processings will take the new configuration. | ||
## Worker Status Document | ||
The worker stores a status document inside the target database. | ||
The worker stores its last update seq here and can resume where it left off. | ||
{ | ||
"_id": "worker-status/myworker", | ||
"_rev": "543-1922b5623d07453a753ab6ab2c634d04", | ||
"last_seq": 34176, | ||
"docs_checked": 145, | ||
"docs_processed": 145 | ||
}, | ||
follow: { | ||
since: 42, | ||
heartbeat: 1000, | ||
filter: 'myddoc/myfilter', | ||
query_params: { | ||
worker: 'my-worker', | ||
app: '1234' | ||
} | ||
} | ||
process: function(doc, done) { | ||
doc.computed_value = Math.random(); | ||
done(null); | ||
} | ||
}); | ||
``` | ||
## Document Status Object | ||
The worker updates a status object inside the document. | ||
This makes it supereasy to monitor worker status as well as | ||
it keeps a lock when many workers listen to the same database. | ||
The status object of the worker could look like this: | ||
"worker_status": { | ||
"myworker": { | ||
"status": "completed" | ||
} | ||
} | ||
The status field can be _triggered_, _completed_ or _error_. | ||
The worker status is scoped by the worker name in order to have many workers | ||
processing the same document. | ||
## Worker Options | ||
### `name` | ||
Each worker needs a uniq name. It will be used for status objects and | ||
config - and status document ids. | ||
### `server` | ||
The CouchDB server url. | ||
### `processor.check` | ||
The `check` function is called to decide whether this doc should be processed generally. | ||
For example you might only be interested in docs of a certain field. | ||
This function is similar to a [filter function](http://guide.couchdb.org/draft/notifications.html#filters). | ||
CouchDB Worker will support Server Side Filters at some point in the future. | ||
### `processor.process` | ||
This function takes two arguments: the _doc_ and a callback function, `done_func`, | ||
which takes an _error_ and the _ouput_ of the processing when the job has been done. | ||
`done_func` also takes a status object as a third argument, | ||
which will be used to set the worker status if present. See lib/attachments-worker.js. | ||
This output will be merged (using jQuery like deep `extend`) with the doc (if _error_ is `null`) and saved. | ||
### `config_id` | ||
The id of the configuration document. Default is `worker-config/<worker-name>`. | ||
### `status_id` | ||
The id of the status document. Default is `worker-status/<worker-name>`. | ||
### `batch_size` | ||
Number of changes to keep in RAM. Higher value means less HTTP requests but higher memory consumption. | ||
Default is `10`. | ||
### `timeout` | ||
Number of miliseconds for timeout the changes feed. Defaults to `60000`. | ||
### `since` | ||
`update_seq` to initially start listening. Default is `0`. | ||
### Conflict Resolution | ||
Note that the doc could have changed after the job has been started | ||
so that the doc can not be saved. In that case CouchDB Worker will reset its worker state. | ||
The document will again show up in the changes feed an processed again. | ||
Future versions of CouchDB Worker will provide a _resolve conflict_ callback, | ||
where you can resolve that conflict in order to keep your heavy computed output. | ||
Also take a look at [examples](couchdb-worker/tree/master/examples). | ||
## Testing | ||
To run the tests, run `grunt`. | ||
Testing is done with Mocha. | ||
The tests run agains a CouchDB server, and they create random databases of the form `couchdb-worker-test-<uuid>`. | ||
The default url is `http://localhost:5984`, | ||
which can be changed by setting the `COUCH_URL` environment variable, | ||
eg via `COUCH_URL=http://me:secure@me.iriscouch.com grunt`. | ||
npm test | ||
## Contributing | ||
In lieu of a formal styleguide, take care to maintain the existing coding style. | ||
Add unit tests for any new or changed functionality. | ||
Lint and test your code using [Grunt](http://gruntjs.com/). | ||
## Versioning | ||
couchdb-worker follows [semver-ftw](http://semver-ftw.org/). | ||
Dont think 1.0.0 means production ready yet. | ||
There were some breaking changes, so had to move up the major version. | ||
## Release History | ||
* `1.0.0`: complete rewrite and new (functional) API using [nano](https://github.com/dscape/nano) | ||
(and [follow](https://github.com/iriscouch/follow)) - _currently no attachment support_ | ||
* `0.x`: object oriented version with attachment support - _The `0.x` line continues on the v0 branch_ | ||
## License & Copyright | ||
(c) null2 GmbH, 2012 | ||
License: The MIT License | ||
## License | ||
Copyright (c) 2012-2013 Johannes J. Schmidt, null2 GmbH | ||
Licensed under the MIT license. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 2 instances in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
0
1
1
18457
4
8
387
136
+ Addednano@~3.3.8
+ Addedasync@0.2.10(transitive)
+ Addedaws-sign@0.2.1(transitive)
+ Addedboom@0.3.8(transitive)
+ Addedcombined-stream@0.0.7(transitive)
+ Addedcookie-jar@0.2.0(transitive)
+ Addedcryptiles@0.1.3(transitive)
+ Addeddelayed-stream@0.0.5(transitive)
+ Addederrs@0.2.4(transitive)
+ Addedfollow@0.8.0(transitive)
+ Addedforever-agent@0.2.0(transitive)
+ Addedform-data@0.0.10(transitive)
+ Addedhawk@0.10.2(transitive)
+ Addedhoek@0.7.6(transitive)
+ Addedjson-stringify-safe@3.0.0(transitive)
+ Addedmime@1.2.11(transitive)
+ Addednano@3.3.8(transitive)
+ Addednode-uuid@1.4.8(transitive)
+ Addedoauth-sign@0.2.0(transitive)
+ Addedqs@0.5.6(transitive)
+ Addedrequest@2.16.62.2.9(transitive)
+ Addedsntp@0.1.4(transitive)
+ Addedtunnel-agent@0.2.0(transitive)
+ Addedunderscore@1.4.4(transitive)
- Removedrequest@*
- Removedunderscore@*
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.9.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removedunderscore@1.13.7(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)