1
+ /*
2
+ * start-stop-relative.js: start or stop forever using relative paths, the script path could be start with './', '../' ...
3
+ *
4
+ * (C) 2010 Charlie Robbins & the Contributors
5
+ * MIT LICENCE
6
+ *
7
+ */
8
+
9
+ var assert = require ( 'assert' ) ,
10
+ path = require ( 'path' ) ,
11
+ fs = require ( 'fs' ) ,
12
+ spawn = require ( 'child_process' ) . spawn ,
13
+ vows = require ( 'vows' ) ,
14
+ forever = require ( '../../lib/forever' ) ;
15
+
16
+ function runCmd ( cmd , args ) {
17
+ var proc = spawn ( process . execPath , [
18
+ path . resolve ( __dirname , '../../' , 'bin/forever' ) ,
19
+ cmd
20
+ ] . concat ( args ) , { detached : true } ) ;
21
+ proc . unref ( ) ;
22
+ return proc ;
23
+ }
24
+
25
+ vows . describe ( 'forever/core/stopbypid-peaceful' ) . addBatch ( {
26
+ "When using forever" : {
27
+ "to run script with 100% exit" : {
28
+ topic : function ( ) {
29
+ runCmd ( 'start' , [
30
+ './test/fixtures/cluster-fork-mode.js'
31
+ ] ) ;
32
+ setTimeout ( function ( that ) {
33
+ forever . list ( false , that . callback ) ;
34
+ } , 2000 , this ) ;
35
+ } ,
36
+ "the script should be marked as `STOPPED`" : function ( err , procs ) {
37
+ assert . isNull ( err ) ;
38
+ assert . isArray ( procs ) ;
39
+ assert . equal ( procs . length , 1 ) ;
40
+ assert . ok ( ! procs [ 0 ] . running ) ;
41
+ }
42
+ }
43
+ }
44
+ } ) . addBatch ( {
45
+ "When the script is running" : {
46
+ "try to stop by pid" : {
47
+ topic : function ( ) {
48
+ var that = this ;
49
+ forever . list ( false , function ( err , procs ) {
50
+ assert . isNull ( err ) ;
51
+ assert . isArray ( procs ) ;
52
+ assert . equal ( procs . length , 1 ) ;
53
+ // get pid.
54
+ var pid = procs [ 0 ] . pid ;
55
+ // run command
56
+ var cmd = runCmd ( 'stop' , [ pid ] ) ;
57
+ cmd . stdout . on ( 'data' , onData ) ;
58
+ //listen on the `data` event.
59
+ function onData ( data ) {
60
+ // check whether pid exists or not.
61
+ var line = data . toString ( ) . replace ( / [ \n \r \t \s ] + / g, ' ' ) ;
62
+ if ( line && line . search ( new RegExp ( pid ) ) > 0 ) {
63
+ that . callback ( null , true ) ;
64
+ cmd . stdout . removeListener ( 'data' , onData ) ;
65
+ }
66
+ // if pid did not exist, that means CLI has crashed, and no output was printed.
67
+ // vows will raise an Asynchronous Error.
68
+ }
69
+ } ) ;
70
+ } ,
71
+ "the shut down should works fine" : function ( err , peaceful ) {
72
+ assert . isNull ( err ) ;
73
+ assert . ok ( peaceful ) ;
74
+ }
75
+ }
76
+ }
77
+ } ) . export ( module ) ;
0 commit comments