|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Client; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\AMQP\Client as ClientInterface; |
|
7
|
|
|
use Innmind\OperatingSystem\CurrentProcess\Signals; |
|
8
|
|
|
use Innmind\Signals\Signal; |
|
9
|
|
|
|
|
10
|
|
|
final class SignalAware implements ClientInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private $client; |
|
13
|
|
|
private $signals; |
|
14
|
|
|
private $handlersRegistered = false; |
|
15
|
|
|
|
|
16
|
4 |
|
public function __construct(ClientInterface $client, Signals $signals) |
|
17
|
|
|
{ |
|
18
|
4 |
|
$this->client = $client; |
|
19
|
4 |
|
$this->signals = $signals; |
|
20
|
4 |
|
} |
|
21
|
|
|
|
|
22
|
2 |
|
public function channel(): Channel |
|
23
|
|
|
{ |
|
24
|
2 |
|
$this->register(); |
|
25
|
|
|
|
|
26
|
2 |
|
return $this->client->channel(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
2 |
|
public function closed(): bool |
|
30
|
|
|
{ |
|
31
|
2 |
|
return $this->client->closed(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
public function close(): void |
|
35
|
|
|
{ |
|
36
|
2 |
|
$this->client->close(); |
|
37
|
2 |
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
private function register(): void |
|
40
|
|
|
{ |
|
41
|
2 |
|
if ($this->handlersRegistered === true) { |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$softClose = function(): void { |
|
46
|
2 |
|
$this->close(); |
|
47
|
2 |
|
}; |
|
48
|
|
|
|
|
49
|
|
|
$this->signals->listen(Signal::hangup(), static function() { |
|
50
|
|
|
//do nothing so it can run in background |
|
51
|
2 |
|
}); |
|
52
|
2 |
|
$this->signals->listen(Signal::interrupt(), $softClose); |
|
53
|
2 |
|
$this->signals->listen(Signal::abort(), $softClose); |
|
54
|
2 |
|
$this->signals->listen(Signal::terminate(), $softClose); |
|
55
|
2 |
|
$this->signals->listen(Signal::terminalStop(), $softClose); |
|
56
|
2 |
|
$this->signals->listen(Signal::alarm(), $softClose); |
|
57
|
2 |
|
$this->handlersRegistered = true; |
|
58
|
2 |
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|