Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Yii2 Queue Extension Change Log
2.3.1 under development
-----------------------

- no changes in this release.
- Bug #380: Fixed amqp-interop queue/listen signal handling (tarinu)


2.3.0 June 04, 2019
Expand Down
22 changes: 22 additions & 0 deletions src/drivers/amqp_interop/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,28 @@ public function init()
Event::on(BaseApp::class, BaseApp::EVENT_AFTER_REQUEST, function () {
$this->close();
});

if (extension_loaded('pcntl') && 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);
});
}
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/drivers/CliTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function runProcess($cmd)

/**
* @param string $cmd
* @return Process
*/
protected function startProcess($cmd)
{
Expand All @@ -48,6 +49,7 @@ protected function startProcess($cmd)
throw new ProcessFailedException($process);
}
$this->processes[] = $process;
return $process;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/drivers/amqp_interop/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ public function testPriority()
$this->assertEquals('12345', file_get_contents(PriorityJob::getFileName()));
}

/**
* @requires extension pcntl
*/
public function testSignals()
{
$signals = [
1 => 129, // SIGHUP
2 => 130, // SIGINT
3 => 131, // SIGQUIT
15 => 143, // SIGTERM
];

foreach ($signals as $signal => $exitCode) {
$process = $this->startProcess('php yii queue/listen');
$this->assertTrue($process->isRunning());
$process->signal($signal);
$process->wait();
$this->assertFalse($process->isRunning());
$this->assertEquals($exitCode, $process->getExitCode());
}
}

/**
* @return Queue
*/
Expand Down