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

daycry / jobs / 24568210769

05 Apr 2026 08:48AM UTC coverage: 53.938% (-2.2%) from 56.164%
24568210769

push

github

daycry
Optimize

50 of 192 new or added lines in 14 files covered. (26.04%)

3 existing lines in 3 files now uncovered.

1219 of 2260 relevant lines covered (53.94%)

4.42 hits per line

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

20.0
/src/Traits/ActivityTrait.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\Traits;
15

16
use CodeIgniter\I18n\Time;
17
use Cron\CronExpression;
18

19
/**
20
 * Adds retry/timeout metadata and schedule evaluation helpers to a Job.
21
 * Provides shouldRun() using cron expression & environment filtering, plus max retries and timeout accessors.
22
 */
23
trait ActivityTrait
24
{
25
    protected ?int $maxRetries = null;
26
    protected ?int $timeout    = null;
27

28
    public function maxRetries(int $retries): self
29
    {
30
        $this->maxRetries = $retries;
59✔
31

32
        return $this;
59✔
33
    }
34

35
    public function timeout(int $timeout): self
36
    {
37
        $this->timeout = $timeout;
58✔
38

39
        return $this;
58✔
40
    }
41

42
    public function getMaxRetries(): ?int
43
    {
44
        return $this->maxRetries ?? null;
31✔
45
    }
46

47
    /**
48
     * Get the timeout (in seconds) for this job.
49
     */
50
    public function getTimeout(): ?int
51
    {
52
        return $this->timeout ?? null;
29✔
53
    }
54

55
    public function shouldRun(?Time $testTime = null): bool
56
    {
57
        // Are we restricting to environments?
NEW
58
        if (! empty($this->environments) && ! $this->inEnvironment($_SERVER['CI_ENVIRONMENT'] ?? 'production')) {
×
59
            return false;
×
60
        }
61

62
        $cron = new CronExpression($this->getExpression());
×
63

64
        $testTime = ($testTime) ?: 'now';
×
65

66
        return $cron->isDue($testTime, config('App')->appTimezone);
×
67
    }
68

69
    /**
70
     * Returns the date this was last ran, using the configured logger handler.
71
     *
72
     * @return string|Time
73
     */
74
    public function lastRun(): string|Time
75
    {
NEW
76
        $config = config('Jobs');
×
77

NEW
78
        if ($config->logPerformance === false) {
×
UNCOV
79
            return '--';
×
80
        }
81

NEW
82
        $name = ($this->name ?? '') ?: $this->getName();
×
83

NEW
84
        $handler = $this->resolveLoggerHandler($config);
×
NEW
85
        if ($handler === null) {
×
NEW
86
            return '--';
×
87
        }
88

NEW
89
        return $handler->lastRun($name);
×
90
    }
91

92
    /**
93
     * Returns the last run time as a Time instance or null when not available.
94
     */
95
    public function getLastRunTime(): ?Time
96
    {
97
        $val = $this->lastRun();
×
98

99
        return $val instanceof Time ? $val : null;
×
100
    }
101

102
    /**
103
     * Resolve the configured logger handler instance (file or database).
104
     */
105
    private function resolveLoggerHandler(object $config): ?object
106
    {
NEW
107
        if (empty($config->log) || ! isset($config->loggers[$config->log])) {
×
NEW
108
            return null;
×
109
        }
110

NEW
111
        $class = $config->loggers[$config->log];
×
112

NEW
113
        if (! class_exists($class)) {
×
NEW
114
            return null;
×
115
        }
116

117
        try {
NEW
118
            $handler = new $class();
×
NEW
119
        } catch (\Throwable) {
×
NEW
120
            return null;
×
121
        }
122

NEW
123
        return method_exists($handler, 'lastRun') ? $handler : null;
×
124
    }
125
}
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