From: Jérôme Benoit Date: Wed, 19 Jul 2023 10:26:54 +0000 (+0200) Subject: refactor: add sanity checks to worker code X-Git-Tag: v2.6.18~4 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=209d7d1586c12f0c0e6faf58eb51b262464ecc60;p=poolifier.git refactor: add sanity checks to worker code Signed-off-by: Jérôme Benoit --- 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)