• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daycry / jobs / 21442227581

28 Jan 2026 02:30PM UTC coverage: 56.301% (-3.1%) from 59.413%
21442227581

push

github

daycry
Improve queue handling and job execution logic

Refactor queue worker to support background execution and improve job fetching logic. Update JobLifecycleCoordinator to prioritize job-specific and default timeouts without a global cap. Replace custom UUID generation with service-based UUID v7 in JobLogger. Ensure queue scheduling uses application timezone for consistency.

13 of 24 new or added lines in 5 files covered. (54.17%)

63 existing lines in 4 files now uncovered.

1175 of 2087 relevant lines covered (56.3%)

4.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.0
/src/Queues/RequeueHelper.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Queues.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Jobs\Queues;
15

16
use Daycry\Jobs\Job;
17
use Daycry\Jobs\Libraries\DeadLetterQueue;
18
use Daycry\Jobs\Metrics\Metrics;
19
use Daycry\Jobs\Metrics\MetricsCollectorInterface;
20

21
/**
22
 * Coordinates finalization of a job execution.
23
 * Unified attempts semantics:
24
 *  - attempts starts at 0 (never executed)
25
 *  - each completed execution cycle (success OR failure) increments attempts exactly once
26
 *  - on failure the increment still reflects that one full run happened before requeue
27
 *  - requeued jobs therefore arrive at the worker with attempts >= 1
28
 * Integrates Dead Letter Queue for permanently failed jobs.
29
 */
30
final class RequeueHelper
31
{
32
    private ?DeadLetterQueue $dlq = null;
33

34
    public function __construct(private ?MetricsCollectorInterface $metrics = null)
35
    {
36
    }
4✔
37

38
    public function finalize(Job $job, JobEnvelope $envelope, callable $removeFn, bool $success): void
39
    {
40
        // Lazy resolve metrics if not injected (Option B hybrid DI + facade fallback)
41
        if ($this->metrics === null) {
4✔
42
            $this->metrics = Metrics::get();
1✔
43
        }
44

45
        // Lazy resolve DLQ
46
        if ($this->dlq === null) {
4✔
47
            $this->dlq = new DeadLetterQueue();
4✔
48
        }
49

50
        // Authoritative increment for this execution cycle (success or failure)
51
        $job->addAttempt();
4✔
52

53
        if ($success) {
4✔
54
            $removeFn($job, false);
2✔
55
            $this->metrics?->increment('jobs_succeeded', 1, ['queue' => $envelope->queue]);
2✔
56

57
            return;
2✔
58
        }
59

60
        // Failure: check if we should requeue based on maxRetries
61
        $maxRetries     = $job->getMaxRetries();
2✔
62
        $currentAttempt = $job->getAttempt();
2✔
63

64
        // If maxRetries is null, do NOT requeue (fail immediately)
65
        // If maxRetries is set, requeue only if attempts < maxRetries + 1 (first attempt + retries)
66
        $shouldRequeue = ($maxRetries !== null) && ($currentAttempt < ($maxRetries + 1));
2✔
67

68
        if ($shouldRequeue) {
2✔
69
            $removeFn($job, true); // Requeue
2✔
70
            $this->metrics?->increment('jobs_failed', 1, ['queue' => $envelope->queue]);
2✔
71
            $this->metrics?->increment('jobs_requeued', 1, ['queue' => $envelope->queue]);
2✔
72
        } else {
UNCOV
73
            $removeFn($job, false); // Mark as failed permanently
×
UNCOV
74
            $this->metrics?->increment('jobs_failed', 1, ['queue' => $envelope->queue]);
×
UNCOV
75
            $this->metrics?->increment('jobs_failed_permanently', 1, ['queue' => $envelope->queue]);
×
76

77
            // Move to Dead Letter Queue if configured
UNCOV
78
            $this->dlq->store(
×
UNCOV
79
                $job,
×
UNCOV
80
                'Max retries exceeded',
×
UNCOV
81
                $currentAttempt,
×
UNCOV
82
            );
×
83
        }
84
    }
85
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc