Giter Club home page Giter Club logo

thoonk-jobs.js's Introduction

Thoonk Jobs

What is Thoonk?

Thoonk is a persistent (and fast!) framework for Redis backed live data and objects, such as push feeds, queues, and jobs.

See https://github.com/fritzy/thoonk.js for more on Thoonk.

What is this?

Thoonk Jobs are Redis-based job queues with support for claiming, stalling, retrying, and retracting individual tasks. Jobs are useful for distributing load, ensuring a task is completed regardless of outages, and keeping long running tasks away from synchronous interfaces.

Publish |
      V |     
        |
    +---------+  Stall>    +-------+
    |Available|------------|Stalled|
    +---------+  <Retry    +-------+
        |  | ^
    Get |  | Cancel
      V |  |
      +-------+             +--------+
      |Claimed|-------------|Finished|
      +-------+    Finish>  +--------+

Multiple Thoonk Job instances (aka workers) may share the same job queue, allowing multiple tasks to be processed in parallel.

When a task becomes available in the queue, a worker must claim it to have sole control over its processing. In the case of multiple workers receiving the available task notification, the first to claim the task wins. Once the task is claimed, if there is some recoverable error such as a lack of required resources, the task must be cancelled to send it back to the queue for other workers to claim it. The number of times that a task has been sent back is tracked to facilitate monitoring.

Tasks can also be stalled and moved to a holding area where they will not be dispatched for workers to claim. Tasks that have been cancelled multiple times are good candidates for stalling so that the repeated issues can be investigated before retrying the task.

Installing

npm install thoonk-jobs

Example

var thoonk = require('thoonk').createClient();
var Job = require('thoonk-jobs');

thoonk.registerObject('Job', Job, function () {

    var jobPublisher = thoonk.objects.Job('coolJobChannel');
    jobPublisher.subscribe(function () {

        jobPublisher.publish({whatever: 'you need as json'}, {
            //id: 'customId',
            //priority: true, // push the job to the front of the queue
            onFinish: function () {
                console.log('Job completed!');
            }
        }, function () {
            console.log('Job published');
        });

    });
});
var async = require('async');
var thoonk = require('thoonk').createClient();
var Job = require('thoonk-jobs');

thoonk.registerObject('Job', Job, function () {

    var jobWorker = thoonk.objects.Job('coolJobChannel');
    async.forever(function (next) {
        jobWorker.get(0, function (err, item, id) { 
            if (err) return next();

            item = JSON.parse(item);

            //doStuff(item);

            jobWorker.finish(id, 'the results', function (err) {
                next();  
            });
        });
    });
});

Testing

To prevent accidentally clobbering databases, tests are disabled by default. Set "enabled" to true in test_config.json to enable them, then run:

npm test

License

MIT

Created By

If you like this, follow @fritzy on twitter.

thoonk-jobs.js's People

Contributors

fritzy avatar legastero avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jlertle roboplan

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.