Giter Club home page Giter Club logo

dworker's Introduction

dworker

NPM version Build Status Coverage Status

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 = require('dworker')
var Broker = dw.Broker;
var Worker = dw.Worker;
var Promise = require('bluebird');
var util = require('util');

// Create a subclass of Worker.
util.inherits(MyWorker, Worker);

// Define a simple Worker.
function MyWorker() {
    Worker.apply(this, arguments); // <-- Don't forget this!
}
// Implement all virtual methods.
MyWorker.prototype.onCreate = function (info) {
    console.log('MyWorker: onCreate() is called: info=' + JSON.stringify(info));
    return Promise.resolve();
};
MyWorker.prototype.onDestroy = function (info) {
    console.log('MyWorker: onDestroy() is called: info=' + JSON.stringify(info));
    return Promise.resolve();
};
MyWorker.prototype.onAsk = function (method, data) {
    console.log('MyWorker: onAsk() is called');
    if (method === 'echo') {
        // Simply returns the data back to the agent.
        return Promise.resolve(data);
    }
    return Promise.reject(new Error('Unknown ask method: ' + method));
};
MyWorker.prototype.onTell = function (method, data) {
    console.log('MyWorker: onTell() is called');
    if (method === 'log') {
        console.log('MyWorker: logging: ' + JSON.stringify(data));
        return;
    }
    return Promise.reject(new Error('Unknown tell method: ' + method));
};


var brokerId = 'my-broker-0001'; // This must be unique across entire worker system
var br = new Broker(brokerId);

// Register your worker
br.registerWorker(MyWorker);

br.start()
.then(function () {
    console.log('Test: broker started');
    return br.createWorker('MyWorker');
})
.then(function (agent) {
    console.log('Test: worker is created: ID=' + agent.id);
    return agent.ask('echo', { msg: 'Yahoo!' })
    .then(function (data) {
        console.log('Test: echo is received: ' + data.msg);
        return agent.tell('log', { debug: 'echo successful' })
        .then(function () {
            return Promise.delay(10); // give 10 msec for the worker to log.
        });
    })
})
.then(function () {
    // Now destroy the broker
    return br.destroy()
    .then(function () {
        console.log('Test: DONE!');
        // Kill the redis connections.
        br.quit();
    });
})
.done();

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.