Skip to content

Commit

Permalink
[api test doc] Expose .fork() through forever for node-specific pro…
Browse files Browse the repository at this point in the history
…cesses. Currently blocked by nodejs/node-v0.x-archive#2454
  • Loading branch information
indexzero committed Jan 5, 2012
1 parent 1f78240 commit 7aa72c9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions examples/process-send.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

setInterval(function () {
if (process.send) {
process.send({ from: 'child' });
}
}, 1000)
21 changes: 20 additions & 1 deletion lib/forever/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
var events = require('events'),
fs = require('fs'),
path = require('path'),
fork = require('child_process').fork,
spawn = require('child_process').spawn,
broadway = require('broadway'),
psTree = require('ps-tree'),
Expand Down Expand Up @@ -156,9 +157,17 @@ Monitor.prototype.start = function (restart) {
self.emit(restart ? 'restart' : 'start', self, self.data);
});

function reemit() {
self.emit.apply(self, arguments);
}

// Re-emit messages from the child process
this.child.on('message', reemit);

child.on('exit', function (code) {
var spinning = Date.now() - self.ctime < self.minUptime;
self.warn('Forever detected script exited with code: ' + code);
child.removeListener('message', reemit);

function letChildDie() {
self.running = false;
Expand Down Expand Up @@ -200,6 +209,9 @@ Monitor.prototype.start = function (restart) {
// trying to execute a script with an env: e.g. node myfile.js
//
Monitor.prototype.trySpawn = function () {
var execPath = process.execPath,
forked;

if (this.command === 'node' || (this.checkFile && !this.childExists)) {
try {
var stats = fs.statSync(this.args[0]);
Expand All @@ -213,7 +225,14 @@ Monitor.prototype.trySpawn = function () {
this.spawnWith.cwd = this.cwd || this.spawnWith.cwd;
this.spawnWith.env = this._getEnv();

return spawn(this.command, this.args, this.spawnWith);
if (this.command === 'node' || this.fork) {
process.execPath = this.command;
forked = fork(this.options[0], this.options.slice(1), this.spawnWith);
process.execPath = execPath;
return forked;
}

return spawn(this.command, this.options, this.spawnWith);
};

//
Expand Down
29 changes: 29 additions & 0 deletions test/fork-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* spin-test.js: Tests for spin restarts in forever.
*
* (C) 2010 Nodejitsu Inc.
* MIT LICENCE
*
*/

var assert = require('assert'),
path = require('path'),
vows = require('vows'),
forever = require('../lib/forever');

vows.describe('forever/spin-restart').addBatch({
"When using forever": {
"and spawning a script that uses `process.send()`": {
topic: function () {
var script = path.join(__dirname, '..', 'examples', 'process-send.js'),
child = new (forever.Monitor)(script, { silent: true, minUptime: 2000, max: 1 });

child.on('message', this.callback.bind(null, null));
child.start();
},
"should reemit the message correctly": function (err, child, spinning) {
console.dir(arguments);
}
}
}
}).export(module);

0 comments on commit 7aa72c9

Please sign in to comment.