@@ -3,12 +3,11 @@ var childProcess = require('child_process'),
33 path = require ( 'path' ) ,
44 running = require ( 'is-running' ) ,
55 LocalBinary = require ( './LocalBinary' ) ,
6- LocalError = require ( './LocalError' ) ,
7- psTree = require ( 'ps-tree' ) ;
6+ LocalError = require ( './LocalError' ) ;
7+
88
99function Local ( ) {
1010 this . pid = undefined ;
11- this . isProcessRunning = false ;
1211 this . retriesLeft = 5 ;
1312 this . key = process . env . BROWSERSTACK_ACCESS_KEY ;
1413 this . logfile = path . join ( process . cwd ( ) , 'local.log' ) ;
@@ -58,20 +57,56 @@ function Local(){
5857 callback ( new LocalError ( data [ 'message' ] [ 'message' ] ) ) ;
5958 } else {
6059 that . pid = data [ 'pid' ] ;
61- that . isProcessRunning = true ;
6260 callback ( ) ;
6361 }
6462 } ) ;
63+
64+ // that.tunnel = childProcess.spawn(binaryPath, that.getBinaryArgs());
65+ // that.tunnel.on('exit', function(){
66+ // that.tunnel = undefined;
67+ // if(that.exitCallback) that.exitCallback();
68+ // });
69+
70+ // that.stdout = fs.openSync(that.logfile, 'r');
71+ // var chunkSize = 512,
72+ // buffer = new Buffer(81920),
73+ // bytesRead = 0,
74+ // error = undefined;
75+
76+ // while(true){
77+ // var bytes = fs.readSync(that.stdout, buffer, bytesRead, chunkSize, bytesRead);
78+ // if(bytes == 0) continue;
79+
80+ // var buffRead = buffer.slice(bytesRead, bytesRead+bytes);
81+ // bytesRead += bytes;
82+
83+ // var data = buffRead.toString();
84+
85+ // if(data.match(that.errorRegex)){
86+ // fs.closeSync(that.stdout);
87+ // error = data.match(that.errorRegex)[0].trim();
88+ // break;
89+ // }
90+
91+ // if(data.match(that.doneRegex)){
92+ // fs.closeSync(that.stdout);
93+ // break;
94+ // }
95+ // }
96+
97+ // if(error) throw new LocalError(error);
98+ // callback();
6599 } ) ;
66100 } ;
67101
68102 this . isRunning = function ( ) {
69- return this . pid && running ( this . pid ) && this . isProcessRunning ;
103+ return this . pid && running ( this . pid ) ;
70104 } ;
71105
72106 this . stop = function ( callback ) {
73107 if ( ! this . pid ) return callback ( ) ;
74- this . killAllProcesses ( function ( error ) {
108+ this . opcode = 'stop' ;
109+ this . tunnel = childProcess . execFile ( this . binaryPath , this . getBinaryArgs ( ) , function ( error ) {
75110 if ( error ) callback ( new LocalError ( error . toString ( ) ) ) ;
76111 callback ( ) ;
77112 } ) ;
@@ -252,35 +287,5 @@ function Local(){
252287 }
253288 return args ;
254289 } ;
255-
256- this . killAllProcesses = function ( callback ) {
257- psTree ( this . pid , ( err , children ) => {
258- var childPids = children . map ( val => val . PID ) ;
259- var killChecker = setInterval ( ( ) => {
260- if ( childPids . length === 0 ) {
261- clearInterval ( killChecker ) ;
262- try {
263- process . kill ( this . pid ) ;
264- // This gives time to local binary to send kill signal to railsApp.
265- setTimeout ( ( ) => {
266- this . isProcessRunning = false ;
267- callback ( ) ;
268- } , 2000 ) ;
269- } catch ( err ) {
270- this . isProcessRunning = false ;
271- callback ( ) ;
272- }
273- }
274- for ( var i in childPids ) {
275- try {
276- process . kill ( childPids [ i ] ) ;
277- } catch ( err ) {
278- childPids . splice ( i , 1 ) ;
279- }
280- }
281- } , 500 ) ;
282- } ) ;
283- } ;
284290}
285-
286291module . exports = Local ;
0 commit comments