X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=92d68bcb6e9d4f38fdb3cc87170a038c4f226722;hb=c2d2417cb80d8c14637fe6a19d58bcf5ce18ae98;hp=ba74409e2b5c53aa7ddb3c9a3b34ad8ec2515b11;hpb=d3d981d0642550d0ece8d9368dea362668f83904;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index ba74409e..92d68bcb 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -148,9 +148,9 @@ export abstract class AbstractWorker< if (message.statistics != null) { // Statistics message received this.statistics = message.statistics - } else if (message.dynamic === true) { - // Worker dynamic message received - this.startCheckAlive() + } else if (message.checkAlive != null) { + // Check alive message received + message.checkAlive ? this.startCheckAlive() : this.stopCheckAlive() } else if (message.id != null && message.data != null) { // Task message received const fn = this.getTaskFunction(message.name) @@ -159,13 +159,16 @@ export abstract class AbstractWorker< } else { this.runInAsyncScope(this.runSync.bind(this), this, fn, message) } - } else if (message.kill != null) { + } else if (message.kill === true) { // Kill message received - this.aliveInterval != null && clearInterval(this.aliveInterval) + this.stopCheckAlive() this.emitDestroy() } } + /** + * Starts the worker alive check interval. + */ private startCheckAlive (): void { this.lastTaskTimestamp = performance.now() this.aliveInterval = setInterval( @@ -175,6 +178,25 @@ export abstract class AbstractWorker< this.checkAlive.bind(this)() } + /** + * Stops the worker alive check interval. + */ + private stopCheckAlive (): void { + this.aliveInterval != null && clearInterval(this.aliveInterval) + } + + /** + * Checks if the worker should be terminated, because its living too long. + */ + private checkAlive (): void { + if ( + performance.now() - this.lastTaskTimestamp > + (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) + ) { + this.sendToMainWorker({ kill: this.opts.killBehavior }) + } + } + /** * Returns the main worker. * @@ -196,18 +218,6 @@ export abstract class AbstractWorker< message: MessageValue ): void - /** - * Checks if the worker should be terminated, because its living too long. - */ - protected checkAlive (): void { - if ( - performance.now() - this.lastTaskTimestamp > - (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) - ) { - this.sendToMainWorker({ kill: this.opts.killBehavior }) - } - } - /** * Handles an error and convert it to a string so it can be sent back to the main worker. *