X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=4aad8e62669335c3940ba3fb88cc08afda062eb3;hb=7e2a464b15953320c79646e2ff88d953db4e5b96;hp=8f86fb485cd26f30efcc51a1546eb1752579ba24;hpb=df422579fde9c2638e5802e94daa4f6eda698b2b;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 8f86fb48..4aad8e62 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -10,7 +10,7 @@ const DEFAULT_MAX_INACTIVE_TIME = 1000 * 60 const DEFAULT_KILL_BEHAVIOR: KillBehavior = KillBehaviors.SOFT /** - * Base class containing some shared logic for all poolifier workers. + * Base class that implements some shared logic for all poolifier workers. * * @template MainWorker Type of main worker. * @template Data Type of data this worker receives from pool's execution. This can only be serializable data. @@ -33,7 +33,6 @@ export abstract class AbstractWorker< * Options for the worker. */ public readonly opts: WorkerOptions - /** * Constructs a new poolifier worker. * @@ -66,7 +65,7 @@ export abstract class AbstractWorker< this.checkWorkerOptions(this.opts) this.lastTaskTimestamp = Date.now() // Keep the worker active - if (!isMain) { + if (isMain === false) { this.aliveInterval = setInterval( this.checkAlive.bind(this), (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2 @@ -75,25 +74,32 @@ export abstract class AbstractWorker< } this.mainWorker?.on('message', (value: MessageValue) => { - if (value?.data && value.id) { - // Here you will receive messages - if (this.opts.async) { - this.runInAsyncScope(this.runAsync.bind(this), this, fn, value) - } else { - this.runInAsyncScope(this.run.bind(this), this, fn, value) - } - } else if (value.parent) { - // Save a reference of the main worker to communicate with it - // This will be received once - this.mainWorker = value.parent - } else if (value.kill) { - // Here is time to kill this worker, just clearing the interval - if (this.aliveInterval) clearInterval(this.aliveInterval) - this.emitDestroy() - } + this.messageListener(value, fn) }) } + protected messageListener ( + value: MessageValue, + fn: (data: Data) => Response + ): void { + if (value.data !== undefined && value.id !== undefined) { + // Here you will receive messages + if (this.opts.async) { + this.runInAsyncScope(this.runAsync.bind(this), this, fn, value) + } else { + this.runInAsyncScope(this.run.bind(this), this, fn, value) + } + } else if (value.parent !== undefined) { + // Save a reference of the main worker to communicate with it + // This will be received once + this.mainWorker = value.parent + } else if (value.kill !== undefined) { + // Here is time to kill this worker, just clearing the interval + if (this.aliveInterval) clearInterval(this.aliveInterval) + this.emitDestroy() + } + } + private checkWorkerOptions (opts: WorkerOptions) { this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR this.opts.maxInactiveTime = @@ -165,8 +171,10 @@ export abstract class AbstractWorker< value: MessageValue ): void { try { + const startTaskTimestamp = Date.now() const res = fn(value.data) - this.sendToMainWorker({ data: res, id: value.id }) + const taskRunTime = Date.now() - startTaskTimestamp + this.sendToMainWorker({ data: res, id: value.id, taskRunTime }) } catch (e) { const err = this.handleError(e as Error) this.sendToMainWorker({ error: err, id: value.id }) @@ -185,9 +193,11 @@ export abstract class AbstractWorker< fn: (data?: Data) => Promise, value: MessageValue ): void { + const startTaskTimestamp = Date.now() fn(value.data) .then(res => { - this.sendToMainWorker({ data: res, id: value.id }) + const taskRunTime = Date.now() - startTaskTimestamp + this.sendToMainWorker({ data: res, id: value.id, taskRunTime }) return null }) .catch(e => {