@@ -105,6 +105,7 @@ function getAllProcesses(callback) {
105
105
}
106
106
107
107
socket . dataOnce ( [ 'data' ] , function ( data ) {
108
+ data . socket = fullPath ;
108
109
next ( null , data ) ;
109
110
socket . end ( ) ;
110
111
} ) ;
@@ -146,6 +147,59 @@ function getAllPids(processes) {
146
147
} ) ;
147
148
}
148
149
150
+ function stopOrRestart ( action , event , format , target ) {
151
+ var emitter = new events . EventEmitter ( ) ,
152
+ results = [ ] ,
153
+ pids ;
154
+
155
+ function sendAction ( proc , next ) {
156
+ var socket = new nssocket . NsSocket ( ) ;
157
+
158
+ socket . connect ( proc . socket , function ( err ) {
159
+ if ( err ) {
160
+ next ( err ) ;
161
+ }
162
+
163
+ socket . dataOnce ( [ action , 'ok' ] , function ( data ) {
164
+ next ( ) ;
165
+ socket . end ( ) ;
166
+ } ) ;
167
+
168
+ socket . send ( [ action ] ) ;
169
+ } ) ;
170
+
171
+ socket . on ( 'error' , function ( err ) {
172
+ next ( err ) ;
173
+ } ) ;
174
+ }
175
+
176
+ getAllProcesses ( function ( processes ) {
177
+ var procs = processes ;
178
+
179
+ if ( target ) {
180
+ procs = forever . findByIndex ( target , processes )
181
+ || forever . findByScript ( target , processes ) ;
182
+ }
183
+
184
+ if ( procs && procs . length > 0 ) {
185
+ async . map ( procs , sendAction , function ( err , results ) {
186
+ if ( err ) {
187
+ emitter . emit ( 'error' , err ) ;
188
+ }
189
+
190
+ emitter . emit ( event , forever . format ( format , procs ) ) ;
191
+ } ) ;
192
+ }
193
+ else {
194
+ process . nextTick ( function ( ) {
195
+ emitter . emit ( 'error' , new Error ( 'Cannot find forever process: ' + target ) ) ;
196
+ } ) ;
197
+ }
198
+ } ) ;
199
+
200
+ return emitter ;
201
+ }
202
+
149
203
//
150
204
// ### function load (options, [callback])
151
205
// #### @options {Object} Options to load into the forever module
@@ -383,33 +437,7 @@ forever.startServer = function () {
383
437
// in the list of all processes
384
438
//
385
439
forever . stop = function ( target , format ) {
386
- var emitter = new events . EventEmitter ( ) ,
387
- results = [ ] ,
388
- pids ;
389
-
390
- getAllProcesses ( function ( processes ) {
391
- var procs = forever . findByIndex ( target , processes )
392
- || forever . findByScript ( target , processes ) ;
393
-
394
- if ( procs && procs . length > 0 ) {
395
- pids = procs . reduce ( function ( agg , proc ) {
396
- return agg . concat ( proc . foreverPid , proc . pid ) ;
397
- } , [ ] ) ;
398
-
399
- async . forEach ( pids , function ( pid , next ) {
400
- forever . kill ( pid , true , next ) ;
401
- } , function ( ) {
402
- emitter . emit ( 'stop' , forever . format ( format , procs ) ) ;
403
- } ) ;
404
- }
405
- else {
406
- process . nextTick ( function ( ) {
407
- emitter . emit ( 'error' , new Error ( 'Cannot find forever process: ' + target ) ) ;
408
- } ) ;
409
- }
410
- } ) ;
411
-
412
- return emitter ;
440
+ return stopOrRestart ( 'stop' , 'stop' , format , target ) ;
413
441
} ;
414
442
415
443
//
@@ -420,88 +448,7 @@ forever.stop = function (target, format) {
420
448
// in the list of all processes
421
449
//
422
450
forever . restart = function ( target , format ) {
423
- var emitter = new events . EventEmitter ( ) ,
424
- runner = forever . stop ( target , false ) ;
425
-
426
- runner . on ( 'stop' , function ( procs ) {
427
- if ( procs && procs . length > 0 ) {
428
- async . forEach ( procs , function ( proc , next ) {
429
- //
430
- // We need to spawn a new process running the forever CLI
431
- // here because we want each process to daemonize separately
432
- // without the main process running `forever restart myscript.js`
433
- // daemonizing itself.
434
- //
435
- var restartCommand = [
436
- 'forever' ,
437
- 'start' ,
438
- '--sourceDir' , proc . sourceDir ,
439
- '-l' , proc . logFile ,
440
- '--append true'
441
- ] ;
442
-
443
- if ( proc . silent ) {
444
- restartCommand . push ( '--silent true' ) ;
445
- }
446
-
447
- if ( proc . command ) {
448
- restartCommand . push ( '-c' , command ) ;
449
- }
450
-
451
- if ( proc . outFile ) {
452
- restartCommand . push ( '-o' , path . join ( proc . sourceDir , proc . outFile ) ) ;
453
- }
454
-
455
- if ( proc . errFile ) {
456
- restartCommand . push ( '-e' , path . join ( proc . sourceDir , proc . outFile ) ) ;
457
- }
458
-
459
- restartCommand . push ( proc . file , proc . options . join ( ' ' ) ) ;
460
- forever . log . silly ( 'Restarting with options' , { options : restartCommand . join ( ' ' ) } ) ;
461
-
462
- exec ( restartCommand . join ( ' ' ) , proc . spawnWith , function ( err , stdout , stderr ) {
463
- next ( ) ;
464
- } ) ;
465
- } , function ( ) {
466
- emitter . emit ( 'restart' , forever . format ( format , procs ) ) ;
467
- } ) ;
468
- }
469
- else {
470
- emitter . emit ( 'error' , new Error ( 'Cannot find forever process: ' + target ) ) ;
471
- }
472
- } ) ;
473
-
474
- // Bubble up the error to the appropriate EventEmitter instance.
475
- runner . on ( 'error' , function ( err ) {
476
- emitter . emit ( 'error' , err ) ;
477
- } ) ;
478
-
479
- return emitter ;
480
- } ;
481
-
482
- //
483
- // ### function findByIndex (index, processes)
484
- // #### @index {string} Index of the process to find.
485
- // #### @processes {Array} Set of processes to find in.
486
- // Finds the process with the specified index.
487
- //
488
- forever . findByIndex = function ( index , processes ) {
489
- var proc = processes && processes [ parseInt ( index , 10 ) ] ;
490
- return proc ? [ proc ] : null ;
491
- } ;
492
-
493
- //
494
- // ### function findByScript (script, processes)
495
- // #### @script {string} The name of the script to find.
496
- // #### @processes {Array} Set of processes to find in.
497
- // Finds the process with the specified script name.
498
- //
499
- forever . findByScript = function ( script , processes ) {
500
- return ! processes
501
- ? null
502
- : processes . filter ( function ( p ) {
503
- return p . file === script ;
504
- } ) ;
451
+ return stopOrRestart ( 'restart' , 'restart' , format , target ) ;
505
452
} ;
506
453
507
454
//
@@ -510,36 +457,7 @@ forever.findByScript = function (script, processes) {
510
457
// Stops all processes managed by forever.
511
458
//
512
459
forever . stopAll = function ( format ) {
513
- var emitter = new events . EventEmitter ( ) ;
514
-
515
- getAllProcesses ( function ( processes ) {
516
- var pids = getAllPids ( processes ) ;
517
-
518
- if ( format ) {
519
- processes = forever . format ( format , processes ) ;
520
- }
521
-
522
- if ( pids && processes ) {
523
- pids = pids . reduce ( function ( agg , proc ) {
524
- return agg . concat ( proc . foreverPid , proc . pid ) ;
525
- } , [ ] ) ;
526
-
527
- async . forEach ( pids , function ( pid , next ) {
528
- if ( pid !== process . pid ) {
529
- forever . kill ( pid , true , next ) ;
530
- }
531
- } , function ( ) {
532
- emitter . emit ( 'stopAll' , processes ) ;
533
- } ) ;
534
- }
535
- else {
536
- process . nextTick ( function ( ) {
537
- emitter . emit ( 'stopAll' , null ) ;
538
- } ) ;
539
- }
540
- } ) ;
541
-
542
- return emitter ;
460
+ return stopOrRestart ( 'stop' , 'stopAll' , format ) ;
543
461
} ;
544
462
545
463
//
@@ -603,6 +521,31 @@ forever.tail = function (target, length, callback) {
603
521
} ) ;
604
522
} ;
605
523
524
+ //
525
+ // ### function findByIndex (index, processes)
526
+ // #### @index {string} Index of the process to find.
527
+ // #### @processes {Array} Set of processes to find in.
528
+ // Finds the process with the specified index.
529
+ //
530
+ forever . findByIndex = function ( index , processes ) {
531
+ var proc = processes && processes [ parseInt ( index , 10 ) ] ;
532
+ return proc ? [ proc ] : null ;
533
+ } ;
534
+
535
+ //
536
+ // ### function findByScript (script, processes)
537
+ // #### @script {string} The name of the script to find.
538
+ // #### @processes {Array} Set of processes to find in.
539
+ // Finds the process with the specified script name.
540
+ //
541
+ forever . findByScript = function ( script , processes ) {
542
+ return ! processes
543
+ ? null
544
+ : processes . filter ( function ( p ) {
545
+ return p . file === script ;
546
+ } ) ;
547
+ } ;
548
+
606
549
//
607
550
// ### function format (format, procs)
608
551
// #### @format {Boolean} Value indicating if processes should be formatted
0 commit comments