-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Console] Add
AlarmableCommandInterface
and console.alarm
event
- Loading branch information
Showing
11 changed files
with
578 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.1 | ||
--- | ||
|
||
* Add `AlarmableCommandInterface` and `console.alarm` event | ||
|
||
7.0 | ||
--- | ||
|
||
|
34 changes: 34 additions & 0 deletions
34
src/Symfony/Component/Console/Command/AlarmableCommandInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Command; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
|
||
/** | ||
* Interface for command that listens to SIGALRM signals. | ||
*/ | ||
interface AlarmableCommandInterface | ||
{ | ||
/** | ||
* The method will be called before the command is run and subsequently on each SIGALRM signal. | ||
* | ||
* @return int The alarm time in seconds | ||
*/ | ||
public function getAlarmTime(InputInterface $input): int; | ||
|
||
/** | ||
* The method will be called when the application is signaled with SIGALRM. | ||
* | ||
* @return int|false The exit code to return or false to continue the normal execution | ||
*/ | ||
public function handleAlarm(int|false $previousExitCode = 0): int|false; | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Symfony/Component/Console/Command/TraceableAlarmableCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Command; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Stopwatch\Stopwatch; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @property Command&AlarmableCommandInterface $command | ||
*/ | ||
final class TraceableAlarmableCommand extends TraceableCommand implements AlarmableCommandInterface | ||
{ | ||
public function __construct(Command&AlarmableCommandInterface $command, Stopwatch $stopwatch) | ||
{ | ||
parent::__construct($command, $stopwatch); | ||
} | ||
|
||
public function getSubscribedSignals(): array | ||
{ | ||
$commandSignals = parent::getSubscribedSignals(); | ||
|
||
if (!\in_array(\SIGALRM, $commandSignals, true)) { | ||
$commandSignals[] = \SIGALRM; | ||
} | ||
|
||
return $commandSignals; | ||
} | ||
|
||
public function getAlarmTime(InputInterface $input): int | ||
{ | ||
return $this->command->getAlarmTime($input); | ||
} | ||
|
||
public function handleAlarm(false|int $previousExitCode = 0): int|false | ||
{ | ||
$event = $this->stopwatch->start($this->getName().'.handle_alarm'); | ||
|
||
$exit = $this->command->handleAlarm($previousExitCode); | ||
|
||
$event->stop(); | ||
|
||
$this->recordHandledSignal(\SIGALRM, $event); | ||
|
||
return $exit; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Event; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class ConsoleAlarmEvent extends ConsoleEvent | ||
{ | ||
public function __construct( | ||
Command $command, | ||
InputInterface $input, | ||
OutputInterface $output, | ||
private int|false $exitCode = 0, | ||
) { | ||
parent::__construct($command, $input, $output); | ||
} | ||
|
||
public function setExitCode(int $exitCode): void | ||
{ | ||
if ($exitCode < 0 || $exitCode > 255) { | ||
throw new \InvalidArgumentException('Exit code must be between 0 and 255.'); | ||
} | ||
|
||
$this->exitCode = $exitCode; | ||
} | ||
|
||
public function abortExit(): void | ||
{ | ||
$this->exitCode = false; | ||
} | ||
|
||
public function getExitCode(): int|false | ||
{ | ||
return $this->exitCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.