UNPKG

redis-smq-common

Version:

RedisSMQ Common Library provides many components that are mainly used by RedisSMQ and RedisSMQ Monitor.

67 lines 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WorkerRunnable = void 0; const index_js_1 = require("../power-switch/index.js"); const index_js_2 = require("./errors/index.js"); const index_js_3 = require("./types/index.js"); const worker_js_1 = require("./worker.js"); class WorkerRunnable extends worker_js_1.Worker { constructor(workerFilename, initialPayload, logger) { super(workerFilename, initialPayload); this.type = index_js_3.EWorkerType.RUNNABLE; this.logger = logger !== null && logger !== void 0 ? logger : this.logger; this.powerSwitch = new index_js_1.PowerSwitch(); this.logger.info(`WorkerRunnable instance created for ${workerFilename}`); this.logger.debug('WorkerRunnable initialization details', { id: this.id, type: index_js_3.EWorkerType[this.type], hasCustomLogger: !!logger, initialPayload: this.initialPayload ? 'provided' : 'none', }); } run(cb) { this.logger.info(`Attempting to run worker ${this.id}`); const r = this.powerSwitch.goingUp(); if (r) { this.logger.debug('Power switch state changed to going up'); this.logger.debug('Registering worker thread event handlers'); this.registerEvents(this); this.logger.debug(`Posting RUN message to worker thread`); this.postMessage({ type: index_js_3.EWorkerThreadParentMessage.RUN }); this.logger.debug('Committing power switch state change'); this.powerSwitch.commit(); this.logger.info(`Worker ${this.id} started successfully`); cb(); } else { this.logger.warn(`Cannot start worker ${this.id}: already running`); cb(new index_js_2.WorkerAlreadyRunningError()); } } shutdown(cb) { this.logger.info(`Attempting to shut down worker ${this.id}`); const r = this.powerSwitch.goingDown(); if (r) { this.logger.debug('Power switch state changed to going down'); this.logger.debug('Calling parent shutdown method'); super.shutdown((err) => { if (err) { this.logger.warn(`Error during worker ${this.id} shutdown: ${err.message}`, { error: err.message, stack: err.stack, }); } this.logger.debug('Committing power switch state change'); this.powerSwitch.commit(); this.logger.info(`Worker ${this.id} shut down successfully`); cb(); }); } else { this.logger.warn(`Cannot shut down worker ${this.id}: already down`); cb(new index_js_2.WorkerAlreadyDownError()); } } } exports.WorkerRunnable = WorkerRunnable; //# sourceMappingURL=worker-runnable.js.map