Skip to content

Commit

Permalink
Merge pull request #57 from LibreSign/features/list-all-proccess
Browse files Browse the repository at this point in the history
List all process
  • Loading branch information
vitormattos authored May 29, 2023
2 parents b7f5e3b + 8d6174e commit d98b6fc
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 26 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: none

- name: Set up dependencies
run: composer install

- name: Run behat
run: vendor/bin/behat
run: |
export BEHAT_RUN_AS=$(ls -ld behat.yml | awk '{print $3}')
export BEHAT_VERBOSE="$RUNNER_DEBUG"
vendor/bin/behat
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ default:
| config | default | Environment | Description |
| ------- | ------------- | -------------- | -------------------------------------------------- |
| verbose | false | none | Enables/disables verbose mode |
| verbose | false | BEHAT_VERBOSE | Enables/disables verbose mode |
| rootDir | /var/www/html | BEHAT_HOST | Specifies http root dir |
| host | localhost | BEHAT_ROOT_DIR | Host domain or IP |
| runAs | | BEHAT_RUN_AS | The username to be used to run the built-in server |
Expand Down
12 changes: 6 additions & 6 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function __construct()
public function serverIsUp(string $status)
{
if ($status === 'up') {
Assert::assertTrue($this->server->isRunning());
Assert::assertTrue($this->server->isRunning(), 'Server is up?');
} else {
Assert::assertFalse($this->server->isRunning());
Assert::assertFalse($this->server->isRunning(), 'Server is down?');
}
}

Expand All @@ -60,7 +60,7 @@ public function theHostOfServerIs($host)
public function startServer()
{
$this->server->start();
Assert::assertTrue($this->server->isRunning());
Assert::assertTrue($this->server->isRunning(), 'Server is running after start?');
}

/**
Expand All @@ -69,15 +69,15 @@ public function startServer()
public function stopServer()
{
$this->server->stop();
Assert::assertFalse($this->server->isRunning());
Assert::assertFalse($this->server->isRunning(), 'Server is stopped after stop?');
}

/**
* @When kill all instances
*/
public function killAllInstances()
{
$this->server->killZombies();
Assert::assertFalse($this->server->isRunning());
$this->server->stop();
Assert::assertFalse($this->server->isRunning(), 'Is server running after run kill?');
}
}
37 changes: 27 additions & 10 deletions src/RunServerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public function start(): void
$verbose = '2>&1';
}

$fullCmd = sprintf(
$fullCmd = $this->parseCommand(sprintf(
'%s > /dev/null %s & echo $!',
escapeshellcmd($cmd),
$verbose
);
));

$this->pid = (string)(int) exec($fullCmd);

Expand All @@ -114,7 +114,10 @@ public function start(): void
}

if (!$this->isRunning()) {
throw new ServerException('Failed to start server. Is something already running on port ' . self::$port . '?');
throw new ServerException(
'Failed to start server. Is something already running on port ' . self::$port . "?\n" .
'Full command: ' . $fullCmd
);
}

register_shutdown_function(function () {
Expand Down Expand Up @@ -146,11 +149,9 @@ public function isRunning(): bool
public function stop(): void
{
if ($this->pid) {
exec(sprintf(
'kill %d',
$this->pid
));
exec($this->parseCommand('kill ' . $this->pid));
}

$this->killZombies();

$this->pid = '0';
Expand All @@ -162,15 +163,31 @@ public function killZombies(): void
'grep "php -S ' . self::$host . '"|' .
'grep -v grep|' .
'sed -e "s/^[[:space:]]*//"|cut -d" " -f1';
$pids = trim(exec($cmd));
$pids = trim(shell_exec($cmd));
$pids = explode("\n", $pids);
foreach ($pids as $pid) {
if ($pid) {
exec('kill ' . $pid);
if ($pid && (!$this->pid || $pid !== $this->pid)) {
exec($this->parseCommand('kill ' . $pid));
}
}
}

/**
* Parse command
*
* Have commands that need to be executed as sudo otherwise don't will work,
* by example the command runuser or kill. To prevent error when run in a
* GitHub Actions, these commands are executed prefixed by sudo when exists
* an environment called GITHUB_ACTIONS.
*/
private function parseCommand(string $command): string
{
if (getenv('GITHUB_ACTIONS') !== false) {
$command = 'sudo ' . $command;
}
return $command;
}

/**
* Get the HTTP root of the webserver
* e.g.: http://127.0.0.1:8123
Expand Down
13 changes: 10 additions & 3 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,19 @@ public function configure(ArrayNodeDefinition $builder): void
/** @inheritDoc */
public function load(ContainerBuilder $container, array $config): void
{
$verbose = $this->getVerboseLevel($container, $config);
$rootDir = $this->getRootDir($config);
$host = $this->getHost($config);
$runAs = $this->getRunAs($config);
$verbose = $this->getVerboseLevel($container, $config);
if (is_numeric($verbose)) {
$output = $container->get('cli.output');
if ($output instanceof OutputInterface) {
$output->writeln('<info>Root dir: ' . $rootDir . '</info>');
$output->writeln('<info>Verbose: ' . $verbose . '</info>');
$output->writeln('<info>Host: ' . $host . '</info>');
$output->writeln('<info>RunAs: ' . $runAs . '</info>');
}
}
$host = $this->getHost($config);
$runAs = $this->getRunAs($config);
$definition = (new Definition('PhpBuiltin\RunServerListener'))
->addTag('event_dispatcher.subscriber')
->setArguments([$verbose, $rootDir, $host, $runAs])
Expand Down Expand Up @@ -150,6 +153,10 @@ private function getVerboseLevel(ContainerBuilder $container, array $config): ?i
$verbose = $input->getParameterOption('-v');
return strlen($verbose);
}
$runAs = getenv('BEHAT_VERBOSE');
if (is_numeric($runAs)) {
return (int) $runAs;
}
return $config['verbose'] ? 0 : null;
}

Expand Down
13 changes: 8 additions & 5 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.12.0@f90118cdeacd0088e7215e64c0c99ceca819e176">
<file src="src/Server.php">
<UndefinedInterfaceMethod>
<code>scalarNode</code>
</UndefinedInterfaceMethod>
<files psalm-version="5.4.0@62db5d4f6a7ae0a20f7cc5a4952d730272fc0863">
<file src="src/RunServerListener.php">
<ForbiddenCode occurrences="1">
<code>shell_exec($cmd)</code>
</ForbiddenCode>
<PossiblyNullArgument occurrences="1">
<code>shell_exec($cmd)</code>
</PossiblyNullArgument>
</file>
</files>

0 comments on commit d98b6fc

Please sign in to comment.