Skip to content

Commit

Permalink
Run command: add signals handling
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 23, 2024
1 parent 7f71a5f commit 55bebf9
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/Command/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Buggregator\Trap\Sender;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\SignalableCommandInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -24,8 +25,10 @@
name: 'run',
description: 'Run application',
)]
final class Run extends Command
final class Run extends Command implements SignalableCommandInterface
{
private ?Application $app = null;

public function configure(): void
{
$this->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Port to listen', 9912);
Expand Down Expand Up @@ -54,14 +57,14 @@ protected function execute(

$registry = $this->createRegistry($output);

$app = new Application(
$this->app = new Application(
[new SocketServer($port)],
new Logger($output),
senders: $registry->getSenders($senders),
withFrontend: $input->getOption('ui') !== false,
);

$app->run();
$this->app->run();
} catch (\Throwable $e) {
if ($output->isVerbose()) {
// Write colorful exception (title, message, stacktrace)
Expand Down Expand Up @@ -93,4 +96,28 @@ public function createRegistry(OutputInterface $output): Sender\SenderRegistry

return $registry;
}

public function getSubscribedSignals(): array
{
$result = [];
\defined('SIGINT') and $result[] = \SIGINT;
\defined('SIGTERM') and $result[] = \SIGTERM;

return $result;
}

public function handleSignal(int $signal): void
{
if (\defined('SIGINT') && $signal === \SIGINT) {
// todo may be uncommented if it's possible to switch fibers when signal is received
// todo Error: Cannot switch fibers in current execution context
// $this->app?->cancel();

$this->app?->destroy();
}

if (\defined('SIGTERM') && $signal === \SIGTERM) {
$this->app?->destroy();
}
}
}

0 comments on commit 55bebf9

Please sign in to comment.