Git Product home page Git Product logo

sequelize-jobs's Introduction

Sequelize-Jobs

Sequelize-Jobs is inspired by Ruby's DelayedJob and allows processing of longer tasks in the background.

Installation

Sequelize-Jobs supports Sequelize 1.7.x.

As Sequelize supports multiple backends for storing the job queue, Sequelize-Jobs simply uses the defined backend.

$ npm install sequelize-jobs
var Sequelize = require('sequelize');

// create database as usual
var db = new Sequelize(
  "database",
  "username",
  "password",
  {}
);

// configure Sequelize-Jobs
var options = {
  maxAttempts: 1
};

SequelizeJobs = require('sequelize-jobs')(db, Sequelize, options);

Queuing Jobs

Call createJob on the SequelizeJobs object and pass the following parameters:

SequelizeJobs.createJob('helloHandler', { message: "hello world" }, options);

Available options are:

  • queue: the queue to put the job in (default: default)
  • priority: priority of the job (higher numbers first, default: 0)
  • runAt: Date object to set the date at which the job is processed. (default: NOW)

Processing Jobs

Create a worker and start processing:

var worker = SequelizeJobs.createWorker();

var myJobProcessor = function(job, done) {
  // you may use job.handler to decide which class should process it

  var myHelloHandler = function(job, done) {
    console.log("myHelloHandler says", job.data.message);
  };

  switch (job.handler) {
    case 'helloHandler':
      return myHelloHandler(job, done);

    default:
      done()
  }
};

worker.start(myJobProcessor);

On failure, the job is scheduled again in 5 seconds + N ** 4, where N is the number of retries.

Named Queues

There is also support for named queues. The goal is to provide a system for grouping tasks to be worked by separate pools of workers, which may be scaled and controlled individually. The default queue name is default.

Jobs can be assigned to a queue by setting the queue option:

SequelizeJobs.createJob('mailHandler', { data: 'here' }, { queue: 'mailing' });

Jobs will be processed by a worker of their queue only. You may pass the queue name to the worker as well:

var mailingWorker = SequelizeJobs.createWorker({ queue: 'mailing' });
var multiQueueWorker = SequelizeJobs.createWorker({ queue: ['mailing', 'default'] });

License

Copyright (c) 2014 Robert Wachs [email protected]

sequelize-jobs is released under the LGPLv3 license (see LICENSE).

sequelize-jobs's People

Contributors

clintonium-119 avatar spiderpug avatar

Watchers

 avatar

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.