UNPKG

actionhero

Version:

The reusable, scalable, and quick node.js API server for stateless and stateful applications

185 lines (184 loc) 9.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ResqueInitializer = void 0; const node_resque_1 = require("node-resque"); const index_1 = require("../index"); /** * The node-resque workers and scheduler which process tasks. * see https://github.com/actionhero/node-resque */ class ResqueInitializer extends index_1.Initializer { constructor() { super(); this.startQueue = async () => { index_1.api.resque.queue = new node_resque_1.Queue({ connection: index_1.api.resque.connectionDetails }, index_1.api.tasks.jobs); index_1.api.resque.queue.on("error", (error) => { (0, index_1.log)(error.toString(), "error", "[api.resque.queue]"); }); await index_1.api.resque.queue.connect(); }; this.stopQueue = () => { if (index_1.api.resque.queue) { return index_1.api.resque.queue.end(); } }; this.startScheduler = async () => { if (index_1.config.tasks.scheduler === true) { index_1.api.resque.scheduler = new node_resque_1.Scheduler({ connection: index_1.api.resque.connectionDetails, timeout: index_1.config.tasks.timeout, stuckWorkerTimeout: index_1.config.tasks.stuckWorkerTimeout, retryStuckJobs: index_1.config.tasks.retryStuckJobs, }); index_1.api.resque.scheduler.on("error", (error) => { (0, index_1.log)(error.toString(), "error", "[api.resque.scheduler]"); }); await index_1.api.resque.scheduler.connect(); index_1.api.resque.scheduler.on("start", () => { (0, index_1.log)("resque scheduler started", index_1.config.tasks.schedulerLogging.start); }); index_1.api.resque.scheduler.on("end", () => { (0, index_1.log)("resque scheduler ended", index_1.config.tasks.schedulerLogging.end); }); index_1.api.resque.scheduler.on("poll", () => { (0, index_1.log)("resque scheduler polling", index_1.config.tasks.schedulerLogging.poll); }); index_1.api.resque.scheduler.on("leader", () => { (0, index_1.log)("This node is now the Resque scheduler leader", "notice"); }); index_1.api.resque.scheduler.on("cleanStuckWorker", (workerName, errorPayload, delta) => { (0, index_1.log)("cleaned stuck worker", "warning", { workerName, errorPayload, delta, }); }); index_1.api.resque.scheduler.start(); } }; this.stopScheduler = async () => { var _a; return (_a = index_1.api.resque.scheduler) === null || _a === void 0 ? void 0 : _a.end(); }; this.startMultiWorker = async () => { index_1.api.resque.multiWorker = new node_resque_1.MultiWorker({ connection: index_1.api.resque.connectionDetails, queues: Array.isArray(index_1.config.tasks.queues) ? index_1.config.tasks.queues : await index_1.config.tasks.queues(), timeout: index_1.config.tasks.timeout, checkTimeout: index_1.config.tasks.checkTimeout, minTaskProcessors: index_1.config.tasks.minTaskProcessors, maxTaskProcessors: index_1.config.tasks.maxTaskProcessors, maxEventLoopDelay: index_1.config.tasks.maxEventLoopDelay, }, index_1.api.tasks.jobs); // normal worker emitters index_1.api.resque.multiWorker.on("start", (workerId) => { (0, index_1.log)("[ worker ] started", index_1.config.tasks.workerLogging.start, { workerId, }); }); index_1.api.resque.multiWorker.on("end", (workerId) => { (0, index_1.log)("[ worker ] ended", index_1.config.tasks.workerLogging.end, { workerId, }); }); index_1.api.resque.multiWorker.on("cleaning_worker", (workerId, worker, pid) => { (0, index_1.log)(`[ worker ] cleaning old worker ${worker}, (${pid})`, index_1.config.tasks.workerLogging.cleaning_worker); }); index_1.api.resque.multiWorker.on("poll", (workerId, queue) => { (0, index_1.log)(`[ worker ] polling ${queue}`, index_1.config.tasks.workerLogging.poll, { workerId, }); }); index_1.api.resque.multiWorker.on("job", (workerId, queue, job) => { (0, index_1.log)(`[ worker ] working job ${queue}`, index_1.config.tasks.workerLogging.job, { workerId, class: job.class, queue: job.queue, args: JSON.stringify(index_1.utils.filterObjectForLogging(job.args[0])), }); }); index_1.api.resque.multiWorker.on("reEnqueue", (workerId, queue, job, plugin) => { (0, index_1.log)("[ worker ] reEnqueue task", index_1.config.tasks.workerLogging.reEnqueue, { workerId, plugin: JSON.stringify(plugin), class: job.class, queue: job.queue, }); }); index_1.api.resque.multiWorker.on("pause", (workerId) => { (0, index_1.log)("[ worker ] paused", index_1.config.tasks.workerLogging.pause, { workerId, }); }); index_1.api.resque.multiWorker.on("failure", (workerId, queue, job, failure) => { index_1.api.exceptionHandlers.task(failure, queue, job, workerId); }); index_1.api.resque.multiWorker.on("error", (error, workerId, queue, job) => { index_1.api.exceptionHandlers.task(error, queue, job, workerId); }); index_1.api.resque.multiWorker.on("success", (workerId, queue, job, result, duration) => { const payload = { workerId, class: job.class, queue: job.queue, args: JSON.stringify(index_1.utils.filterObjectForLogging(job.args[0])), result, duration, }; (0, index_1.log)("[ worker ] task success", index_1.config.tasks.workerLogging.success, payload); }); // multiWorker emitters index_1.api.resque.multiWorker.on("multiWorkerAction", (verb, delay) => { (0, index_1.log)(`[ multiworker ] checked for worker status: ${verb} (event loop delay: ${delay}ms)`, index_1.config.tasks.workerLogging.multiWorkerAction); }); if (index_1.config.tasks.minTaskProcessors > 0) { index_1.api.resque.multiWorker.start(); } }; this.stopMultiWorker = async () => { if (index_1.api.resque.multiWorker && index_1.config.tasks.minTaskProcessors > 0) { return index_1.api.resque.multiWorker.stop(); } }; this.name = "resque"; this.loadPriority = 600; this.startPriority = 950; this.stopPriority = 100; } async initialize() { var _a, _b; index_1.api.resque = { queue: null, multiWorker: null, scheduler: null, connectionDetails: Object.assign({}, index_1.config.tasks.connectionOptions.tasks, { redis: index_1.api.redis.clients.tasks, pkg: ["RedisMock", "_RedisMock"].includes((_b = (_a = index_1.api.redis.clients.tasks) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name) ? "ioredis-mock" : "ioredis", }), startQueue: this.startQueue, stopQueue: this.stopQueue, startScheduler: this.startScheduler, stopScheduler: this.stopScheduler, startMultiWorker: this.startMultiWorker, stopMultiWorker: this.stopMultiWorker, }; } async start() { if (index_1.config.tasks.minTaskProcessors === 0 && index_1.config.tasks.maxTaskProcessors > 0) { index_1.config.tasks.minTaskProcessors = 1; } await index_1.api.resque.startScheduler(); await index_1.api.resque.startMultiWorker(); } async stop() { await index_1.api.resque.stopScheduler(); await index_1.api.resque.stopMultiWorker(); await index_1.api.resque.stopQueue(); } } exports.ResqueInitializer = ResqueInitializer;