Skip to content

Commit 7aa72c9

Browse files
committed
[api test doc] Expose .fork() through forever for node-specific processes. Currently blocked by nodejs/node-v0.x-archive#2454
1 parent 1f78240 commit 7aa72c9

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

examples/process-send.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
setInterval(function () {
3+
if (process.send) {
4+
process.send({ from: 'child' });
5+
}
6+
}, 1000)

lib/forever/monitor.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
var events = require('events'),
1010
fs = require('fs'),
1111
path = require('path'),
12+
fork = require('child_process').fork,
1213
spawn = require('child_process').spawn,
1314
broadway = require('broadway'),
1415
psTree = require('ps-tree'),
@@ -156,9 +157,17 @@ Monitor.prototype.start = function (restart) {
156157
self.emit(restart ? 'restart' : 'start', self, self.data);
157158
});
158159

160+
function reemit() {
161+
self.emit.apply(self, arguments);
162+
}
163+
164+
// Re-emit messages from the child process
165+
this.child.on('message', reemit);
166+
159167
child.on('exit', function (code) {
160168
var spinning = Date.now() - self.ctime < self.minUptime;
161169
self.warn('Forever detected script exited with code: ' + code);
170+
child.removeListener('message', reemit);
162171

163172
function letChildDie() {
164173
self.running = false;
@@ -200,6 +209,9 @@ Monitor.prototype.start = function (restart) {
200209
// trying to execute a script with an env: e.g. node myfile.js
201210
//
202211
Monitor.prototype.trySpawn = function () {
212+
var execPath = process.execPath,
213+
forked;
214+
203215
if (this.command === 'node' || (this.checkFile && !this.childExists)) {
204216
try {
205217
var stats = fs.statSync(this.args[0]);
@@ -213,7 +225,14 @@ Monitor.prototype.trySpawn = function () {
213225
this.spawnWith.cwd = this.cwd || this.spawnWith.cwd;
214226
this.spawnWith.env = this._getEnv();
215227

216-
return spawn(this.command, this.args, this.spawnWith);
228+
if (this.command === 'node' || this.fork) {
229+
process.execPath = this.command;
230+
forked = fork(this.options[0], this.options.slice(1), this.spawnWith);
231+
process.execPath = execPath;
232+
return forked;
233+
}
234+
235+
return spawn(this.command, this.options, this.spawnWith);
217236
};
218237

219238
//

test/fork-test.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* spin-test.js: Tests for spin restarts in forever.
3+
*
4+
* (C) 2010 Nodejitsu Inc.
5+
* MIT LICENCE
6+
*
7+
*/
8+
9+
var assert = require('assert'),
10+
path = require('path'),
11+
vows = require('vows'),
12+
forever = require('../lib/forever');
13+
14+
vows.describe('forever/spin-restart').addBatch({
15+
"When using forever": {
16+
"and spawning a script that uses `process.send()`": {
17+
topic: function () {
18+
var script = path.join(__dirname, '..', 'examples', 'process-send.js'),
19+
child = new (forever.Monitor)(script, { silent: true, minUptime: 2000, max: 1 });
20+
21+
child.on('message', this.callback.bind(null, null));
22+
child.start();
23+
},
24+
"should reemit the message correctly": function (err, child, spinning) {
25+
console.dir(arguments);
26+
}
27+
}
28+
}
29+
}).export(module);

0 commit comments

Comments
 (0)