Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MERGE]: develop into main #15

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions app/Commands/DriverManagerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class DriverManagerCommand extends Command
];

protected array $commands = [
'start' => './chromedriver --log-level=ALL --port={port} &',
'pid' => "ps aux | grep '[c]hromedriver --log-level=ALL {options}' | awk '{print $2,$13}'",
'start' => './{binary} --log-level=ALL --port={port} &',
'pid' => "ps aux | grep '{binary} --log-level=ALL {options}' | awk '{print $2,$13}'",
'stop' => 'kill -9 {pid}',
];

Expand Down Expand Up @@ -85,6 +85,9 @@ protected function start(string $port): int
return self::SUCCESS;
}

/**
* @throws \Exception if you're on a Windows machine (OS)
*/
public function stop(string $port): int
{
intro("Stopping Google Chrome Driver on port [$port]");
Expand Down Expand Up @@ -129,15 +132,19 @@ protected function status(string $port): int
{
intro("Getting Google Chrome Driver status on port [$port]");

$pid = $this->getProcessID($port);
try {
$pid = $this->getProcessID($port);
} catch (\Exception) {
warning('We are running on windows and we cannot be sure if the server is running, but we will try to check');
}

if (empty($pid)) {
warning("There's no server available on port [$port]");

return self::FAILURE;
}

$response = Http::get('http://localhost:9515/status');
$response = Http::get("http://localhost:$port/status");

$data = $response->json('value');

Expand All @@ -152,6 +159,9 @@ protected function status(string $port): int
return self::SUCCESS;
}

/**
* @throws \Exception if you're on a Windows machine (OS)
*/
protected function list(): int
{
info('Listing all the servers available');
Expand All @@ -169,6 +179,9 @@ protected function list(): int
return self::SUCCESS;
}

/**
* @throws \Exception if you're on a Windows machine (OS)
*/
protected function kill(): int
{
$pids = $this->getProcessIDs();
Expand Down Expand Up @@ -199,6 +212,14 @@ protected function kill(): int

protected function command(string $cmd, array $with)
{
$binary = $this->getBinary();

if ($cmd === 'pid') {
$binary = Str::of($binary)->replaceFirst('c', '[c]');
}

$with = array_merge($with, ['{binary}' => $binary]);

return Process::command(
Str::replace(
collect($with)->keys(),
Expand All @@ -210,6 +231,10 @@ protected function command(string $cmd, array $with)

protected function getProcessID(string $port): ?int
{
if ($this->onWindows()) {
throw new \Exception('We cannot get a server PID on Windows');
}

$process = $this->command('pid', ['{options}' => '--port='.$port]);

$output = explode(' ', trim($process->output()));
Expand All @@ -219,6 +244,10 @@ protected function getProcessID(string $port): ?int

protected function getProcessIDs(): ?Collection
{
if ($this->onWindows()) {
throw new \Exception('We cannot get the servers PID on Windows');
}

$process = $this->command('pid', ['{options}' => '']);

if (empty($process->output())) {
Expand All @@ -239,6 +268,16 @@ protected function getPorts(): Collection
return collect($this->option('port') ? [...$this->option('port')] : $this->port)->unique()->filter();
}

protected function onWindows(): bool
{
return OperatingSystem::onWindows();
}

protected function getBinary(): string
{
return 'chromedriver'.($this->onWindows() ? '.exe' : '');
}

protected function getChromeDriverDirectory(): string
{
return $this->option('path')
Expand Down