diff --git a/CHANGELOG.md b/CHANGELOG.md index a99ce1640..008b0db42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Yii2 Queue Extension Change Log - Enh #503: All dependent packages for supported drivers have been updated to the latest versions (@s1lver) - Enh #503: The `opis/closure` package did not support PHP 8.1 and was replaced by the `laravel/serializable-closure` package (@s1lver) - Enh #544: Applying Yii2 coding standards (@s1lver) +- Bug #552: Move signal handling from AMQP Interop queue component to controller (@snewer) - Bug #563: Fix `@property` annotations in `InfoAction` and `Queue` drivers (mspirkov) - Enh #564: Allow `symfony/process` `^8.0` (lukascernydis) - Enh #565: Add the missing `@property` tags (mspirkov) diff --git a/src/drivers/amqp_interop/Command.php b/src/drivers/amqp_interop/Command.php index de7850ad6..fd586a530 100644 --- a/src/drivers/amqp_interop/Command.php +++ b/src/drivers/amqp_interop/Command.php @@ -26,6 +26,38 @@ class Command extends CliCommand */ public CliQueue $queue; + /** + * @inheritdoc + */ + public function init(): void + { + parent::init(); + + // https://github.com/yiisoft/yii2-queue/issues/379 + if (extension_loaded('pcntl') && function_exists('pcntl_signal')) { + // https://github.com/php-amqplib/php-amqplib#unix-signals + $signals = [SIGTERM, SIGQUIT, SIGINT, SIGHUP]; + + foreach ($signals as $signal) { + /** @var (callable(int):void)|int|null $oldHandler */ + $oldHandler = null; + // This got added in php 7.1 and might not exist on all supported versions + if (function_exists('pcntl_signal_get_handler')) { + $oldHandler = pcntl_signal_get_handler($signal); + } + + pcntl_signal($signal, static function ($signal) use ($oldHandler) { + if ($oldHandler && is_callable($oldHandler)) { + $oldHandler($signal); + } + + pcntl_signal($signal, SIG_DFL); + posix_kill(posix_getpid(), $signal); + }); + } + } + } + /** * @inheritdoc */ diff --git a/src/drivers/amqp_interop/Queue.php b/src/drivers/amqp_interop/Queue.php index 6f10abe64..fd045db82 100644 --- a/src/drivers/amqp_interop/Queue.php +++ b/src/drivers/amqp_interop/Queue.php @@ -293,28 +293,6 @@ public function init(): void Event::on(BaseApp::class, BaseApp::EVENT_AFTER_REQUEST, function () { $this->close(); }); - - if (extension_loaded('pcntl') && function_exists('pcntl_signal') && PHP_MAJOR_VERSION >= 7) { - // https://github.com/php-amqplib/php-amqplib#unix-signals - $signals = [SIGTERM, SIGQUIT, SIGINT, SIGHUP]; - - foreach ($signals as $signal) { - $oldHandler = null; - // This got added in php 7.1 and might not exist on all supported versions - if (function_exists('pcntl_signal_get_handler')) { - $oldHandler = pcntl_signal_get_handler($signal); - } - - pcntl_signal($signal, static function ($signal) use ($oldHandler) { - if ($oldHandler && is_callable($oldHandler)) { - $oldHandler($signal); - } - - pcntl_signal($signal, SIG_DFL); - posix_kill(posix_getpid(), $signal); - }); - } - } } /**