Skip to content
Browse files

using a callback to make sure that is initialized.

  • Loading branch information...
1 parent 4b7bb14 commit aeb914e9f8f06f75f912182607ccd49e722edf63 Joachim Kainz committed Oct 2, 2011
Showing with 86 additions and 83 deletions.
  1. +46 −50 examples/master.js
  2. +8 −6 examples/simple-master.js
  3. +2 −1 examples/worker.js
  4. +30 −26 lib/backgrounder.js
View
96 examples/master.js
@@ -2,57 +2,53 @@ var backgrounder = require("../lib/backgrounder");
//
// Spawn the worker in a backround proccess
//
-var worker = backgrounder.spawn(__dirname + "/worker.js");
-//
-// For this demo, let's just print any message we are receiving from the worker
-//
-worker.on("message", function(message) {
- console.log("Master: received message ", message);
-});
-//
-// A little extension to the v0.5 API enables callbacks
-//
-function sendAndTerminteOnCallback() {
- worker.send({
- "company": "jolira"
- }, function(arg1, arg2, arg3) {
- console.log("Master: callback received with %s arguments:", arguments.length, arg1, arg2, arg3);
- console.log("Master: calling terminate now");
- worker.terminate();
- });
-}
-//
-// Process messages that indicate that the workder has become idle. Both worker.config as well as
-// worker.send messages result in "idle" messages from the worker when the processing of the messages
-// is complete.
-//
-var counter = 0;
-worker.on("idle", function(message) {
- switch (counter ++) {
- case 0:
- console.log("Master: worker idle after configuration");
- //
- // Send a message to the
- //
- worker.send({
- "title": "hello world!",
- "flag": true
- });
- break;
- case 1:
- sendAndTerminteOnCallback();
- break;
- case 2:
- break;
- default:
- console.error("Master: unexpected idle message ", counter, message);
+var worker = backgrounder.spawn(__dirname + "/worker.js", function(worker){
+ //
+ // For this demo, let's just print any message we are receiving from the worker
+ //
+ worker.on("message", function(message) {
+ console.log("Master: received message ", message);
+ });
+ //
+ // A little extension to the v0.5 API enables callbacks
+ //
+ function sendAndTerminteOnCallback() {
+ worker.send({
+ "company": "jolira"
+ }, function(arg1, arg2, arg3) {
+ console.log("Master: callback received with %s arguments:", arguments.length, arg1, arg2, arg3);
+ console.log("Master: calling terminate now");
+ worker.terminate();
+ });
}
-});
-//
-// This is optional, but some workers need to be configured. This call sends a message, which triggers
-// a "config" event in the client.
-//
-worker.config({
+ //
+ // Process messages that indicate that the workder has become idle. Both worker.config as well as
+ // worker.send messages result in "idle" messages from the worker when the processing of the messages
+ // is complete.
+ //
+ var counter = 0;
+ worker.on("idle", function(message) {
+ switch (counter ++) {
+ case 0:
+ console.log("Master: worker idle after configuration");
+ //
+ // Send a message to the
+ //
+ worker.send({
+ "title": "hello world!",
+ "flag": true
+ });
+ break;
+ case 1:
+ sendAndTerminteOnCallback();
+ break;
+ case 2:
+ break;
+ default:
+ console.error("Master: unexpected idle message ", counter, message);
+ }
+ });
+}, {
"primaryDirective": "don't interfere",
"overdrive": true
});
View
14 examples/simple-master.js
@@ -1,9 +1,11 @@
var backgrounder = require("../lib/backgrounder");
-var worker = backgrounder.spawn(__dirname + "/simple-worker.js");
-worker.send({
- "company": "jolira"
- }, function(arg1, arg2, arg3) {
- console.log("Master: client called the callback with %s arguments:", arguments.length, arg1, arg2, arg3);
- worker.terminate();
+backgrounder.spawn(__dirname + "/simple-worker.js", function(worker){
+ worker.send({
+ "company": "jolira"
+ }, function(arg1, arg2, arg3) {
+ console.log("Master: client called the callback with %s arguments:",
+ arguments.length, arg1, arg2, arg3);
+ worker.terminate();
+ });
});
View
3 examples/worker.js
@@ -1,8 +1,9 @@
//
// Deal with configuration messages sent from the master
//
-process.on('config', function(config) {
+process.on('config', function(config, callback) {
console.log('Worker: received configuration ', config);
+ callback();
});
//
// Set up a message handler from messages sent from the master
View
56 lib/backgrounder.js
@@ -45,6 +45,21 @@ function processMessage(child, message) {
}
}
//
+// Send a message to the client (do the actual work: stringify, write to stdin, increment the pending counter)
+//
+function sendMessage(child, message, callback) {
+ if (callback) {
+ var id = ++ child.requestCount;
+ child.callbacks[id] = callback;
+ message.id = id;
+ }
+
+ var json = JSON.stringify(message);
+
+ child.pending ++;
+ child.process.stdin.write(json + '\n');
+};
+//
// Process the content of self.buffer. Find all message in the buffer and process the messages.
//
function processBuffer(self) {
@@ -91,21 +106,6 @@ Child.prototype.on = function(event, listener) {
this.emitter.on(event, listener);
};
//
-// Send a message to the client (do the actual work: stringify, write to stdin, increment the pending counter)
-//
-function sendMessage(child, message, callback) {
- if (callback) {
- var id = ++ child.requestCount;
- child.callbacks[id] = callback;
- message.id = id;
- }
-
- var json = JSON.stringify(message);
-
- child.pending ++;
- child.process.stdin.write(json + '\n');
-};
-//
// Send a user-defined message to the client
//
Child.prototype.send = function(message, callback) {
@@ -115,15 +115,6 @@ Child.prototype.send = function(message, callback) {
}, callback);
};
//
-// Tell the client to (re-)configure itself
-//
-Child.prototype.config = function(config, callback) {
- sendMessage(this, {
- "type": "config",
- "content": config
- }, callback);
-};
-//
// Tell the client to shut down
//
Child.prototype.terminate = function() {
@@ -132,6 +123,19 @@ Child.prototype.terminate = function() {
});
};
// export the spwan method, which creates the client object.
-module.exports.spawn = function(module) {
- return new Child(module);
+module.exports.spawn = function(module, callback, config) {
+ var child = new Child(module);
+
+ if (!config) {
+ callback(child);
+ return;
+ }
+
+ sendMessage(child, {
+ "type": "config",
+ "content": config
+ }, function(){
+ callback(child);
+
+ });
};

0 comments on commit aeb914e

Please sign in to comment.
Something went wrong with that request. Please try again.