1+ /* global config, threads */
12require ( 'atma-libs/globals' ) ;
23process . on ( 'exit' , shutdownSelf ) ;
34process . on ( 'SIGINT' , shutdownSelf ) ;
@@ -55,15 +56,14 @@ function server_Start(){
5556 startWorker ( workerStarted ) ;
5657 }
5758 function workerStarted ( error ) {
58- if ( error )
59+ if ( error )
5960 send ( error ) ;
60- if ( -- count > - 1 )
61+
62+ if ( -- count > 0 )
6163 return ;
6264
6365 config
64- . $write ( {
65- pid : config . pid
66- } )
66+ . $write ( { pid : config . pid } , false )
6767 . fail ( disconnect )
6868 . done ( function ( ) {
6969 var msg = logger . formatMessage (
@@ -72,7 +72,7 @@ function server_Start(){
7272 ) ;
7373 config . pid . workers . forEach ( function ( pid , index ) {
7474 msg += logger . formatMessage (
75- 'Worker green<%s> process: bold<%s>' . color
75+ '\nWorker green<%s> process: bold<%s>' . color
7676 , index
7777 , pid
7878 ) ;
@@ -87,17 +87,19 @@ function server_Start(){
8787}
8888function startServer ( cb ) {
8989 fork ( 'App/index.js' , function ( error , thread ) {
90- if ( error ) return cb ( error ) ;
90+ if ( thread )
91+ config . $set ( 'pid.server' , thread . child . pid ) ;
9192
92- config . $set ( 'pid.server' , thread . child . pid ) ;
93+ if ( error ) return cb ( error ) ;
9394 cb ( ) ;
9495 } ) ;
9596}
9697function startWorker ( cb ) {
9798 fork ( 'Worker/index.js' , function ( error , thread ) {
99+ if ( thread )
100+ config . $get ( 'pid.workers' ) . push ( thread . child . pid ) ;
101+
98102 if ( error ) return cb ( error ) ;
99-
100- config . $get ( 'pid.workers' ) . push ( thread . child . pid ) ;
101103 cb ( ) ;
102104 } ) ;
103105}
@@ -133,15 +135,21 @@ function fork(path, cb){
133135 silent : true ,
134136 killSignal : 'SIGINT' ,
135137 killTree : false ,
138+ detached : true ,
136139 spawnWith : {
137- detached : true
138- }
140+ detached : true ,
141+ stdio : [ 'ipc' ] ,
142+ } ,
143+ options : [
144+ '--release'
145+ ]
139146 } ) ;
140147 threads . push ( thread ) ;
141148 thread
142149 . on ( 'start' , function ( ) {
143-
144- cb ( null , thread ) ;
150+ //thread.child.on('message', function(msg){
151+ // ...
152+ //});
145153 } )
146154 . on ( 'stop' , function ( error ) {
147155 send ( 'stopped' . bold ) ;
@@ -154,15 +162,19 @@ function fork(path, cb){
154162 . on ( 'restart' , function ( a , b ) {
155163 send ( 'Failed to start: ' + path ) ;
156164 } )
157- . on ( 'exit:code' , function ( code , x ) {
158- //send('Forever detected script exited with code ' + code);
165+ . on ( 'message' , function ( message ) {
166+ if ( message === 'ok' ) {
167+ cb ( null , thread ) ;
168+ return ;
169+ }
159170
171+ cb ( message , thread ) ;
160172 } )
161173 . on ( 'stdout' , function ( data ) {
162- send ( path + ' [stdout]: ' + String ( data ) ) ;
174+ send ( ' [stdout]: ' + path + String ( data ) ) ;
163175 } )
164176 . on ( 'stderr' , function ( data ) {
165- send ( path + ' [stderr]: ' + String ( data ) ) ;
177+ send ( ' [stderr]: ' + path + String ( data ) ) ;
166178 } )
167179 ;
168180
@@ -179,6 +191,13 @@ function send() {
179191 console . log ( msg ) ;
180192}
181193function disconnect ( error ) {
194+
195+ threads . forEach ( function ( thread ) {
196+ thread
197+ . removeAllListeners ( 'stdout' )
198+ . removeAllListeners ( 'stderr' ) ;
199+ } ) ;
200+
182201 if ( error == null ) {
183202 send ( 'ok' ) ;
184203 return ;
0 commit comments