Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

demo works

  • Loading branch information...
commit de17f1d95101fb6a28636cb84587944d09551def 1 parent 3870adb
Joachim Kainz authored
View
1  .gitignore
@@ -0,0 +1 @@
+node_modules
View
3  examples/master.js
@@ -7,4 +7,5 @@ worker.on("message", function(message) {
worker.send({
"title": "hello world!",
"flag": true
-});
+});
+worker.terminate();
View
49 lib/backgrounder-launcher.js
@@ -1,5 +1,6 @@
var util = require('util');
var events = require("events");
+var _ = require("underscore");
function format(f) {
var i;
@@ -32,9 +33,14 @@ function format(f) {
str += ' ' + util.inspect(x);
}
}
+
return str;
}
+process.on('uncaughtException', function(err) {
+ console.log(err.stack);
+});
+
function log(message) {
var _message = {
"type": "console",
@@ -61,18 +67,53 @@ console.timeEnd = function(label) {
};
var buffer = "";
var emitter = new events.EventEmitter();
+
+function processMessages(messages) {
+ _.each(messages, function(message){
+ if (message.length > 0) {
+ var parsed = JSON.parse(message);
+
+ if ("message" === parsed.type) {
+ emitter.emit("message", parsed.content);
+ }
+ else if ("terminate" === parsed.type) {
+ process.exit(0);
+ }
+ else {
+ console.error("unsupported message ", parsed);
+ }
+ }
+ });
+}
+function processBuffer() {
+ var messages = buffer.split('\n');
+ buffer = "";
+ try {
+ processMessages(messages);
+ }
+ finally {
+ var json = JSON.stringify({
+ "type": "idle"
+ });
+ process.stdout.write(json + '\n');
+ }
+}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
buffer += data.toString();
if (buffer.substr(-1) === '\n') {
- var parsed = JSON.parse(buffer);
- buffer = "";
- emitter.emit("message", parsed);
+ processBuffer();
}
});
+var process_on = process.on;
process.on = function(message, listener) {
- emitter.on(message, listener);
+ if ("message" === message) {
+ emitter.on(message, listener);
+ return;
+ }
+
+ process_on(message, listener);
};
process.send = function(message) {
var json = JSON.stringify({
View
44 lib/backgrounder.js
@@ -3,34 +3,43 @@ var cp = require("child_process");
var events = require("events");
var util = require("util");
-function processMessage(emitter, message) {
+function processMessage(child, message) {
if (message.type === 'console') {
console.log(message.content);
}
else if (message.type === 'message') {
- emitter.emit('message', message.content);
+ child.emitter.emit('message', message.content);
+ }
+ else if (message.type === 'idle') {
+ child.emitter.emit('idle');
+ child.busy = true;
}
else {
console.error("unexpected message %s", util.inspect(message));
}
}
+function processBuffer(self) {
+ var messages = self.buffer.split('\n');
+ self.buffer = "";
+ _.each(messages, function(message){
+ if (message.length > 0) {
+ var parsed = JSON.parse(message);
+ processMessage(self, parsed);
+ }
+ });
+}
+
function Child(mdl, module) {
var self = this;
+ this.busy = false;
this.buffer = "";
this.emitter = new events.EventEmitter();
this.process = cp.spawn('node', [mdl, module]);
this.process.stdout.on('data', function(data) {
self.buffer += data.toString();
if (self.buffer.substr(-1) === '\n') {
- var messages = self.buffer.split('\n');
- self.buffer = "";
- _.each(messages, function(message){
- if (message.length > 0) {
- var parsed = JSON.parse(message);
- processMessage(self.emitter, parsed);
- }
- });
+ processBuffer(self);
}
});
this.process.stderr.on('data', function(data) {
@@ -45,10 +54,21 @@ Child.prototype.on = function(event, listener) {
this.emitter.on(event, listener);
};
Child.prototype.send = function(message) {
- var json = JSON.stringify(message);
+ var json = JSON.stringify({
+ "type": "message",
+ "content": message
+ });
+ this.busy = true;
+ this.process.stdin.write(json + '\n');
+};
+Child.prototype.terminate = function() {
+ var json = JSON.stringify({
+ "type": "terminate"
+ });
+ this.busy = true;
this.process.stdin.write(json + '\n');
};
module.exports.spawn = function(module) {
var mdl = __dirname + '/backgrounder-launcher.js';
return new Child(mdl, module);
-};
+};
View
3  package.json
@@ -16,5 +16,8 @@
},
"engines": {
"node": ">= 0.4.9"
+ },
+ "dependencies": {
+ "underscore": ">= 1.1.7"
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.