adonis5-queue
Version:
Adonis JS 5 queue package based backed by Kue and Kue-scheduler
163 lines (162 loc) • 4.65 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const kue_1 = __importDefault(require("kue"));
const randomstring_1 = __importDefault(require("randomstring"));
const errors_1 = require("../errors");
/**
* Parse producer job contents and generate kue job
*
* @version 2.0.0
* @adonis-version 4.0+
*/
class JobMaker {
/**
* Get the final made job
* @return {Kue/Job}
*/
getFinalJob() {
return this.kueJob;
}
/**
* Inject the app job
* @param {App/Job} job
*/
setAppJob(job) {
this.job = job;
return this;
}
/**
* Inject the Kue queue
* @param {Queue} queue
*/
setQueue(queue) {
this.queue = queue;
return this;
}
/**
* Run through the making procedures
* @return {this}
*/
process() {
return this.initialize()
.assignPriority()
.assignFailureAttempts()
.assignFailureBackoff()
.assignFailureBackoff()
.assignDelay()
.assignTTL()
.assignUnique()
.assignEventListeners();
}
/**
* Initalize the Kue Job
* @param {App/Job} job
* @return {this}
*/
initialize() {
// generate an UUID for this job along with its type
this.job.data['__unique_id__'] = this.job.constructor.type + randomstring_1.default.generate(15);
this.kueJob = this.queue.createJob(this.job.constructor.type, this.job.data);
return this;
}
/**
* Set priority for the job
* @return {this}
*/
assignPriority() {
if (this.job.priority) {
this.kueJob.priority(this.job.priority);
}
return this;
}
/**
* Set failure attempts for the job
* @return {this}
*/
assignFailureAttempts() {
if (this.job.attempts) {
this.kueJob.attempts(this.job.attempts);
}
return this;
}
/**
* Set failure backoff for the job
* @return {this}
*/
assignFailureBackoff() {
if (this.job.backoff) {
this.kueJob.backoff(this.job.backoff);
}
return this;
}
/**
* Set job delay
* @return {this}
*/
assignDelay() {
if (this.job.delay) {
this.kueJob.delay(this.job.delay * 1000);
}
return this;
}
/**
* Set Time to live for the job
* @return {this}
*/
assignTTL() {
if (this.job.ttl) {
this.kueJob.ttl(this.job.ttl * 1000);
}
return this;
}
/**
* Set uniqueness of this job
* @return {this}
*/
assignUnique() {
if (this.job.constructor.unique) {
this.kueJob.unique(this.job.constructor.type);
}
return this;
}
/**
* Assign event listeners for the job
* @return {this}
*/
assignEventListeners() {
let events = ['enqueue', 'start', 'promotion', 'progress', 'failed attempts', 'failed', 'complete'];
events.forEach((event) => {
this.queue.on(`job ${event}`, (id, ...args) => {
kue_1.default.Job.get(id, (err, kueJob) => {
if (!err) {
if (kueJob.data['__unique_id__'] === this.job.data['__unique_id__']) {
const eventName = 'on' +
event
.split(' ')
.map((word) => {
return word[0].toUpperCase() + word.slice(1);
})
.join('');
if (event === 'enqueue') {
this.job.onInit(kueJob);
// save kue job to job when enqueued
this.job.kueJob = kueJob;
}
if (this.job[eventName]) {
this.job[eventName](...args);
}
}
}
else if (this.job['onFailed']) {
this.job.onFailed(new errors_1.JobFetchError(`Failed to fetch job id ${id}, event ${event}`).setError(err).updateMessage());
}
});
});
});
return this;
}
}
exports.default = JobMaker;