pgriess / node-webworker

A WebWorkers implementation for NodeJS
BSD 3-Clause "New" or "Revised" License
646 stars 84 forks source link

Notify master when child worker exits #3

Closed pgriess closed 14 years ago

pgriess commented 14 years ago

This event should probably include the exit code and signal.

lgreenspan commented 14 years ago

I am currently experimenting with this implementation:

diff --git a/lib/webworker.js b/lib/webworker.js
index 8b27f1d..df7dd46 100644
--- a/lib/webworker.js
+++ b/lib/webworker.js
@@ -176,6 +176,8 @@ var WorkerImpl = function(w, src) {
     // talk to the worker. It contains [type, data, fd] tuples.
     var msgQueue = [];

+    var exitListener = undefined;
+
     // Begin worker execution
     //
     // Spawns the child process and away we go.
@@ -219,7 +221,10 @@ var WorkerImpl = function(w, src) {
                 );
             }

-            MASTER.maybeDestroy();
+            //MASTER.maybeDestroy();
+
+            if (exitListener)
+                exitListener(pid, code, signal);
         });

         MASTER.addWorker(pid, self);
@@ -342,6 +347,10 @@ var WorkerImpl = function(w, src) {
             cp.kill('SIGTERM');
         }, timeout);
     };
+
+    self.setExitListener = function (callback) {
+        exitListener = callback;
+    };
 };

 // Implementation of the Worker API; externally visible to library users.
@@ -362,6 +371,10 @@ var Worker = function(src) {
         impl.terminate(5000);
     };

+    self.setExitListener = function (callback) {
+        impl.setExitListener(callback);
+    }
+
     impl.start();
 };

Problem is, that MASTER.maybeDestroy() is problematic with this approach, hence I commented it out. If all children die and the listener restarts workers, the master might be down and the error is:

events:11
        throw arguments[1];
                       ^
Error: ENOENT, No such file or directory
    at doConnect (net:840:19)
    at Stream.connect (net:913:30)
    at Object.createConnection (net:619:5)
    at Object.<anonymous> (/usr/local/share/node/webworker-child.js:106:13)
    at Module._compile (module:384:23)
    at Module._loadScriptSync (module:393:8)
    at Module.loadSync (module:296:10)
    at Object.runMain (module:447:22)
    at node.js:208:10

Do you already know how you want to implement support for this?

pgriess commented 14 years ago

Fixed with 7a55ee325f090efe8a0521fb4fd48c310b7dd18d.