-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathCalculateRootJobStatusService.php
131 lines (114 loc) · 3.44 KB
/
CalculateRootJobStatusService.php
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
<?php
namespace Enqueue\JobQueue;
use Doctrine\Common\Collections\Collection;
use Enqueue\JobQueue\Doctrine\JobStorage;
class CalculateRootJobStatusService
{
/**
* @var JobStorage
*/
private $jobStorage;
/**
* @param JobStorage $jobStorage
*/
public function __construct(JobStorage $jobStorage)
{
$this->jobStorage = $jobStorage;
}
/**
* @param Job $job
*
* @return bool true if root job was stopped
*/
public function calculate(Job $job)
{
$rootJob = $job->isRoot() ? $job : $job->getRootJob();
$stopStatuses = [Job::STATUS_SUCCESS, Job::STATUS_FAILED, Job::STATUS_CANCELLED];
if (in_array($rootJob->getStatus(), $stopStatuses, true)) {
return;
}
$rootStopped = false;
$this->jobStorage->saveJob($rootJob, function (Job $rootJob) use ($stopStatuses, &$rootStopped) {
if (in_array($rootJob->getStatus(), $stopStatuses, true)) {
return;
}
$childJobs = $rootJob->getChildJobs();
if ($childJobs instanceof Collection) {
$childJobs = $childJobs->toArray();
}
$status = $this->calculateRootJobStatus($childJobs);
$rootJob->setStatus($status);
if (in_array($status, $stopStatuses, true)) {
$rootStopped = true;
if (!$rootJob->getStoppedAt()) {
$rootJob->setStoppedAt(new \DateTime());
}
}
});
return $rootStopped;
}
/**
* @param Job[] $jobs
*
* @return string
*/
protected function calculateRootJobStatus(array $jobs)
{
$new = 0;
$running = 0;
$cancelled = 0;
$failed = 0;
$success = 0;
foreach ($jobs as $job) {
switch ($job->getStatus()) {
case Job::STATUS_NEW:
$new++;
break;
case Job::STATUS_RUNNING:
$running++;
break;
case Job::STATUS_CANCELLED:
$cancelled++;
break;
case Job::STATUS_FAILED:
$failed++;
break;
case Job::STATUS_SUCCESS:
$success++;
break;
default:
throw new \LogicException(sprintf(
'Got unsupported job status: id: "%s" status: "%s"',
$job->getId(),
$job->getStatus()
));
}
}
return $this->getRootJobStatus($new, $running, $cancelled, $failed, $success);
}
/**
* @param int $new
* @param int $running
* @param int $cancelled
* @param int $failed
* @param int $success
*
* @return string
*/
protected function getRootJobStatus($new, $running, $cancelled, $failed, $success)
{
$status = Job::STATUS_NEW;
if (!$new && !$running) {
if ($cancelled) {
$status = Job::STATUS_CANCELLED;
} elseif ($failed) {
$status = Job::STATUS_FAILED;
} else {
$status = Job::STATUS_SUCCESS;
}
} elseif ($running || $cancelled || $failed || $success) {
$status = Job::STATUS_RUNNING;
}
return $status;
}
}