que-it
Background job processing inspired by laravel.
Last updated 4 months ago by bitnbytes .
ISC · Original npm · Tarball · package.json
$ cnpm install que-it 
SYNC missed versions from official npm registry.

QUE-IT

Background job processing inspired by laravel.

build status Coverage Status node version NPM version David deps

USAGE

Sending contact mail example.

You need to add jobs directory in your projects package.json file.

{
  ...
  "queit": {
    "jobsDir": "path/test/jobs"
  }
}

Then create a job class ContactMail.js in decalred directory.

Note: File name should match the class name.

const { Job } = require('que-it');

class ContactMail extends Job {
  constructor({ to }) {
    this.to = to;
    /*
    bull JobOptions
    this.$opts = {
      priority: number;
      delay: number;
      attempts: number;

      repeat: {
        tz?: string,
        endDate?: Date | string | number
      }
    };
    */
  }

  async run() {
    // do some fun
    // SomeMailingPackage.to(this.to).send('Hi!');
  }
}

module.exports = ContactMail;

In your controller

const { dispatch } = require('que-it');
const ContactMail = require('path/test/jobs');

// do some code

dispatch(new ContactMail({ to: 'user@example.com' }));

dispatch method will push your job on bull queue.

In order to start processing queue, you need to start worker as background process with forever/pm2 or what ever process manager you like.

Worker file example.

const { worker, config } = require('que-it');

// add queue names used in project to worker
config.addQueue('default');

// passing options
// config.addQueue('default', {redis: {port: 6379, host: '127.0.0.1', password: 'foobared'}});

// will return the object of queues created by worker
// Single process:
const queues = worker.start();

// You can use concurrency as well:
// const queues = worker.start({ concurrency : 5 });
// and name your processor
// const queues = worker.start({ name: 'myProcessor', concurrency : 5 });

queues['default'].on('completed', () => {
  // bull queue event
});

Worker will create logs directory in current working directory to save job processing logs.

Adding multiple queues

config.addQueue('default');
config.addQueue('encoder');
config.addQueue('mailer');

Dispatching job on specific queue

dispatch(new ContactMail({ to: 'user@example.com' }).on('mailer'));

Process job without queueing.

const { dispatch } = require('que-it');
const ContactMail = require('path/test/jobs');

// do some code

dispatch(new ContactMail({ to: 'user@example.com' }).now());

Configure

Changing default queue drivers.

const { config } = require('que-it');
// every job will be processed immediately instead of queue in case of sync drivers
config.set('driver', 'sync');

// queueable driver
// config.set('driver', 'bull');

Note: Producer, Worker and Processor each runs as seperate process, on fly config in one does not effect another, considure using file based config.

Creating config file

config.set('driver', 'sync');
config.setJobsDir('./app/jobs');
config.save();

Adding jobs from different directories

config.addJob('PasswordReset', './modules/auth/PasswordReset.js');

Que-it considers job as independent module. In case of using database, you have to write a boot file for that.

Example with mongodb:

// boot.js
// configure env variables
require('dotenv').config();
const mongoose = require('mongoose');
// db connection
mongoose.connect(process.env.MONGODB, { useNewUrlParser: true }, (err) => {
  if (err) {
    console.error(err);
  }
});

You need to register boot file in package.json of your project.

{
  ...
  "queit": {
    "jobsDir": "path/test/jobs",
    "booter": "./boot.js"
  }
}

By using this boot file, you will be able to use mongoose models in you jobs. Make sure to require used models (populate models also) in job files otherwise mongoose will throw "MissingSchemaError: Schema hasn't been registered for model" this error.

For more configuration, read Bull documentation.

Current Tags

  • 0.2.0                                ...           latest (4 months ago)

11 Versions

  • 0.2.0                                ...           4 months ago
  • 0.1.9                                ...           8 months ago
  • 0.1.8                                ...           8 months ago
  • 0.1.7                                ...           8 months ago
  • 0.1.6                                ...           8 months ago
  • 0.1.5                                ...           8 months ago
  • 0.1.4                                ...           9 months ago
  • 0.1.3                                ...           9 months ago
  • 0.1.2                                ...           9 months ago
  • 0.1.1                                ...           9 months ago
  • 0.1.0                                ...           9 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (5)
Dev Dependencies (7)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |