oneuptime/CommonServer/Infrastructure/Queue.ts

44 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-02-16 22:16:24 +00:00
import { Queue as BullQueue, JobsOptions, Job } from 'bullmq';
2023-02-15 14:54:13 +00:00
import { JSONObject } from 'Common/Types/JSON';
import ObjectID from 'Common/Types/ObjectID';
import { RedisHostname, RedisPort } from '../Config';
export enum QueueName {
2023-02-16 22:45:22 +00:00
Workflow = 'Workflow',
2023-02-15 14:54:13 +00:00
}
2023-02-16 22:16:24 +00:00
export type QueueJob = Job;
2023-02-15 14:54:13 +00:00
export default class Queue {
public static getQueue(queueName: QueueName): BullQueue {
2023-02-16 22:16:24 +00:00
return new BullQueue(queueName, {
connection: {
host: RedisHostname.toString(),
2023-02-16 22:45:22 +00:00
port: RedisPort.toNumber(),
},
2023-02-16 22:16:24 +00:00
});
2023-02-15 14:54:13 +00:00
}
2023-02-16 22:45:22 +00:00
public static async addJob(
queueName: QueueName,
jobId: ObjectID,
jobName: string,
data: JSONObject,
options?: {
scheduleAt?: string;
}
2023-02-16 23:02:42 +00:00
): Promise<void> {
2023-02-16 22:16:24 +00:00
const optionsObject: JobsOptions = {
2023-02-16 22:45:22 +00:00
jobId: jobId.toString(),
2023-02-16 22:16:24 +00:00
};
2023-02-16 22:45:22 +00:00
if (options && options.scheduleAt) {
2023-02-16 22:16:24 +00:00
optionsObject.repeat = {
2023-02-16 22:45:22 +00:00
pattern: options.scheduleAt,
};
2023-02-16 22:16:24 +00:00
}
await this.getQueue(queueName).add(jobName, data, optionsObject);
2023-02-15 14:54:13 +00:00
}
2023-02-16 22:45:22 +00:00
}