que-it
v1.0.0
Published
Background job processing inspired by laravel.
Downloads
5
Maintainers
Readme
QUE-IT
Background job processing inspired by laravel.
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: '[email protected]' }));
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: '[email protected]' }).on('mailer'));
Process job without queueing.
const { dispatch } = require('que-it');
const ContactMail = require('path/test/jobs');
// do some code
dispatch(new ContactMail({ to: '[email protected]' }).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.