1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* TaskScheduler |
7
|
|
|
* |
8
|
|
|
* @author Raffael Sahli <[email protected]> |
9
|
|
|
* @copyright Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com) |
10
|
|
|
* @license MIT https://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace TaskScheduler; |
14
|
|
|
|
15
|
|
|
use MongoDB\BSON\UTCDateTime; |
16
|
|
|
use MongoDB\Database; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use TaskScheduler\Exception\SpawnForkException; |
19
|
|
|
use League\Event\Emitter; |
20
|
|
|
|
21
|
|
|
class Queue |
22
|
|
|
{ |
23
|
|
|
use InjectTrait; |
24
|
|
|
use EventsTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Database. |
28
|
|
|
* |
29
|
|
|
* @var Database |
30
|
|
|
*/ |
31
|
|
|
protected $db; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Logger. |
35
|
|
|
* |
36
|
|
|
* @var LoggerInterface |
37
|
|
|
*/ |
38
|
|
|
protected $logger; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Worker factory. |
42
|
|
|
* |
43
|
|
|
* @var WorkerFactoryInterface |
44
|
|
|
*/ |
45
|
|
|
protected $factory; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Jobs queue. |
49
|
|
|
* |
50
|
|
|
* @var MessageQueue |
51
|
|
|
*/ |
52
|
|
|
protected $jobs; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Events queue. |
56
|
|
|
* |
57
|
|
|
* @var MessageQueue |
58
|
|
|
*/ |
59
|
|
|
protected $events; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Worker manager pid. |
63
|
|
|
* |
64
|
|
|
* @var int |
65
|
|
|
*/ |
66
|
|
|
protected $manager_pid; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Sysmfsg queue. |
70
|
|
|
* |
71
|
|
|
* @var resource |
72
|
|
|
*/ |
73
|
|
|
protected $queue; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Scheduler |
77
|
|
|
* |
78
|
|
|
* @var Scheduler |
79
|
|
|
*/ |
80
|
|
|
protected $scheduler; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Init queue. |
84
|
|
|
*/ |
85
|
1 |
|
public function __construct(Scheduler $scheduler, Database $db, WorkerFactoryInterface $factory, LoggerInterface $logger, ?Emitter $emitter=null) |
86
|
|
|
{ |
87
|
1 |
|
$this->scheduler = $scheduler; |
88
|
1 |
|
$this->db = $db; |
89
|
1 |
|
$this->logger = $logger; |
90
|
1 |
|
$this->factory = $factory; |
91
|
1 |
|
$this->jobs = new MessageQueue($db, $scheduler->getJobQueue(), $scheduler->getJobQueueSize(), $logger); |
92
|
1 |
|
$this->events = new MessageQueue($db, $scheduler->getEventQueue(), $scheduler->getEventQueueSize(), $logger); |
93
|
1 |
|
$this->emitter = $emitter ?? new Emitter(); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Startup (blocking process). |
98
|
|
|
*/ |
99
|
1 |
|
public function process(): void |
100
|
|
|
{ |
101
|
|
|
try { |
102
|
1 |
|
$this->queue = msg_get_queue(ftok(__FILE__, 't')); |
103
|
1 |
|
$this->catchSignal(); |
104
|
1 |
|
$this->initWorkerManager(); |
105
|
1 |
|
$this->main(); |
106
|
|
|
} catch (\Exception $e) { |
107
|
|
|
$this->logger->error('main() throw an exception, cleanup and exit', [ |
108
|
|
|
'class' => get_class($this), |
109
|
|
|
'exception' => $e, |
110
|
|
|
]); |
111
|
|
|
|
112
|
|
|
$this->cleanup(SIGTERM); |
113
|
|
|
} |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Wait for worker manager. |
118
|
|
|
*/ |
119
|
|
|
public function exitWorkerManager(int $sig, array $pid): void |
120
|
|
|
{ |
121
|
|
|
$this->logger->debug('fork manager ['.$pid['pid'].'] exit with ['.$sig.']', [ |
122
|
|
|
'category' => get_class($this), |
123
|
|
|
]); |
124
|
|
|
|
125
|
|
|
pcntl_waitpid($pid['pid'], $status, WNOHANG | WUNTRACED); |
126
|
|
|
$this->cleanup(SIGTERM); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Cleanup. |
131
|
|
|
*/ |
132
|
|
|
public function cleanup(int $sig): void |
133
|
|
|
{ |
134
|
|
|
if (null !== $this->manager_pid) { |
135
|
|
|
$this->logger->debug('received exit signal ['.$sig.'], forward signal to the fork manager ['.$sig.']', [ |
136
|
|
|
'category' => get_class($this), |
137
|
|
|
]); |
138
|
|
|
|
139
|
|
|
posix_kill($this->manager_pid, $sig); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->exit(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Fork a worker manager. |
147
|
|
|
*/ |
148
|
1 |
|
protected function initWorkerManager() |
149
|
|
|
{ |
150
|
1 |
|
$pid = pcntl_fork(); |
151
|
1 |
|
$this->manager_pid = $pid; |
152
|
|
|
|
153
|
1 |
|
if (-1 === $pid) { |
154
|
|
|
throw new SpawnForkException('failed to spawn fork manager'); |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
if (!$pid) { |
158
|
|
|
$manager = $this->factory->buildManager(); |
159
|
|
|
$manager->process(); |
160
|
|
|
exit(); |
|
|
|
|
161
|
|
|
} |
162
|
1 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Fork handling, blocking process. |
166
|
|
|
*/ |
167
|
1 |
|
protected function main(): void |
168
|
|
|
{ |
169
|
1 |
|
$this->logger->info('start job listener', [ |
170
|
1 |
|
'category' => get_class($this), |
171
|
|
|
]); |
172
|
|
|
|
173
|
1 |
|
$cursor_jobs = $this->jobs->getCursor([ |
174
|
1 |
|
'$or' => [ |
175
|
|
|
['status' => JobInterface::STATUS_WAITING], |
176
|
|
|
['status' => JobInterface::STATUS_POSTPONED], |
177
|
|
|
], |
178
|
|
|
]); |
179
|
|
|
|
180
|
1 |
|
$cursor_events = $this->events->getCursor([ |
181
|
1 |
|
'timestamp' => ['$gte' => new UTCDateTime()], |
182
|
|
|
'job' => ['$exists' => true], |
183
|
|
|
]); |
184
|
|
|
|
185
|
1 |
|
$this->catchSignal(); |
186
|
|
|
|
187
|
1 |
|
while ($this->loop()) { |
188
|
1 |
|
while ($this->loop()) { |
189
|
|
|
if (null === $cursor_events->current()) { |
190
|
|
|
if ($cursor_events->getInnerIterator()->isDead()) { |
|
|
|
|
191
|
|
|
$this->logger->error('event queue cursor is dead, is it a capped collection?', [ |
192
|
|
|
'category' => get_class($this), |
193
|
|
|
]); |
194
|
|
|
|
195
|
|
|
$this->events->create(); |
196
|
|
|
|
197
|
|
|
$this->main(); |
198
|
|
|
|
199
|
|
|
break; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$this->events->next($cursor_events, function () { |
203
|
|
|
$this->main(); |
204
|
|
|
}); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$event = $cursor_events->current(); |
208
|
|
|
$this->events->next($cursor_events, function () { |
209
|
|
|
$this->main(); |
210
|
|
|
}); |
211
|
|
|
|
212
|
|
|
if($event === null) { |
213
|
|
|
break; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$this->handleEvent($event); |
217
|
|
|
} |
218
|
|
|
|
219
|
1 |
|
if (null === $cursor_jobs->current()) { |
220
|
1 |
|
if ($cursor_jobs->getInnerIterator()->isDead()) { |
221
|
|
|
$this->logger->error('job queue cursor is dead, is it a capped collection?', [ |
222
|
|
|
'category' => get_class($this), |
223
|
|
|
]); |
224
|
|
|
|
225
|
|
|
$this->jobs->create(); |
226
|
|
|
$this->main(); |
227
|
|
|
|
228
|
|
|
break; |
229
|
|
|
} |
230
|
|
|
|
231
|
1 |
|
$this->jobs->next($cursor_jobs, function () { |
232
|
|
|
$this->main(); |
233
|
1 |
|
}); |
234
|
|
|
|
235
|
1 |
|
continue; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$job = $cursor_jobs->current(); |
239
|
|
|
|
240
|
|
|
$this->jobs->next($cursor_jobs, function () { |
241
|
|
|
$this->main(); |
242
|
|
|
}); |
243
|
|
|
|
244
|
|
|
$this->handleJob($job); |
245
|
|
|
} |
246
|
1 |
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Handle events. |
250
|
|
|
*/ |
251
|
|
|
protected function handleEvent(array $event): self |
252
|
|
|
{ |
253
|
|
|
$this->emit($this->scheduler->getJob($event['job'])); |
254
|
|
|
|
255
|
|
|
if($event['status'] > JobInterface::STATUS_POSTPONED) { |
256
|
|
|
$this->logger->debug('received event ['.$event['status'].'] for job ['.$event['job'].'], write into systemv queue', [ |
257
|
|
|
'category' => get_class($this), |
258
|
|
|
]); |
259
|
|
|
|
260
|
|
|
msg_send($this->queue, WorkerManager::TYPE_EVENT, $event); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Handle job. |
268
|
|
|
*/ |
269
|
|
|
protected function handleJob(array $job): self |
270
|
|
|
{ |
271
|
|
|
$this->logger->debug('received job ['.$job['_id'].'], write in systemv message queue', [ |
272
|
|
|
'category' => get_class($this), |
273
|
|
|
]); |
274
|
|
|
|
275
|
|
|
msg_send($this->queue, WorkerManager::TYPE_JOB, [ |
276
|
|
|
'_id' => $job['_id'], |
277
|
|
|
'options' => $job['options'], |
278
|
|
|
]); |
279
|
|
|
|
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Catch signals and cleanup. |
285
|
|
|
*/ |
286
|
1 |
|
protected function catchSignal(): self |
287
|
|
|
{ |
288
|
1 |
|
pcntl_async_signals(true); |
289
|
1 |
|
pcntl_signal(SIGTERM, [$this, 'cleanup']); |
290
|
1 |
|
pcntl_signal(SIGINT, [$this, 'cleanup']); |
291
|
1 |
|
pcntl_signal(SIGCHLD, [$this, 'exitWorkerManager']); |
292
|
|
|
|
293
|
1 |
|
return $this; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.