feat: add pool and worker readyness tracking infrastructure
[poolifier.git] / src / worker / abstract-worker.ts
index 92d68bcb6e9d4f38fdb3cc87170a038c4f226722..4e92be01ae340da26d4699595ff692566075f006 100644 (file)
@@ -145,7 +145,10 @@ export abstract class AbstractWorker<
    * @param message - Message received.
    */
   protected messageListener (message: MessageValue<Data, Data>): void {
-    if (message.statistics != null) {
+    if (message.ready != null && message.workerId === this.id) {
+      // Startup message received
+      this.workerReady()
+    } else if (message.statistics != null) {
       // Statistics message received
       this.statistics = message.statistics
     } else if (message.checkAlive != null) {
@@ -166,6 +169,13 @@ export abstract class AbstractWorker<
     }
   }
 
+  /**
+   * Notifies the main worker that this worker is ready to process tasks.
+   */
+  protected workerReady (): void {
+    !this.isMain && this.sendToMainWorker({ ready: true, workerId: this.id })
+  }
+
   /**
    * Starts the worker alive check interval.
    */