Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
que-it
Advanced tools
Readme
Background job processing inspired by laravel.
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());
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.
FAQs
Background job processing inspired by laravel.
The npm package que-it receives a total of 4 weekly downloads. As such, que-it popularity was classified as not popular.
We found that que-it demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.