UNPKG

redis-smq-common

Version:

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

64 lines 2.77 kB
import { PowerSwitch } from '../power-switch/index.js'; import { WorkerAlreadyDownError, WorkerAlreadyRunningError, } from './errors/index.js'; import { EWorkerThreadParentMessage, EWorkerType, } from './types/index.js'; import { Worker } from './worker.js'; export class WorkerRunnable extends Worker { type = EWorkerType.RUNNABLE; powerSwitch; constructor(workerFilename, initialPayload, logger) { super(workerFilename, initialPayload); this.logger = logger ?? this.logger; this.powerSwitch = new PowerSwitch(); this.logger.info(`WorkerRunnable instance created for ${workerFilename}`); this.logger.debug('WorkerRunnable initialization details', { id: this.id, type: 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: 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 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 WorkerAlreadyDownError()); } } } //# sourceMappingURL=worker-runnable.js.map