Skip to content

Commit e72da4d

Browse files
committed
Merge pull request browserstack#9 from browserstack/daemon
Daemon
2 parents 6b99643 + d0a0c8c commit e72da4d

File tree

2 files changed

+62
-75
lines changed

2 files changed

+62
-75
lines changed

lib/Local.php

Lines changed: 47 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
class Local {
1212

13-
private $handle = NULL;
14-
private $pipes = array();
15-
private $loghandle = NULL;
1613
public $pid = NULL;
1714

1815
public function __construct() {
@@ -25,11 +22,30 @@ public function __destruct() {
2522
}
2623

2724
public function isRunning() {
28-
if (is_null($this->handle))
25+
if ($this->pid == NULL)
2926
return False;
30-
31-
$status = proc_get_status($this->handle);
32-
return is_null($status["running"]) ? false : $status["running"];
27+
if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
28+
{
29+
$processes = explode( "\n", shell_exec( "tasklist.exe" ));
30+
foreach( $processes as $process )
31+
{
32+
if( strpos( "Image Name", $process ) === 0 || strpos( "===", $process ) === 0 )
33+
continue;
34+
$matches = false;
35+
preg_match( "/(.*?)\s+(\d+).*$/", $process, $matches );
36+
$this->pid = $matches[ 2 ];
37+
return True;
38+
}
39+
return False;
40+
}
41+
else {
42+
$return_message = shell_exec("ps -" . "$this->pid " . "| wc -l");
43+
if (intval($return_message) > 1)
44+
{
45+
return True;
46+
}
47+
return False;
48+
}
3349
}
3450

3551
public function add_args($arg_key, $value = NULL) {
@@ -82,78 +98,49 @@ public function start($arguments) {
8298
$this->binary = new LocalBinary();
8399
$this->binary_path = $this->binary->binary_path();
84100

85-
$descriptorspec = array(
86-
0 => array("pipe", "r"),
87-
1 => array("pipe", "w"),
88-
2 => array("pipe", "w")
89-
);
90-
91-
$call = $this->command();
101+
$call = $this->start_command();
92102
if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
93103
system('echo "" > '. '$this->logfile');
94104
else
95105
system("echo \"\" > '$this->logfile' ");
96-
97-
$this->handle = proc_open($call, $descriptorspec, $this->pipes);
98-
$status = proc_get_status($this->handle);
99-
$this->pid = $status['pid'];
100-
101-
$this->loghandle = fopen($this->logfile,"r");
102-
while (true) {
103-
$buffer = fread($this->loghandle, 1024);
104-
if (preg_match("/Error:[^\n]+/i", $buffer, $match)) {
105-
$this->stop();
106-
throw new LocalException($match[0]);
107-
break;
108-
}
109-
elseif (preg_match("/\bPress Ctrl-C to exit\b/i", $buffer, $match)){
110-
fclose($this->loghandle);
111-
break;
112-
}
113-
114-
//flush();
115-
sleep(1);
106+
$call = $call . "2>&1";
107+
$return_message = shell_exec($call);
108+
$data = json_decode($return_message,true);
109+
if ($data["state"] != "connected") {
110+
throw new LocalException($data['message']);
116111
}
112+
$this->pid = $data['pid'];
117113
}
118114

119115
public function stop() {
120-
fclose($this->loghandle);
121-
if (is_null($this->handle))
122-
return;
123-
else {
124-
while(fgets($this->pipes[0]));
125-
fclose($this->pipes[0]);
126-
fclose($this->pipes[1]);
127-
fclose($this->pipes[2]);
128-
129-
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
130-
exec('kill -15 ' . $this->pid);
131-
} else {
132-
$binary_pid = proc_get_status($this->handle)['pid'];
133-
$wmic_output = shell_exec('wmic process where (ParentProcessId='. $binary_pid. ') get Caption,ProcessId');
134-
preg_match_all('!\d+!', $wmic_output, $possible_pids);
135-
if(!empty($possible_pids[0][0])) {
136-
exec("taskkill /F /PID ". $possible_pids[0][0]);
137-
}
138-
}
139-
140-
proc_terminate($this->handle);
141-
while($this->isRunning())
142-
sleep(1);
143-
}
116+
$call = $this->stop_command();
117+
shell_exec("$call");
118+
}
119+
120+
public function start_command() {
121+
$exec = "exec";
122+
// TODO to test on windows
123+
if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
124+
$exec = "call";
125+
126+
$user_args = join(' ', $this->user_args);
127+
$command = "$exec $this->binary_path -d start -logFile '$this->logfile' $this->folder_flag $this->key $this->folder_path $this->force_local_flag $this->local_identifier_flag $this->only_flag $this->only_automate_flag $this->proxy_host $this->proxy_port $this->proxy_user $this->proxy_pass $this->force_proxy_flag $this->force_flag $this->verbose_flag $this->hosts $user_args";
128+
$command = preg_replace('/\s+/S', " ", $command);
129+
return $command;
144130
}
145131

146-
public function command() {
132+
public function stop_command() {
147133
$exec = "exec";
148134
// TODO to test on windows
149135
if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
150136
$exec = "call";
151137

152138
$user_args = join(' ', $this->user_args);
153-
$command = "$exec $this->binary_path -logFile '$this->logfile' $this->folder_flag $this->key $this->folder_path $this->force_local_flag $this->local_identifier_flag $this->only_flag $this->only_automate_flag $this->proxy_host $this->proxy_port $this->proxy_user $this->proxy_pass $this->force_proxy_flag $this->force_flag $this->verbose_flag $this->hosts $user_args";
139+
$command = "$exec $this->binary_path -d stop $this->local_identifier_flag";
154140
$command = preg_replace('/\s+/S', " ", $command);
155141
return $command;
156142
}
143+
157144
}
158145

159146
?>

tests/LocalTest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public function tearDown(){
2222

2323
public function test_verbose() {
2424
$this->bs_local->add_args('v');
25-
$this->assertContains('-v',$this->bs_local->command());
25+
$this->assertContains('-v',$this->bs_local->start_command());
2626
}
2727

2828
public function test_set_folder() {
2929
$this->bs_local->add_args('f', "/");
30-
$this->assertContains('-f',$this->bs_local->command());
31-
$this->assertContains('/',$this->bs_local->command());
30+
$this->assertContains('-f',$this->bs_local->start_command());
31+
$this->assertContains('/',$this->bs_local->start_command());
3232
}
3333

3434
public function test_enable_force() {
@@ -37,54 +37,54 @@ public function test_enable_force() {
3737

3838
public function test_set_local_identifier() {
3939
$this->bs_local->add_args("localIdentifier", "randomString");
40-
$this->assertContains('-localIdentifier randomString',$this->bs_local->command());
40+
$this->assertContains('-localIdentifier randomString',$this->bs_local->start_command());
4141
}
4242

4343
public function test_enable_only() {
4444
$this->bs_local->add_args("only");
45-
$this->assertContains('-only',$this->bs_local->command());
45+
$this->assertContains('-only',$this->bs_local->start_command());
4646
}
4747

4848
public function test_enable_only_automate() {
4949
$this->bs_local->add_args("onlyAutomate");
50-
$this->assertContains('-onlyAutomate', $this->bs_local->command());
50+
$this->assertContains('-onlyAutomate', $this->bs_local->start_command());
5151
}
5252

5353
public function test_enable_force_local() {
5454
$this->bs_local->add_args("forcelocal");
55-
$this->assertContains('-forcelocal',$this->bs_local->command());
55+
$this->assertContains('-forcelocal',$this->bs_local->start_command());
5656
}
5757

5858
public function test_custom_boolean_argument() {
5959
$this->bs_local->add_args("boolArg1", true);
6060
$this->bs_local->add_args("boolArg2", true);
61-
$this->assertContains('-boolArg1',$this->bs_local->command());
62-
$this->assertContains('-boolArg2',$this->bs_local->command());
61+
$this->assertContains('-boolArg1',$this->bs_local->start_command());
62+
$this->assertContains('-boolArg2',$this->bs_local->start_command());
6363
}
6464

6565
public function test_custom_keyval() {
6666
$this->bs_local->add_args("customKey1", "custom value1");
6767
$this->bs_local->add_args("customKey2", "custom value2");
68-
$this->assertContains('-customKey1 \'custom value1\'',$this->bs_local->command());
69-
$this->assertContains('-customKey2 \'custom value2\'',$this->bs_local->command());
68+
$this->assertContains('-customKey1 \'custom value1\'',$this->bs_local->start_command());
69+
$this->assertContains('-customKey2 \'custom value2\'',$this->bs_local->start_command());
7070
}
7171

7272
public function test_set_proxy() {
7373
$this->bs_local->add_args("proxyHost", "localhost");
7474
$this->bs_local->add_args("proxyPort", 8080);
7575
$this->bs_local->add_args("proxyUser", "user");
7676
$this->bs_local->add_args("proxyPass", "pass");
77-
$this->assertContains('-proxyHost localhost -proxyPort 8080 -proxyUser user -proxyPass pass',$this->bs_local->command());
77+
$this->assertContains('-proxyHost localhost -proxyPort 8080 -proxyUser user -proxyPass pass',$this->bs_local->start_command());
7878
}
7979

8080
public function test_enable_force_proxy() {
8181
$this->bs_local->add_args("-forceproxy");
82-
$this->assertContains('-forceproxy',$this->bs_local->command());
82+
$this->assertContains('-forceproxy',$this->bs_local->start_command());
8383
}
8484

8585
public function test_hosts() {
8686
$this->bs_local->add_args("-hosts", "localhost,8080,0");
87-
$this->assertContains('localhost,8080,0',$this->bs_local->command());
87+
$this->assertContains('localhost,8080,0',$this->bs_local->start_command());
8888
}
8989

9090
public function test_isRunning() {
@@ -113,7 +113,7 @@ public function test_multiple_binary() {
113113
$this->fail("Expected Exception has not been raised.");
114114
} catch (LocalException $ex) {
115115
$emessage = $ex->getMessage();
116-
$this->assertEquals(trim($emessage), 'Error: Either another browserstack local client is running on your machine or some server is listening on port 45691');
116+
$this->assertEquals(trim($emessage), 'Either another browserstack local client is running on your machine or some server is listening on port 45691');
117117
unlink($log_file2);
118118
return;
119119
}

0 commit comments

Comments
 (0)