|
1
|
|
|
<?php namespace Comodojo\Extender\Base; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Extender\Components\Niceness; |
|
4
|
|
|
use \Comodojo\Dispatcher\Components\DataAccess as DataAccessTrait; |
|
5
|
|
|
use \Comodojo\Extender\Events\SignalEvent; |
|
6
|
|
|
use \Comodojo\Dispatcher\Components\Configuration; |
|
7
|
|
|
use \Comodojo\Dispatcher\Components\Timestamp as TimestampTrait; |
|
8
|
|
|
use \League\Event\Emitter; |
|
9
|
|
|
use \Psr\Log\LoggerInterface; |
|
10
|
|
|
use \Exception; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Basic signaling for extender components |
|
14
|
|
|
* |
|
15
|
|
|
* @package Comodojo Framework |
|
16
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
17
|
|
|
* @license GPL-3.0+ |
|
18
|
|
|
* |
|
19
|
|
|
* LICENSE: |
|
20
|
|
|
* |
|
21
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
22
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
23
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
24
|
|
|
* License, or (at your option) any later version. |
|
25
|
|
|
* |
|
26
|
|
|
* This program is distributed in the hope that it will be useful, |
|
27
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
28
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
29
|
|
|
* GNU Affero General Public License for more details. |
|
30
|
|
|
* |
|
31
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
32
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
33
|
|
|
*/ |
|
34
|
|
|
|
|
35
|
|
|
abstract class Process { |
|
36
|
|
|
|
|
37
|
|
|
use DataAccessTrait; |
|
38
|
|
|
use TimestampTrait; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @todo exit condition if not in command line |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
Configuration $configuration, |
|
45
|
|
|
LoggerInterface $logger, |
|
46
|
|
|
Emitter $events, |
|
47
|
|
|
$niceness = null) |
|
48
|
|
|
{ |
|
49
|
|
|
|
|
50
|
|
|
// get current PID and timestamp |
|
51
|
|
|
|
|
52
|
|
|
$this->pid = $this->getPid(); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$this->setTimestamp(); |
|
55
|
|
|
|
|
56
|
|
|
// init main components |
|
57
|
|
|
|
|
58
|
|
|
$this->configuration = $configuration; |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
$this->logger = $logger; |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
$this->events = $events; |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
// adjust process niceness |
|
65
|
|
|
|
|
66
|
|
|
$this->niceness = new Niceness($this->logger); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
$this->niceness->set($niceness); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$this->registerSignals(); |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
abstract public function shutdown(); |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Register signals |
|
78
|
|
|
* |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function registerSignals() { |
|
81
|
|
|
|
|
82
|
|
|
$pluggable_signals = array( |
|
83
|
|
|
SIGHUP, SIGCHLD, SIGUSR1, SIGUSR2, SIGILL, SIGTRAP, SIGABRT, SIGIOT, |
|
84
|
|
|
SIGBUS, SIGFPE, SIGSEGV, SIGPIPE, SIGALRM, SIGTTIN, SIGTTOU, SIGURG, |
|
85
|
|
|
SIGXCPU, SIGXFSZ, SIGVTALRM, SIGPROF, SIGWINCH, SIGIO, SIGSYS, SIGBABY, |
|
86
|
|
|
SIGTSTP, SIGCONT |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
if ( defined('SIGPOLL') ) $pluggable_signals[] = SIGPOLL; |
|
90
|
|
|
if ( defined('SIGPWR') ) $pluggable_signals[] = SIGPWR; |
|
91
|
|
|
if ( defined('SIGSTKFLT') ) $pluggable_signals[] = SIGSTKFLT; |
|
92
|
|
|
|
|
93
|
|
|
// register supported signals |
|
94
|
|
|
|
|
95
|
|
|
pcntl_signal(SIGTERM, array($this, 'sigTermHandler')); |
|
96
|
|
|
|
|
97
|
|
|
pcntl_signal(SIGINT, array($this, 'sigIntHandler')); |
|
98
|
|
|
|
|
99
|
|
|
// pcntl_signal(SIGTSTP, array($this, 'sigStopHandler')); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
// pcntl_signal(SIGCONT, array($this, 'sigContHandler')); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
// register pluggable signals |
|
104
|
|
|
|
|
105
|
|
|
foreach ( $pluggable_signals as $signal ) { |
|
106
|
|
|
|
|
107
|
|
|
pcntl_signal($signal, array($this, 'genericSignalHandler')); |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// register shutdown function |
|
112
|
|
|
|
|
113
|
|
|
register_shutdown_function(array($this, 'shutdown')); |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* The sigTerm handler. |
|
119
|
|
|
* |
|
120
|
|
|
* It kills everything and then exit with status 1 |
|
121
|
|
|
*/ |
|
122
|
|
View Code Duplication |
public function sigIntHandler($signal) { |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
if ( $this->pid == $this->getPid() ) { |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
$this->logger->info("Received TERM signal, shutting down process gracefully"); |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
$this->events->emit( new SignalEvent($signal, $this) ); |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
$this->end(0); |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* The sigTerm handler. |
|
138
|
|
|
* |
|
139
|
|
|
* It kills everything and then exit with status 1 |
|
140
|
|
|
*/ |
|
141
|
|
View Code Duplication |
public function sigTermHandler($signal) { |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
if ( $this->pid == $this->getPid() ) { |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
$this->logger->info("Received TERM signal, shutting down process"); |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
$this->events->emit( new SignalEvent($signal, $this) ); |
|
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
$this->end(1); |
|
150
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* The generig signal handler. |
|
157
|
|
|
* |
|
158
|
|
|
* It can be used to handle custom signals |
|
159
|
|
|
*/ |
|
160
|
|
View Code Duplication |
public function genericSignalHandler($signal) { |
|
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
if ( $this->pid == $this->getPid() ) { |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
$this->logger->info("Received $signal signal, firing associated event(s)"); |
|
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
$this->events->emit( new SignalEvent($signal, $this) ); |
|
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param integer $return_code |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function end($return_code) { |
|
176
|
|
|
|
|
177
|
|
|
if ( $this->configuration->get('is-test') === true ) { |
|
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
if ( $return_code === 1 ) throw new Exception("Test Exception"); |
|
180
|
|
|
|
|
181
|
|
|
else return $return_code; |
|
182
|
|
|
|
|
183
|
|
|
} else { |
|
184
|
|
|
|
|
185
|
|
|
exit($return_code); |
|
186
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
private function getPid() { |
|
192
|
|
|
|
|
193
|
|
|
return posix_getpid(); |
|
194
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.