1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Manager; |
4
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Model\BaseJob; |
6
|
|
|
use Dtc\QueueBundle\Model\RetryableJob; |
7
|
|
|
use Dtc\QueueBundle\Model\Job; |
8
|
|
|
use Dtc\QueueBundle\Model\JobTiming; |
9
|
|
|
|
10
|
|
|
abstract class RetryableJobManager extends AbstractJobManager |
11
|
|
|
{ |
12
|
|
|
protected $defaultMaxRetries; |
13
|
|
|
protected $defaultMaxFailures; |
14
|
|
|
protected $defaultMaxExceptions; |
15
|
|
|
|
16
|
|
|
protected $autoRetryOnFailure; |
17
|
|
|
protected $autoRetryOnException; |
18
|
|
|
|
19
|
|
|
abstract protected function retryableSave(RetryableJob $job); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param RetryableJob $job |
23
|
|
|
* @param $retry bool |
24
|
|
|
* |
25
|
|
|
* @return |
26
|
|
|
*/ |
27
|
|
|
abstract protected function retryableSaveHistory(RetryableJob $job, $retry); |
28
|
|
|
|
29
|
76 |
|
public function save(Job $job) |
30
|
|
|
{ |
31
|
76 |
|
if (!$job instanceof RetryableJob) { |
32
|
3 |
|
throw new \InvalidArgumentException('Job is not instanceof '.RetryableJob::class); |
33
|
|
|
} |
34
|
|
|
|
35
|
76 |
|
if (!$job->getId()) { |
36
|
76 |
|
$this->setBaseRetryableJobDefaults($job); |
37
|
|
|
} |
38
|
76 |
|
$this->recordTiming($job); |
39
|
76 |
|
$job->setUpdatedAt(new \DateTime()); |
40
|
|
|
|
41
|
76 |
|
return $this->retryableSave($job); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return bool true if retry |
46
|
|
|
* |
47
|
|
|
* @param bool $retry true if the job was retried, false if not |
|
|
|
|
48
|
|
|
*/ |
49
|
20 |
|
public function saveHistory(Job $job) |
50
|
|
|
{ |
51
|
20 |
|
if (!$job instanceof RetryableJob) { |
52
|
3 |
|
throw new \InvalidArgumentException('job not instance of '.RetryableJob::class); |
53
|
|
|
} |
54
|
|
|
|
55
|
20 |
|
switch ($job->getStatus()) { |
56
|
20 |
|
case BaseJob::STATUS_FAILURE: |
57
|
9 |
|
return $this->retryableSaveHistory($job, $this->updateJobFailure($job)); |
58
|
11 |
|
case BaseJob::STATUS_EXCEPTION: |
59
|
6 |
|
return $this->retryableSaveHistory($job, $this->updateJobException($job)); |
60
|
|
|
} |
61
|
|
|
|
62
|
5 |
|
return $this->retryableSaveHistory($job, false); |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
private function updateJobException(RetryableJob $job) |
66
|
|
|
{ |
67
|
6 |
|
return $this->updateJobMax($job, 'Exceptions', RetryableJob::STATUS_MAX_EXCEPTIONS, $this->autoRetryOnException); |
68
|
|
|
} |
69
|
|
|
|
70
|
17 |
|
protected function updateJobMax(RetryableJob $job, $type, $maxStatus, $autoRetry) |
71
|
|
|
{ |
72
|
17 |
|
$setMethod = 'set'.$type; |
73
|
17 |
|
$getMethod = 'get'.$type; |
74
|
17 |
|
$getMax = 'getMax'.$type; |
75
|
17 |
|
$job->$setMethod(intval($job->$getMethod()) + 1); |
76
|
17 |
|
if (!$this->updateMaxStatus($job, $maxStatus, $job->$getMax(), $job->$getMethod()) && |
77
|
17 |
|
!$this->updateMaxStatus($job, RetryableJob::STATUS_MAX_RETRIES, $job->getMaxRetries(), $job->getRetries())) { |
|
|
|
|
78
|
16 |
|
if ($autoRetry) { |
79
|
14 |
|
return $this->resetRetryableJob($job); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
17 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
9 |
|
private function updateJobFailure(RetryableJob $job) |
87
|
|
|
{ |
88
|
9 |
|
return $this->updateJobMax($job, 'Failures', RetryableJob::STATUS_MAX_FAILURES, $this->autoRetryOnFailure); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Determine if we've hit a max retry condition. |
93
|
|
|
* |
94
|
|
|
* @param RetryableJob $job |
95
|
|
|
* @param $status |
96
|
|
|
* @param null $max |
97
|
|
|
* @param int $count |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
19 |
|
protected function updateMaxStatus(RetryableJob $job, $status, $max = null, $count = 0) |
102
|
|
|
{ |
103
|
19 |
|
if (null !== $max && $count >= $max) { |
104
|
17 |
|
$job->setStatus($status); |
105
|
|
|
|
106
|
17 |
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
18 |
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
14 |
|
protected function resetRetryableJob(RetryableJob $job) |
113
|
|
|
{ |
114
|
14 |
|
if ($this->resetJob($job)) { |
115
|
14 |
|
$this->getJobTimingManager()->recordTiming(JobTiming::STATUS_INSERT); |
116
|
|
|
|
117
|
14 |
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param RetryableJob $retryableJob |
|
|
|
|
125
|
|
|
* |
126
|
|
|
* @return bool true if the job was successfully "reset", i.e. re-queued |
127
|
|
|
*/ |
128
|
|
|
abstract protected function resetJob(RetryableJob $job); |
129
|
|
|
|
130
|
76 |
|
protected function setBaseRetryableJobDefaults(RetryableJob $job) |
131
|
|
|
{ |
132
|
76 |
|
if (null === $job->getMaxExceptions()) { |
133
|
43 |
|
$job->setMaxExceptions($this->defaultMaxExceptions); |
134
|
|
|
} |
135
|
|
|
|
136
|
76 |
|
if (null === $job->getMaxRetries()) { |
137
|
43 |
|
$job->setMaxRetries($this->defaultMaxRetries); |
138
|
|
|
} |
139
|
|
|
|
140
|
76 |
|
if (null === $job->getMaxFailures()) { |
141
|
43 |
|
$job->setMaxFailures($this->defaultMaxFailures); |
142
|
|
|
} |
143
|
|
|
|
144
|
76 |
|
if (null === $job->getCrcHash()) { |
145
|
76 |
|
$hashValues = array(get_class($job), $job->getMethod(), $job->getWorkerName(), $job->getArgs()); |
146
|
76 |
|
$crcHash = hash('sha256', serialize($hashValues)); |
147
|
76 |
|
$job->setCrcHash($crcHash); |
148
|
|
|
} |
149
|
76 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return int|null |
153
|
|
|
*/ |
154
|
6 |
|
public function getDefaultMaxRetries() |
155
|
|
|
{ |
156
|
6 |
|
return $this->defaultMaxRetries; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param int|null $defaultMaxRetry |
|
|
|
|
161
|
|
|
*/ |
162
|
6 |
|
public function setDefaultMaxRetries($defaultMaxRetries) |
163
|
|
|
{ |
164
|
6 |
|
$this->defaultMaxRetries = $defaultMaxRetries; |
165
|
6 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return int|null |
169
|
|
|
*/ |
170
|
6 |
|
public function getDefaultMaxFailures() |
171
|
|
|
{ |
172
|
6 |
|
return $this->defaultMaxFailures; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param int|null $defaultMaxFailure |
|
|
|
|
177
|
|
|
*/ |
178
|
6 |
|
public function setDefaultMaxFailures($defaultMaxFailures) |
179
|
|
|
{ |
180
|
6 |
|
$this->defaultMaxFailures = $defaultMaxFailures; |
181
|
6 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
6 |
|
public function getAutoRetryOnFailure() |
187
|
|
|
{ |
188
|
6 |
|
return $this->autoRetryOnFailure; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param bool $autoRetryOnFailure |
193
|
|
|
*/ |
194
|
18 |
|
public function setAutoRetryOnFailure($autoRetryOnFailure) |
195
|
|
|
{ |
196
|
18 |
|
$this->autoRetryOnFailure = $autoRetryOnFailure; |
197
|
18 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
6 |
|
public function getAutoRetryOnException() |
203
|
|
|
{ |
204
|
6 |
|
return $this->autoRetryOnException; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param bool $autoRetryOnException |
209
|
|
|
*/ |
210
|
12 |
|
public function setAutoRetryOnException($autoRetryOnException) |
211
|
|
|
{ |
212
|
12 |
|
$this->autoRetryOnException = $autoRetryOnException; |
213
|
12 |
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return int|null |
217
|
|
|
*/ |
218
|
6 |
|
public function getDefaultMaxExceptions() |
219
|
|
|
{ |
220
|
6 |
|
return $this->defaultMaxExceptions; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param int|null $defaultMaxException |
|
|
|
|
225
|
|
|
*/ |
226
|
6 |
|
public function setDefaultMaxExceptions($defaultMaxExceptions) |
227
|
|
|
{ |
228
|
6 |
|
$this->defaultMaxExceptions = $defaultMaxExceptions; |
229
|
6 |
|
} |
230
|
|
|
|
231
|
76 |
|
protected function recordTiming(Job $job) |
232
|
|
|
{ |
233
|
76 |
|
$status = JobTiming::STATUS_INSERT; |
234
|
76 |
|
if ($job->getWhenAt() && $job->getWhenAt() > (new \DateTime())) { |
235
|
4 |
|
$status = JobTiming::STATUS_INSERT_DELAYED; |
236
|
|
|
} |
237
|
|
|
|
238
|
76 |
|
$this->getJobTimingManager()->recordTiming($status); |
239
|
76 |
|
} |
240
|
|
|
} |
241
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.