forked from doppar/queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueWorker.php
More file actions
604 lines (518 loc) · 15 KB
/
Copy pathQueueWorker.php
File metadata and controls
604 lines (518 loc) · 15 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<?php
namespace Doppar\Queue;
use Doppar\Queue\Models\QueueJob;
use Doppar\Queue\Exceptions\JobTimeoutException;
use Doppar\Queue\Contracts\JobInterface;
class QueueWorker
{
/**
* The queue manager instance.
*
* @var QueueManager
*/
protected $manager;
/**
* Indicates if the worker should stop processing.
*
* @var bool
*/
protected $shouldQuit = false;
/**
* The maximum number of seconds a worker may run.
*
* @var int
*/
protected $maxExecutionTime = 0;
/**
* The number of seconds to wait before polling the queue.
*
* @var int
*/
protected $sleep = 3;
/**
* The maximum amount of RAM the worker may consume.
*
* @var int (in megabytes)
*/
protected $maxMemory = 128;
/**
* The maximum number of jobs to process.
*
* @var int|null
*/
protected $maxJobs = null;
/**
* The number of jobs processed.
*
* @var int
*/
protected $jobsProcessed = 0;
/**
* Callback to be executed **before** a job is processed.
*
* @var callable|null
*/
protected $onJobProcessing;
/**
* Callback to be executed **after** a job has been successfully processed.
*
*
* @var callable|null
*/
protected $onJobProcessed;
/**
* Flag to indicate if we're inside a forked timeout context.
*
* @var bool
*/
protected $insideTimeoutContext = false;
/**
* Create a new queue worker.
*
* @param QueueManager $manager
*/
public function __construct(QueueManager $manager)
{
$this->manager = $manager;
}
/**
* Set the callback to be executed **before** a job is processed.
*
* @param callable $callback
* @return void
*/
public function setOnJobProcessing(callable $callback): void
{
$this->onJobProcessing = $callback;
}
/**
* Set the callback to be executed **after** a job has been processed.
*
* @param callable $callback
* @return void
*/
public function setOnJobProcessed(callable $callback): void
{
$this->onJobProcessed = $callback;
}
/**
* Run the worker daemon.
*
* @param string $queue
* @param array $options
* @return void
*/
public function daemon(string $queue = 'default', array $options = []): void
{
$this->configureOptions($options);
$this->registerSignalHandlers();
$startTime = time();
while (true) {
// Check if we should quit
if ($this->shouldQuit()) {
break;
}
// Check if max jobs limit reached
if ($this->maxJobsReached()) {
$this->stop(0, "Maximum job limit of {$this->maxJobs} reached");
break;
}
// Check memory usage
if ($this->memoryExceeded()) {
$this->stop(12, 'Memory limit exceeded');
break;
}
// Check execution time
if ($this->timeExceeded($startTime)) {
$this->stop(13, 'Execution time limit exceeded');
break;
}
// Process the next job
$this->processNextJob($queue);
}
}
/**
* Process the next job on the queue.
*
* @param string $queue
* @return void
*/
protected function processNextJob(string $queue): void
{
try {
$queueJob = $this->manager->pop($queue);
if ($queueJob === null) {
$this->sleep($this->sleep);
return;
}
$this->processJob($queueJob);
$this->jobsProcessed++;
} catch (\Throwable $e) {
$this->handleWorkerException($e);
$this->sleep($this->sleep);
}
}
/**
* Process a single job.
*
* @param QueueJob $queueJob
* @return void
*/
protected function processJob(QueueJob $queueJob): void
{
try {
// Unserialize the job
$job = $this->manager->unserializeJob($queueJob->payload);
$job->attempts = $queueJob->attempts;
if (is_callable($this->onJobProcessing)) {
($this->onJobProcessing)($job);
}
// Execute the job with timeout
$this->executeJobWithTimeout($job);
// Delete the job from queue if successful
$this->manager->delete($queueJob);
// Dispatch next job in chain if this job is chained
if ($job->isChained()) {
$job->dispatchNextChainJob();
}
// Trigger onJobProcessed callback
if (is_callable($this->onJobProcessed)) {
($this->onJobProcessed)($job);
}
} catch (\Throwable $e) {
$this->handleJobException($queueJob, $job ?? null, $e);
}
}
/**
* Execute a job with timeout protection using process forking.
*
* @param JobInterface $job
* @return void
* @throws \Throwable
*/
protected function executeJobWithTimeout(JobInterface $job): void
{
$timeout = $job->getTimeout();
// No timeout or PCNTL not available
if ($timeout === null || !extension_loaded('pcntl') || !function_exists('pcntl_fork')) {
$this->executeJob($job);
return;
}
// Clean up database connections before forking
db()->cleanupAllConnections();
$this->insideTimeoutContext = true;
$pid = pcntl_fork();
if ($pid === -1) {
$this->insideTimeoutContext = false;
$this->logError("Failed to fork process, executing without timeout");
$this->executeJob($job);
return;
}
if ($pid === 0) {
// CHILD PROCESS
pcntl_signal(SIGTERM, SIG_DFL);
pcntl_signal(SIGINT, SIG_DFL);
try {
// Create new database connection in child
db()->cleanupAllConnections();
$this->executeJob($job);
exit(0);
} catch (\Throwable $e) {
error("Job exception: " . $e->getMessage());
exit(1);
}
}
// PARENT PROCESS
$startTime = time();
$timedOut = false;
while (true) {
$status = null;
$result = pcntl_waitpid($pid, $status, WNOHANG);
if ($result === $pid) {
$this->insideTimeoutContext = false;
// Clean up parent's connections too
db()->cleanupAllConnections();
if (pcntl_wifexited($status)) {
$exitCode = pcntl_wexitstatus($status);
if ($exitCode === 0) {
return;
}
throw new \RuntimeException("Job process exited with code {$exitCode}");
}
if (pcntl_wifsignaled($status)) {
$signal = pcntl_wtermsig($status);
if ($timedOut) {
throw new JobTimeoutException(
"Job exceeded maximum execution time of {$timeout} seconds"
);
}
throw new \RuntimeException("Job process was terminated by signal {$signal}");
}
return;
}
// Check timeout
if ((time() - $startTime) >= $timeout) {
$timedOut = true;
$this->logError("Job timeout exceeded ({$timeout}s), killing process {$pid}");
// Kill the process
posix_kill($pid, SIGKILL);
pcntl_waitpid($pid, $status);
// Clean up connections
$this->insideTimeoutContext = false;
db()->cleanupAllConnections();
throw new JobTimeoutException(
"Job exceeded maximum execution time of {$timeout} seconds"
);
}
// 100ms
usleep(100000);
}
}
/**
* Execute a job.
*
* @param JobInterface $job
* @return void
* @throws \Throwable
*/
protected function executeJob(JobInterface $job): void
{
$job->handle();
}
/**
* Handle an exception that occurred while processing a job.
*
* @param QueueJob $queueJob
* @param JobInterface|null $job
* @param \Throwable $exception
* @return void
*/
protected function handleJobException(QueueJob $queueJob, ?JobInterface $job, \Throwable $exception): void
{
try {
$this->logError("Job failed: " . $exception->getMessage());
if ($job === null) {
// Could not unserialize job, mark as failed immediately
$this->manager->markAsFailed($queueJob, $exception);
return;
}
// Handle chain failure - chain stops here
if ($job->isChained()) {
$job->handleChainFailure($exception);
}
// Check if job should be retried
if ($queueJob->attempts < $job->tries()) {
// Release the job back to the queue with delay
$delay = $job->retryAfter();
$released = $this->manager->release($queueJob, $delay);
if ($released) {
$this->logInfo("Job {$job->getJobId()} released back to queue (attempt {$queueJob->attempts}/{$job->tries()})");
} else {
$this->logError("Failed to release job {$job->getJobId()} back to queue");
}
} else {
// Max attempts reached, mark as failed
$this->manager->markAsFailed($queueJob, $exception);
// Call the failed method on the job
try {
$job->failed($exception);
} catch (\Throwable $e) {
$this->logError("Failed callback error: " . $e->getMessage());
}
$this->logError("Job {$job->getJobId()} marked as failed after {$queueJob->attempts} attempts");
}
} catch (\Throwable $e) {
$this->logError("Error handling job exception: " . $e->getMessage());
}
}
/**
* Handle an exception that occurred while the worker was running.
*
* @param \Throwable $exception
* @return void
*/
protected function handleWorkerException(\Throwable $exception): void
{
$this->logError("Worker exception: " . $exception->getMessage());
}
/**
* Determine if the worker should quit.
*
* @return bool
*/
protected function shouldQuit(): bool
{
return $this->shouldQuit;
}
/**
* Determine if the maximum number of jobs has been reached.
*
* @return bool
*/
protected function maxJobsReached(): bool
{
return !empty($this->maxJobs) && $this->jobsProcessed >= $this->maxJobs;
}
/**
* Stop the worker.
*
* @param int $status
* @param string $message
* @return void
*/
public function stop(int $status = 0, string $message = ''): void
{
$this->shouldQuit = true;
if ($message) {
$this->logInfo($message);
}
}
/**
* Determine if the memory limit has been exceeded.
*
* @return bool
*/
protected function memoryExceeded(): bool
{
return (memory_get_usage(true) / 1024 / 1024) >= $this->maxMemory;
}
/**
* Determine if the time limit has been exceeded.
*
* @param int $startTime
* @return bool
*/
protected function timeExceeded(int $startTime): bool
{
if ($this->maxExecutionTime <= 0) {
return false;
}
return (time() - $startTime) >= $this->maxExecutionTime;
}
/**
* Sleep for the given number of seconds.
*
* @param int $seconds
* @return void
*/
protected function sleep(int $seconds): void
{
if ($seconds < 1) {
usleep($seconds * 1000000);
} else {
sleep($seconds);
}
}
/**
* Register signal handlers for graceful shutdown.
*
* @return void
*/
protected function registerSignalHandlers(): void
{
if (!extension_loaded('pcntl')) {
return;
}
pcntl_async_signals(true);
// SIGTERM handler - only trigger if not in timeout context
pcntl_signal(SIGTERM, function () {
if (!$this->insideTimeoutContext) {
$this->stop(0, 'Received SIGTERM signal');
}
});
// SIGINT handler - only trigger if not in timeout context
pcntl_signal(SIGINT, function () {
if (!$this->insideTimeoutContext) {
$this->stop(0, 'Received SIGINT signal');
}
});
}
/**
* Configure worker options.
*
* @param array $options
* @return void
*/
protected function configureOptions(array $options): void
{
if (isset($options['sleep'])) {
$this->sleep = (int) $options['sleep'];
}
if (isset($options['maxMemory'])) {
$this->maxMemory = (int) $options['maxMemory'];
}
if (isset($options['maxExecutionTime'])) {
$this->maxExecutionTime = (int) $options['maxExecutionTime'];
}
$this->maxJobs = $options['maxJobs'] ?? null;
}
/**
* Log an informational message.
*
* @param string $message
* @return void
*/
protected function logInfo(string $message): void
{
echo "[" . date('Y-m-d H:i:s') . "] INFO: $message\n";
}
/**
* Log an error message.
*
* @param string $message
* @return void
*/
protected function logError(string $message): void
{
echo "[" . date('Y-m-d H:i:s') . "] ERROR: $message\n";
}
/**
* Set the sleep duration.
*
* @param int $seconds
* @return void
*/
public function setSleep(int $seconds): void
{
$this->sleep = $seconds;
}
/**
* Set the maximum memory.
*
* @param int $megabytes
* @return void
*/
public function setMaxMemory(int $megabytes): void
{
$this->maxMemory = $megabytes;
}
/**
* Set the maximum execution time.
*
* @param int $seconds
* @return void
*/
public function setMaxExecutionTime(int $seconds): void
{
$this->maxExecutionTime = $seconds;
}
/**
* Set the maximum number of jobs to process.
*
* @param int|null $maxJobs
* @return void
*/
public function setMaxJobs(?int $maxJobs): void
{
$this->maxJobs = $maxJobs;
}
/**
* Get the number of jobs processed.
*
* @return int
*/
public function getJobsProcessed(): int
{
return $this->jobsProcessed;
}
}