-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPcntlController.php
More file actions
164 lines (140 loc) · 5.23 KB
/
PcntlController.php
File metadata and controls
164 lines (140 loc) · 5.23 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/*
* This file is part of the process-control package.
*
* (c) Hannes Schulz <hannes.schulz@aboutcoders.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Abc\ProcessControl;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* PcntlController determines whether to stop execution based on PCNTL events.
*
* @author Hannes Schulz <hannes.schulz@aboutcoders.com>
*/
class PcntlController implements ControllerInterface
{
/**
* @var ControllerInterface
*/
protected $fallbackController;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var array
*/
private $stopSignals, $pauseSignals, $resumeSignals = array();
/**
* @var bool
*/
private $stop = false;
/**
* @var bool
*/
private $pause = false;
/**
* @param array $stopSignals An array of signal values or name of signals that indicate to stop processing
* @param array $pauseSignals An array of signal values or name of signals that indicate to pause processing
* @param array $resumeSignals An array of signal values or name of signals that indicate to resume processing
* @param ControllerInterface $fallbackController A fallback controller that will be used if PCNTL extension is not loaded
* @param LoggerInterface|null $logger
*/
public function __construct(array $stopSignals, array $pauseSignals = array(), array $resumeSignals = array(), ControllerInterface $fallbackController = null, LoggerInterface $logger = null)
{
$this->fallbackController = $fallbackController;
$this->logger = $logger == null ? new NullLogger() : $logger;
$this->logger->info('Initialize PCNTL controller for signals {signals}', array('signals' => $stopSignals));
if (!extension_loaded('pcntl') && $fallbackController == null) {
throw new \RuntimeException('The PCNTL extension is not loaded');
} elseif (!extension_loaded('pcntl') && $fallbackController != null) {
$this->logger->warning('PcntlController switched to fallback controller because PCNTL functions do not exist');
} elseif (extension_loaded('pcntl')) {
$this->stopSignals = $this->cleanSignals($stopSignals);
$this->pauseSignals = $this->cleanSignals($pauseSignals);
$this->resumeSignals = $this->cleanSignals($resumeSignals);
foreach (array_merge($this->stopSignals, $this->pauseSignals, $this->resumeSignals) as $value) {
if (false === @pcntl_signal($value, array(&$this, 'handleSignal'))) {
throw new \RuntimeException(sprintf('Failed to register handler for signal "%s"', $value));
}
}
}
}
/**
* @deprecated since 1.3.0 (to be removed in 2.0)
* {@inheritdoc}
*/
public function doExit()
{
@trigger_error(sprintf('The %s method is deprecated since version 1.3.0 and will be removed in version 2.0. Use doStop() instead.', __METHOD__), E_USER_DEPRECATED);
return $this->doStop();
}
/**
* {@inheritdoc}
*/
public function doStop()
{
if ($this->fallbackController != null) {
return $this->fallbackController->doStop();
}
pcntl_signal_dispatch();
return $this->stop;
}
/**
* {@inheritdoc}
*/
public function doPause()
{
if ($this->fallbackController != null) {
return $this->fallbackController->doPause();
}
pcntl_signal_dispatch();
return $this->pause;
}
/**
* Internal callback function registered with pcntl_signal()
*
* @param int $signal
*/
public function handleSignal($signal)
{
if (in_array($signal, $this->stopSignals)) {
$this->logger->info(sprintf('Handle stop signal (%s)', $signal));
$this->stop = true;
} elseif (in_array($signal, $this->pauseSignals)) {
$this->logger->info(sprintf('Handle pause signal (%s)', $signal));
$this->pause = true;
} elseif (in_array($signal, $this->resumeSignals)) {
$this->logger->info(sprintf('Handle resume signal (%s)', $signal));
$this->pause = false;
}
}
/**
* Cleans a signal value.
*
* @param array $values
* @return array The cleaned values
* @throws \InvalidArgumentException If validation fails
*/
private function cleanSignals(array $values)
{
$cleanedValues = array();
foreach ($values as $value) {
if (is_string($value)) {
$value = @constant($value);
if (null === $value) {
throw new \InvalidArgumentException(sprintf('The value "%s" does not specify name of a PCNTL signal constant', $value));
}
}
if (!is_int($value) || $value <= 0) {
throw new \InvalidArgumentException(sprintf('The value "%s" is not a valid process signal value', $value));
}
$cleanedValues[] = $value;
}
return $cleanedValues;
}
}