torotask
Version:
Task queue processing in NodeJS based on BullMQ and Redis
71 lines • 2.26 kB
JavaScript
import { FlowProducer } from 'bullmq';
import { TaskJob } from './job.js';
import { convertJobOptions } from './utils/convert-job-options.js';
export class TaskWorkflow extends FlowProducer {
taskClient;
constructor(taskClient, options) {
options = options || {};
options.prefix = options.prefix || taskClient.queuePrefix;
options.connection = options.connection = taskClient.connectionOptions;
super(options);
this.taskClient = taskClient;
}
/**
* Override the Job class to use TaskJob
* @returns {typeof Job}
*/
get Job() {
return TaskJob;
}
_convertToFlow(run, options, removeParentOption) {
const task = this.taskClient.getTask(run.taskGroup, run.taskName);
if (!task) {
throw new Error(`Task ${run.taskGroup}.${run.taskName} not found`);
}
const queueName = task.queueName;
let mergedOptions = {
...task.jobsOptions,
...options,
...run.options,
};
if (removeParentOption && mergedOptions.parent) {
delete mergedOptions.parent;
}
else {
mergedOptions = convertJobOptions(mergedOptions);
}
return {
name: run.name ?? run.taskName,
queueName: queueName,
data: {
payload: run.payload,
state: run.state,
},
opts: mergedOptions,
children: run.children?.map((child) => this._convertToFlow(child, options, true)) || undefined,
};
}
/**
* Run a flow with the given options
* @param run
* @param options
* @returns (Promise<TaskFlowRunNode>)
*/
async runFlow(run, options) {
const flow = this._convertToFlow(run, options);
return await this.add(flow);
}
/**
* Run multiple flows with the given options
* @param runs
* @param options
* @returns (Promise<TaskFlowRunNode[]>)
*/
async runFlows(runs, options) {
const flows = runs.map((run) => {
return this._convertToFlow(run, options);
});
return await this.addBulk(flows);
}
}
//# sourceMappingURL=workflow.js.map