2021-02-09 14:32:40 -08:00
|
|
|
import * as Bull from 'bull';
|
2021-06-22 14:18:52 -07:00
|
|
|
import { config } from '@config';
|
2021-02-09 14:32:40 -08:00
|
|
|
import { IBullJobData } from './Interfaces';
|
|
|
|
|
|
|
|
export class Queue {
|
|
|
|
private jobQueue: Bull.Queue;
|
2021-06-22 14:18:52 -07:00
|
|
|
|
2021-02-09 14:32:40 -08:00
|
|
|
constructor() {
|
|
|
|
const prefix = config.get('queue.bull.prefix') as string;
|
|
|
|
const redisOptions = config.get('queue.bull.redis') as object;
|
2021-06-22 14:18:52 -07:00
|
|
|
// Disabling ready check is necessary as it allows worker to
|
2021-02-09 14:32:40 -08:00
|
|
|
// quickly reconnect to Redis if Redis crashes or is unreachable
|
|
|
|
// for some time. With it enabled, worker might take minutes to realize
|
|
|
|
// redis is back up and resume working.
|
|
|
|
// More here: https://github.com/OptimalBits/bull/issues/890
|
|
|
|
// @ts-ignore
|
|
|
|
this.jobQueue = new Bull('jobs', { prefix, redis: redisOptions, enableReadyCheck: false });
|
|
|
|
}
|
2021-06-22 14:18:52 -07:00
|
|
|
|
2021-02-09 14:32:40 -08:00
|
|
|
async add(jobData: IBullJobData, jobOptions: object): Promise<Bull.Job> {
|
|
|
|
return await this.jobQueue.add(jobData,jobOptions);
|
|
|
|
}
|
2021-06-22 14:18:52 -07:00
|
|
|
|
2021-02-09 14:32:40 -08:00
|
|
|
async getJob(jobId: Bull.JobId): Promise<Bull.Job | null> {
|
|
|
|
return await this.jobQueue.getJob(jobId);
|
|
|
|
}
|
2021-06-22 14:18:52 -07:00
|
|
|
|
2021-02-09 14:32:40 -08:00
|
|
|
async getJobs(jobTypes: Bull.JobStatus[]): Promise<Bull.Job[]> {
|
|
|
|
return await this.jobQueue.getJobs(jobTypes);
|
|
|
|
}
|
2021-06-22 14:18:52 -07:00
|
|
|
|
2021-02-09 14:32:40 -08:00
|
|
|
getBullObjectInstance(): Bull.Queue {
|
|
|
|
return this.jobQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-22 14:18:52 -07:00
|
|
|
*
|
2021-02-09 14:32:40 -08:00
|
|
|
* @param job A Bull.Job instance
|
|
|
|
* @returns boolean true if we were able to securely stop the job
|
|
|
|
*/
|
|
|
|
async stopJob(job: Bull.Job): Promise<boolean> {
|
|
|
|
if (await job.isActive()) {
|
|
|
|
// Job is already running so tell it to stop
|
|
|
|
await job.progress(-1);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Job did not get started yet so remove from queue
|
|
|
|
try {
|
|
|
|
await job.remove();
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
await job.progress(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let activeQueueInstance: Queue | undefined;
|
|
|
|
|
|
|
|
export function getInstance(): Queue {
|
|
|
|
if (activeQueueInstance === undefined) {
|
|
|
|
activeQueueInstance = new Queue();
|
|
|
|
}
|
2021-06-22 14:18:52 -07:00
|
|
|
|
2021-02-09 14:32:40 -08:00
|
|
|
return activeQueueInstance;
|
|
|
|
}
|