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

daycry / jobs / 24850441053

23 Apr 2026 05:54PM UTC coverage: 52.404% (-1.5%) from 53.938%
24850441053

push

github

daycry
Fixes

104 of 219 new or added lines in 42 files covered. (47.49%)

14 existing lines in 9 files now uncovered.

1210 of 2309 relevant lines covered (52.4%)

4.37 hits per line

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

36.59
/src/Traits/StateTrait.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 Daycry\Jobs\Execution\ExecutionResult;
17
use Daycry\Jobs\Job;
18

19
/**
20
 * Unified trait for job state management and notifications.
21
 * Combines StatusTrait and NotificableTrait functionality.
22
 *
23
 * @mixin Job
24
 */
25
trait StateTrait
26
{
27
    protected bool $enabled         = true;
28
    protected bool $notifyOnFailure = false;
29
    protected bool $notifyOnSuccess = false;
30

31
    // ============================================================
32
    // Status & Single Instance (formerly StatusTrait)
33
    // ============================================================
34

35
    private function runningCacheKey(): string
36
    {
37
        return 'job_running_' . $this->getName();
1✔
38
    }
39

40
    public function saveRunningFlag(): bool
41
    {
42
        $cache = service('cache');
×
43
        // Use finite TTL to prevent deadlock if the job crashes without clearing the flag.
44
        // Default: job timeout + 60s buffer, or 600s if no timeout configured.
NEW
45
        $timeout = method_exists($this, 'getTimeout') ? ($this->getTimeout() ?? 0) : 0;
×
NEW
46
        $ttl     = $timeout > 0 ? $timeout + 60 : 600;
×
47

NEW
48
        return $cache->save($this->runningCacheKey(), true, $ttl);
×
49
    }
50

51
    public function isRunning(): bool
52
    {
53
        $cache = service('cache');
1✔
54

55
        return (bool) $cache->get($this->runningCacheKey());
1✔
56
    }
57

58
    public function clearRunningFlag(): bool
59
    {
60
        $cache = service('cache');
×
61

62
        return $cache->delete($this->runningCacheKey());
×
63
    }
64

65
    public function disable(): self
66
    {
67
        $this->enabled = false;
1✔
68

69
        return $this;
1✔
70
    }
71

72
    public function isEnabled(): bool
73
    {
74
        return $this->enabled;
3✔
75
    }
76

77
    // ============================================================
78
    // Notifications (formerly NotificableTrait)
79
    // ============================================================
80

81
    public function notifyOnFailure(bool $notify = true): self
82
    {
83
        $this->notifyOnFailure = $notify;
1✔
84

85
        return $this;
1✔
86
    }
87

88
    public function notifyOnSuccess(bool $notify = true): self
89
    {
90
        $this->notifyOnSuccess = $notify;
1✔
91

92
        return $this;
1✔
93
    }
94

95
    public function shouldNotifyOnFailure(): bool
96
    {
97
        return $this->notifyOnFailure;
14✔
98
    }
99

100
    public function shouldNotifyOnSuccess(): bool
101
    {
102
        return $this->notifyOnSuccess;
22✔
103
    }
104

105
    public function notifyOnCompletion(bool $notify = true): self
106
    {
107
        $this->notifyOnFailure = $notify;
1✔
108
        $this->notifyOnSuccess = $notify;
1✔
109

110
        return $this;
1✔
111
    }
112

113
    public function notify(ExecutionResult $result): bool
114
    {
115
        $email  = service('email');
×
116
        $parser = service('parser');
×
117

118
        $content          = $result->success ? $result->output : $result->error;
×
119
        $normalizedOutput = null;
×
NEW
120
        if (is_object($content)) {
×
121
            $normalizedOutput = json_encode($content);
×
122
        } elseif ($content !== null) {
×
NEW
123
            $normalizedOutput = $content;
×
124
        }
125

126
        $email->setMailType('html');
×
127
        $email->setFrom(config('Jobs')->from, config('Jobs')->fromName);
×
128
        $email->setTo(config('Jobs')->to);
×
129
        $email->setSubject($parser->setData(['job' => $this->getName()])->renderString('Job {job} just finished running.'));
×
130
        $email->setMessage($parser->setData([
×
NEW
131
            'name'     => esc($this->getName()),
×
NEW
132
            'runStart' => '',
×
NEW
133
            'duration' => '',
×
NEW
134
            'output'   => esc($result->success ? ($normalizedOutput ?? '') : ''),
×
NEW
135
            'error'    => esc($result->success ? ('') : $normalizedOutput ?? ''),
×
UNCOV
136
        ])->render(config('Jobs')->emailNotificationView));
×
137

138
        return $email->send();
×
139
    }
140
}
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