Skip to content

Commit 4f48071

Browse files
committed
[amqp] Add test for SignalSocketHelper.
1 parent 2520548 commit 4f48071

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

pkg/amqp-tools/SignalSocketHelper.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class SignalSocketHelper
1414
*/
1515
private $wasThereSignal;
1616

17+
/**
18+
* @var int[]
19+
*/
20+
private $signals = [SIGTERM, SIGQUIT, SIGINT];
21+
1722
public function __construct()
1823
{
1924
$this->handlers = [];
@@ -25,30 +30,33 @@ public function beforeSocket()
2530
throw new \LogicException('The handlers property should be empty but it is not. The afterSocket method might not have been called.');
2631
}
2732
if (null !== $this->wasThereSignal) {
28-
throw new \LogicException('The wasSignal property should be null but it is not. The afterSocket method might not have been called.');
33+
throw new \LogicException('The wasThereSignal property should be null but it is not. The afterSocket method might not have been called.');
2934
}
3035

3136
$this->wasThereSignal = false;
3237

33-
foreach ([SIGTERM, SIGQUIT, SIGINT] as $signal) {
38+
foreach ($this->signals as $signal) {
3439
/** @var callable $handler */
35-
if ($handler = pcntl_signal_get_handler(SIGTERM)) {
36-
pcntl_signal($signal, function ($signal) use ($handler) {
37-
$this->wasThereSignal = true;
40+
$handler = pcntl_signal_get_handler($signal);
3841

39-
$handler($signal);
40-
});
42+
pcntl_signal($signal, function ($signal) use ($handler) {
43+
var_dump('fuckk!');
44+
$this->wasThereSignal = true;
4145

42-
$this->handlers[$signal] = $handler;
43-
}
46+
$handler && $handler($signal);
47+
});
48+
49+
$handler && $this->handlers[$signal] = $handler;
4450
}
4551
}
4652

4753
public function afterSocket()
4854
{
4955
$this->wasThereSignal = null;
5056

51-
foreach ($this->handlers as $signal => $handler) {
57+
foreach ($this->signals as $signal) {
58+
$handler = isset($this->handlers[$signal]) ? $this->handlers[$signal] : SIG_DFL;
59+
5260
pcntl_signal($signal, $handler);
5361
}
5462

pkg/enqueue/Consumption/Extension/SignalExtension.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public function onStart(Context $context)
3131
throw new LogicException('The pcntl extension is required in order to catch signals.');
3232
}
3333

34-
pcntl_async_signals(true);
34+
if (function_exists('pcntl_async_signals')) {
35+
pcntl_async_signals(true);
36+
}
3537

3638
pcntl_signal(SIGTERM, [$this, 'handleSignal']);
3739
pcntl_signal(SIGQUIT, [$this, 'handleSignal']);
@@ -47,7 +49,7 @@ public function onBeforeReceive(Context $context)
4749
{
4850
$this->logger = $context->getLogger();
4951

50-
pcntl_signal_dispatch();
52+
$this->dispatchSignal();
5153

5254
$this->interruptExecutionIfNeeded($context);
5355
}
@@ -65,7 +67,7 @@ public function onPreReceived(Context $context)
6567
*/
6668
public function onPostReceived(Context $context)
6769
{
68-
pcntl_signal_dispatch();
70+
$this->dispatchSignal();
6971

7072
$this->interruptExecutionIfNeeded($context);
7173
}
@@ -75,7 +77,7 @@ public function onPostReceived(Context $context)
7577
*/
7678
public function onIdle(Context $context)
7779
{
78-
pcntl_signal_dispatch();
80+
$this->dispatchSignal();
7981

8082
$this->interruptExecutionIfNeeded($context);
8183
}
@@ -119,4 +121,11 @@ public function handleSignal($signal)
119121
break;
120122
}
121123
}
124+
125+
private function dispatchSignal()
126+
{
127+
if (false == function_exists('pcntl_async_signals')) {
128+
pcntl_signal_dispatch();
129+
}
130+
}
122131
}

0 commit comments

Comments
 (0)