From 29d8b961b1907797d251871a97092973d342c63c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 14 Aug 2023 16:34:52 +0200 Subject: [PATCH] refactor: cleanup main worker checks MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/worker/abstract-worker.ts | 21 ++++++++------------- src/worker/cluster-worker.ts | 2 +- src/worker/thread-worker.ts | 1 - 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 508d8689..911c31ca 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -298,7 +298,9 @@ export abstract class AbstractWorker< * @param message - The received message. */ protected messageListener (message: MessageValue): void { - if (message.workerId != null && message.workerId !== this.id) { + if (this.isMain) { + throw new Error('Cannot handle message to worker in main worker') + } else if (message.workerId != null && message.workerId !== this.id) { throw new Error('Message worker id does not match worker id') } else if (message.workerId === this.id) { if (message.statistics != null) { @@ -306,9 +308,7 @@ export abstract class AbstractWorker< this.statistics = message.statistics } else if (message.checkActive != null) { // Check active message received - !this.isMain && message.checkActive - ? this.startCheckActive() - : this.stopCheckActive() + message.checkActive ? this.startCheckActive() : this.stopCheckActive() } else if (message.taskId != null && message.data != null) { // Task message received this.run(message) @@ -325,11 +325,9 @@ export abstract class AbstractWorker< * @param message - The kill message. */ protected handleKillMessage (message: MessageValue): void { - if (!this.isMain) { - this.stopCheckActive() - this.opts.killHandler?.() - this.emitDestroy() - } + this.stopCheckActive() + this.opts.killHandler?.() + this.emitDestroy() } /** @@ -403,9 +401,6 @@ export abstract class AbstractWorker< * @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found. */ protected run (task: Task): void { - if (this.isMain) { - throw new Error('Cannot run a task in the main worker') - } const fn = this.getTaskFunction(task.name) if (isAsyncFunction(fn)) { this.runInAsyncScope(this.runAsync.bind(this), this, fn, task) @@ -537,7 +532,7 @@ export abstract class AbstractWorker< } private updateLastTaskTimestamp (): void { - if (!this.isMain && this.activeInterval != null) { + if (this.activeInterval != null) { this.lastTaskTimestamp = performance.now() } } diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index 0ccc0013..08eb4e89 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -43,7 +43,7 @@ export class ClusterWorker< /** @inheritDoc */ protected handleReadyMessage (message: MessageValue): void { - if (!this.isMain && message.workerId === this.id && message.ready != null) { + if (message.workerId === this.id && message.ready != null) { this.getMainWorker()?.on('message', this.messageListener.bind(this)) this.sendToMainWorker({ ready: true, workerId: this.id }) } diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index 4666fbc7..e208eb40 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -53,7 +53,6 @@ export class ThreadWorker< /** @inheritDoc */ protected handleReadyMessage (message: MessageValue): void { if ( - !this.isMain && message.workerId === this.id && message.ready != null && message.port != null -- 2.34.1