dworker
Distributed worker system backed by Redis.
Installation
$ npm install dworker [--save]
Features
- A Node.js version of "Actor model", where the 'actor' is called Worker.
- Supports ask() and tell() methods for worker communication. (Built-in RPC)
- Score based load balancing across server cluster.
- Ability to defines more than one "clusters" to set up which cluster a worker should be running in.
- Automatic worker recovery feature. The system detects a death of processes, then salvage workers that were running on the dead process, then automatically reconstruct them on other processes in a collaborative manner.
- No polling. The dworker uses Redis pubsub to signal other brokers (a manger of workers) to wake them up.
- Automatic health-check feature. Each broker periodically check the logical adjacent neighbor broker. When a broker is found dead, the workers held by the dead broker will automatically migrated to other active brokers.
- All distributed - no central entity (except redis-server).
Requirements
- Requires Redis version 2.6.0 or later (dworker uses lua)
API
Generate JSDoc API document by the following command:
$ npm run doc
You should the document at ./doc/index.html
Example
Here's an simple example:
var dw = var Broker = dwBroker;var Worker = dwWorker;var Promise = ;var util = ; // Create a subclass of Worker.util; // Define a simple Worker. { Worker; // <-- Don't forget this!}// Implement all virtual methods.MyWorkerprototype { console; return Promise;};MyWorkerprototype { console; return Promise;};MyWorkerprototype { console; if method === 'echo' // Simply returns the data back to the agent. return Promise; return Promise;};MyWorkerprototype { console; if method === 'log' console; return; return Promise;}; var brokerId = 'my-broker-0001'; // This must be unique across entire worker systemvar br = brokerId; // Register your workerbr; brstart;
Limitation
- If nodjs < 0.12.0, dworker does not work with cluster module. (#1 listen(0) issue). Use node >= 0.12.0 to use cluster module.
Consideration
- Design your application in a way that critical data is always stored on persistent storage in case the data on Redis server is unexpectedly wiped out.
LICENSE
The MIT License (MIT)
Copyright (c) 2015 ngmoco, LLC.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.