You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #53533 [Console] Add ability to schedule alarm signals and a ConsoleAlarmEvent (HypeMC)
This PR was merged into the 7.2 branch.
Discussion
----------
[Console] Add ability to schedule alarm signals and a `ConsoleAlarmEvent`
| Q | A
| ------------- | ---
| Branch? | 7.2
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Issues | Fix#47920
| License | MIT
Part of #53508
This PR introduces the ability to schedule alarm signals and a `console.alarm` event. A command using this feature will automatically dispatch an alarm at the specified interval:
```php
#[AsCommand('app:alarm')]
class AlarmCommand extends Command implements SignalableCommandInterface
{
private bool $run = true;
private int $count = 0;
private OutputInterface $output;
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->getApplication()->setAlarmInterval(10);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->output = $output;
while ($this->run) {
$this->count++;
$output->writeln('execute '.$this->count);
sleep(1);
}
$output->writeln('Done');
return Command::SUCCESS;
}
public function getSubscribedSignals(): array
{
return [\SIGINT, \SIGALRM];
}
public function handleSignal(int $signal, false|int $previousExitCode = 0): int|false
{
if (\SIGINT === $signal) {
$this->run = false;
$this->output->writeln('Bye');
} elseif (\SIGALRM === $signal) {
$this->count = 0;
$this->output->writeln('handleAlarm');
}
return false;
}
}
```
The `console.alarm` event is dispatched on every `SIGALRM` signal:
```php
#[AsEventListener(event: ConsoleAlarmEvent::class)]
final class ConsoleAlarmListener
{
public function __invoke(ConsoleAlarmEvent $event): void
{
$event->getOutput()->writeln('ConsoleAlarmListener');
}
}
```
Commits
-------
97e4391 [Console] Add ability to schedule alarm signals and a `console.alarm` event
if ($commandSignals || $this->dispatcher && $this->signalsToDispatchEvent) {
984
-
if (!$this->signalRegistry) {
985
-
thrownewRuntimeException('Unable to subscribe to signal events. Make sure that the "pcntl" extension is installed and that "pcntl_*" functions are not disabled by your php.ini\'s "disable_functions" directive.');
0 commit comments