-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignalSubscriber.php
67 lines (58 loc) · 1.63 KB
/
SignalSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Quartz\Bridge;
use Quartz\Core\SchedulerException;
use Quartz\Events\Event;
use Quartz\Events\TickEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SignalSubscriber implements EventSubscriberInterface
{
/**
* @var bool
*/
private $interruptConsumption;
public function __construct()
{
$this->interruptConsumption = false;
}
public function registerHandleSignalCallback()
{
if (false == extension_loaded('pcntl')) {
throw new SchedulerException('The pcntl extension is required in order to catch signals.');
}
pcntl_async_signals(true);
pcntl_signal(SIGTERM, [$this, 'handleSignal']);
pcntl_signal(SIGQUIT, [$this, 'handleSignal']);
pcntl_signal(SIGINT, [$this, 'handleSignal']);
}
public function handleSignalDispatch(TickEvent $event)
{
if ($this->interruptConsumption) {
$event->setInterrupted(true);
}
}
/**
* @param int $signal
*/
public function handleSignal($signal)
{
switch ($signal) {
case SIGTERM: // 15 : supervisor default stop
case SIGQUIT: // 3 : kill -s QUIT
case SIGINT: // 2 : ctrl+c
$this->interruptConsumption = true;
break;
default:
break;
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
Event::SCHEDULER_STARTING => 'registerHandleSignalCallback',
Event::SCHEDULER_TICK => 'handleSignalDispatch',
];
}
}