Skip to content

Commit

Permalink
try run pcntl_signal_dispatch on start and end of job
Browse files Browse the repository at this point in the history
  • Loading branch information
dimansuvorlinda committed Jul 15, 2023
1 parent bd92d80 commit bc13f4c
Showing 1 changed file with 3 additions and 31 deletions.
34 changes: 3 additions & 31 deletions src/drivers/amqp_interop/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,7 @@ public function init()
$this->close();
});

if (
extension_loaded('pcntl') && function_exists('pcntl_signal') && PHP_MAJOR_VERSION >= 7 &&
$this->driver !== self::ENQUEUE_AMQP_EXT
) {
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];

Expand Down Expand Up @@ -317,6 +314,7 @@ public function listen()
$consumer = $this->context->createConsumer($queue);

$callback = function (AmqpMessage $message, AmqpConsumer $consumer) {
pcntl_signal_dispatch();
if ($message->isRedelivered()) {
$consumer->acknowledge($message);

Expand All @@ -325,20 +323,6 @@ public function listen()
return true;
}

// Ignore signal for amqp extension while job is executing
if (
extension_loaded('pcntl') && function_exists('pcntl_signal') && PHP_MAJOR_VERSION >= 7 &&
$this->driver === self::ENQUEUE_AMQP_EXT
) {
$signals = [SIGTERM, SIGQUIT, SIGINT, SIGHUP];

foreach ($signals as $signal) {
pcntl_signal($signal, static function ($signal) {
Yii::$app->end();
});
}
}

$ttr = $message->getProperty(self::TTR);
$attempt = $message->getProperty(self::ATTEMPT, 1);

Expand All @@ -349,19 +333,7 @@ public function listen()

$this->redeliver($message);
}

// For amqp extension: restore default signal handlers
if (
extension_loaded('pcntl') && function_exists('pcntl_signal') && PHP_MAJOR_VERSION >= 7 &&
$this->driver === self::ENQUEUE_AMQP_EXT
) {
pcntl_signal_dispatch();

$signals = [SIGTERM, SIGQUIT, SIGINT, SIGHUP];
foreach ($signals as $signal) {
pcntl_signal($signal, SIG_DFL);
}
}
pcntl_signal_dispatch();

return true;
};
Expand Down

0 comments on commit bc13f4c

Please sign in to comment.