Socket
Socket
Sign inDemoInstall

couchdb-worker

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

couchdb-worker - npm Package Compare versions

Comparing version 0.6.0 to 1.0.1

.jshintrc

41

package.json
{
"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"
}
}

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc