magento / magento/community-features
Allow handling POSIX signals (ie. SIGTERM for graceful termination)
Nobody has claimed this yet.
- Dominant language
- No language data
- Stars
- 46
- Forks
- 14
- PR merge metrics
- No merged PRs in 30d
Description
ℹ️ _Copied from https://github.com/magento/magento2/issues/28870 as requested by @engcom-Lima_
### Description (*)
Magento is currently not able to handle POSIX signals, for instance `SIGTERM` that may be received from a controller (ie. within Kubernetes cluster).
Therefore, those signals can not be handled gracefully, and the process may be killed with a `SIGKILL` signal, causing an immediate stop, without calling `__destruct` methods for instance.
### Expected behavior (*)
Since PHP 7.1, signals can be received without overhead using the `pcntl_async_signals(true)` function call.
Signals can subsequently be handled using something like:
```php
pcntl_signal(SIGTERM, function () {
// Do something that will cause graceful shutdown of process
});
```
Calling the `exit()` PHP function within this handler causes the `__destruct` class methods to be called, which is desirable.
#### Example script
```php
`/signal.php`
getObjectManager();
$resourceConnection = $objectManager->create('Magento\Framework\App\ResourceConnection');
class Signal
{
public function run()
{
while (true) {
echo date('Y-m-d H:i:s') . PHP_EOL;
sleep(1);
}
}
public function __destruct()
{
echo __METHOD__ . PHP_EOL;
}
}
pcntl_async_signals(true);
pcntl_signal(SIGTERM, function () {
echo 'Caught SIGTERM' . PHP_EOL;
exit(1);
});
(new Signal())->run();
```
When sending a `SIGTERM` signal to the process, we get the following output:
```
$ php signal.php
2020-06-24 11:06:11
Caught SIGTERM
Signal::__destruct
```
ℹ️ Without these `pcntl_*` functions, the script runs indefinitely, until killed with a `SIGKILL` signal.
**Magento should therefore call `pcntl_async_signals(true)`, at least for cron and consumers.**
### Benefits
Such signal handling would be useful to handle graceful termination of long-running processes (cron, consumers): process current message and exit, close database connection, etc.
For consumers, this achieves the same goal as _poison pill_, but with an immediate effect, not requiring to wait for the message to be processed.
### Additional information
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the signal.php example and app/bootstrap.php, then locate the cron and consumer entry points that manage long-running processes. Compare their lifecycle with the documented pcntl_async_signals and SIGTERM behavior; done means SIGTERM allows graceful termination and cleanup instead of an immediate kill.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend, cli
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100