From 209d7d1586c12f0c0e6faf58eb51b262464ecc60 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 19 Jul 2023 12:26:54 +0200 Subject: [PATCH] refactor: add sanity checks to worker code 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 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 2f4212f9..a431f5a7 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -299,13 +299,15 @@ export abstract class AbstractWorker< this.statistics = message.statistics } else if (message.checkActive != null) { // Check active message received - message.checkActive ? this.startCheckActive() : this.stopCheckActive() + !this.isMain && message.checkActive + ? this.startCheckActive() + : this.stopCheckActive() } else if (message.id != null && message.data != null) { // Task message received this.run(message) } else if (message.kill === true) { // Kill message received - this.stopCheckActive() + !this.isMain && this.stopCheckActive() this.emitDestroy() } } @@ -389,6 +391,9 @@ 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) -- 2.34.1