9
9
var events = require ( 'events' ) ,
10
10
fs = require ( 'fs' ) ,
11
11
path = require ( 'path' ) ,
12
+ fork = require ( 'child_process' ) . fork ,
12
13
spawn = require ( 'child_process' ) . spawn ,
13
14
broadway = require ( 'broadway' ) ,
14
15
psTree = require ( 'ps-tree' ) ,
@@ -156,9 +157,17 @@ Monitor.prototype.start = function (restart) {
156
157
self . emit ( restart ? 'restart' : 'start' , self , self . data ) ;
157
158
} ) ;
158
159
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
+
159
167
child . on ( 'exit' , function ( code ) {
160
168
var spinning = Date . now ( ) - self . ctime < self . minUptime ;
161
169
self . warn ( 'Forever detected script exited with code: ' + code ) ;
170
+ child . removeListener ( 'message' , reemit ) ;
162
171
163
172
function letChildDie ( ) {
164
173
self . running = false ;
@@ -200,6 +209,9 @@ Monitor.prototype.start = function (restart) {
200
209
// trying to execute a script with an env: e.g. node myfile.js
201
210
//
202
211
Monitor . prototype . trySpawn = function ( ) {
212
+ var execPath = process . execPath ,
213
+ forked ;
214
+
203
215
if ( this . command === 'node' || ( this . checkFile && ! this . childExists ) ) {
204
216
try {
205
217
var stats = fs . statSync ( this . args [ 0 ] ) ;
@@ -213,7 +225,14 @@ Monitor.prototype.trySpawn = function () {
213
225
this . spawnWith . cwd = this . cwd || this . spawnWith . cwd ;
214
226
this . spawnWith . env = this . _getEnv ( ) ;
215
227
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 ) ;
217
236
} ;
218
237
219
238
//
0 commit comments