UNPKG

redis-smq-common

Version:

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

41 lines 1.43 kB
import { EWorkerThreadChildExecutionCode, EWorkerThreadChildExitCode, EWorkerThreadParentMessage, } from '../types/index.js'; import { exit } from './worker-thread-message.js'; export function handleWorkerRunnable(worker, messagePort, initialPayload) { let instance = null; try { instance = worker(initialPayload); } catch (err) { exit(EWorkerThreadChildExitCode.INVALID_WORKER_TYPE, err); } if (!instance || typeof instance !== 'object' || !instance?.run || !instance?.shutdown) { exit(EWorkerThreadChildExitCode.INVALID_WORKER_TYPE); } else { const run = () => { try { instance?.run((err) => { if (err) exit(EWorkerThreadChildExecutionCode.PROCESSING_ERROR, err); }); } catch (err) { exit(EWorkerThreadChildExecutionCode.PROCESSING_CAUGHT_ERROR, err); } }; const shutdown = () => { instance?.shutdown(() => void 0); }; const onMessage = (message) => { if (message.type === EWorkerThreadParentMessage.RUN) run(); if (message.type === EWorkerThreadParentMessage.SHUTDOWN) shutdown(); }; messagePort.on('message', onMessage); } } //# sourceMappingURL=worker-thread-runnable.js.map