1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\QueueBundle\Job; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SfCod\QueueBundle\Base\InteractWithTimeTrait; |
7
|
|
|
use SfCod\QueueBundle\Base\JobInterface; |
8
|
|
|
use SfCod\QueueBundle\Base\JobResolverInterface; |
9
|
|
|
use SfCod\QueueBundle\Entity\Job; |
10
|
|
|
use SfCod\QueueBundle\Queue\QueueInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Job |
14
|
|
|
* |
15
|
|
|
* @author Alexey Orlov <[email protected]> |
16
|
|
|
* @author Virchenko Maksim <[email protected]> |
17
|
|
|
* |
18
|
|
|
* @package SfCod\QueueBundle\Base |
19
|
|
|
*/ |
20
|
|
|
class JobContract implements JobContractInterface |
21
|
|
|
{ |
22
|
|
|
use InteractWithTimeTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The job handler instance. |
26
|
|
|
* |
27
|
|
|
* @var JobInterface |
28
|
|
|
*/ |
29
|
|
|
protected $instance; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Indicates if the job has been deleted. |
33
|
|
|
* |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $deleted = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Indicates if the job has been released. |
40
|
|
|
* |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
protected $released = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Indicates if the job has failed. |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
protected $failed = false; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The name of the connection the job belongs to. |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $connectionName; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The name of the queue the job belongs to. |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
protected $queue; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Job resolver |
68
|
|
|
* |
69
|
|
|
* @var JobResolverInterface |
70
|
|
|
*/ |
71
|
|
|
protected $resolver; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The database queue instance. |
75
|
|
|
* |
76
|
|
|
* @var QueueInterface |
77
|
|
|
*/ |
78
|
|
|
protected $database; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The database job payload. |
82
|
|
|
* |
83
|
|
|
* @var Job |
84
|
|
|
*/ |
85
|
|
|
protected $job; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create a new job instance. |
89
|
|
|
* |
90
|
|
|
* @param JobResolverInterface $resolver |
91
|
|
|
* @param QueueInterface $database |
92
|
|
|
* @param Job $job |
93
|
|
|
*/ |
94
|
|
|
public function __construct(JobResolverInterface $resolver, QueueInterface $database, Job $job) |
95
|
|
|
{ |
96
|
|
|
$this->resolver = $resolver; |
97
|
|
|
$this->database = $database; |
98
|
|
|
$this->job = $job; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Fire the job. |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public function fire() |
107
|
|
|
{ |
108
|
|
|
$handler = $this->resolve($this->getName()); |
109
|
|
|
|
110
|
|
|
$this->instance = $handler->fire($this, $this->getData()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Delete the job from the queue. |
115
|
|
|
*/ |
116
|
|
|
public function delete() |
117
|
|
|
{ |
118
|
|
|
$this->deleted = true; |
119
|
|
|
|
120
|
|
|
$this->database->deleteReserved($this->job->getQueue(), $this->getJobId()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Process an exception that caused the job to fail. |
125
|
|
|
* |
126
|
|
|
* @param Exception $e |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function failed($e) |
131
|
|
|
{ |
132
|
|
|
$this->markAsFailed(); |
133
|
|
|
|
134
|
|
|
if (method_exists($this->instance = $this->resolve($this->getName()), 'failed')) { |
135
|
|
|
$this->instance->failed($this->getData(), $e); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Determine if the job has been deleted. |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
|
public function isDeleted(): bool |
145
|
|
|
{ |
146
|
|
|
return $this->deleted; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Release the job back into the queue. |
151
|
|
|
* |
152
|
|
|
* @param int $delay |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function release(int $delay = 0) |
157
|
|
|
{ |
158
|
|
|
$this->released = true; |
159
|
|
|
|
160
|
|
|
$this->database->deleteReserved($this->job->getQueue(), $this->getJobId()); |
161
|
|
|
$this->database->release($this, $delay); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Determine if the job was released back into the queue. |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function isReleased(): bool |
170
|
|
|
{ |
171
|
|
|
return $this->released; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Determine if the job has been deleted or released. |
176
|
|
|
* |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function isDeletedOrReleased(): bool |
180
|
|
|
{ |
181
|
|
|
return $this->isDeleted() || $this->isReleased(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Determine if the job has been marked as a failure. |
186
|
|
|
* |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
|
|
public function hasFailed(): bool |
190
|
|
|
{ |
191
|
|
|
return $this->failed; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Mark the job as "failed". |
196
|
|
|
* |
197
|
|
|
* @return void |
198
|
|
|
*/ |
199
|
|
|
public function markAsFailed() |
200
|
|
|
{ |
201
|
|
|
$this->failed = true; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get the decoded body of the job. |
206
|
|
|
* |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
public function payload(): array |
210
|
|
|
{ |
211
|
|
|
return $this->job->getPayload(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get the number of times to attempt a job. |
216
|
|
|
* |
217
|
|
|
* @return int|null |
218
|
|
|
*/ |
219
|
|
|
public function maxTries(): ?int |
220
|
|
|
{ |
221
|
|
|
return $this->payload()['maxTries'] ?? null; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get the number of seconds the job can run. |
226
|
|
|
* |
227
|
|
|
* @return int|null |
228
|
|
|
*/ |
229
|
|
|
public function timeout(): ?int |
230
|
|
|
{ |
231
|
|
|
return $this->payload()['timeout'] ?? null; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Get the timestamp indicating when the job should timeout. |
236
|
|
|
* |
237
|
|
|
* @return int|null |
238
|
|
|
*/ |
239
|
|
|
public function timeoutAt(): ?int |
240
|
|
|
{ |
241
|
|
|
return $this->payload()['timeoutAt'] ?? null; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get the name of the queued job class. |
246
|
|
|
* |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
public function getName(): string |
250
|
|
|
{ |
251
|
|
|
return $this->payload()['job']; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Get data of queued job. |
256
|
|
|
* |
257
|
|
|
* @return array |
258
|
|
|
*/ |
259
|
|
|
public function getData(): array |
260
|
|
|
{ |
261
|
|
|
return $this->payload()['data']; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Get the name of the connection the job belongs to. |
266
|
|
|
* |
267
|
|
|
* @return string |
268
|
|
|
*/ |
269
|
|
|
public function getConnectionName(): ?string |
270
|
|
|
{ |
271
|
|
|
return $this->connectionName ?? $this->database->getConnectionName(); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Get the name of the queue the job belongs to. |
276
|
|
|
* |
277
|
|
|
* @return string |
278
|
|
|
*/ |
279
|
|
|
public function getQueue(): ?string |
280
|
|
|
{ |
281
|
|
|
return $this->job->getQueue(); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Get the number of times the job has been attempted. |
286
|
|
|
* |
287
|
|
|
* @return int |
288
|
|
|
*/ |
289
|
|
|
public function attempts(): int |
290
|
|
|
{ |
291
|
|
|
return $this->job->getAttempts(); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Check if job reserved |
296
|
|
|
* |
297
|
|
|
* @return bool |
298
|
|
|
*/ |
299
|
|
|
public function reserved(): bool |
300
|
|
|
{ |
301
|
|
|
return $this->job->isReserved(); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Get reserved at time |
306
|
|
|
* |
307
|
|
|
* @return int|null |
308
|
|
|
*/ |
309
|
|
|
public function reservedAt(): ?int |
310
|
|
|
{ |
311
|
|
|
return $this->job->getReservedAt(); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Get the job identifier. |
316
|
|
|
* |
317
|
|
|
* @return string |
318
|
|
|
*/ |
319
|
|
|
public function getJobId(): string |
320
|
|
|
{ |
321
|
|
|
return $this->job->getId(); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Get the raw body string for the job. |
326
|
|
|
* |
327
|
|
|
* @return string |
328
|
|
|
*/ |
329
|
|
|
public function getRawBody(): string |
330
|
|
|
{ |
331
|
|
|
return json_encode($this->job->getPayload()); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Resolve job |
336
|
|
|
* |
337
|
|
|
* @param string $class |
338
|
|
|
* |
339
|
|
|
* @return JobInterface |
340
|
|
|
*/ |
341
|
|
|
protected function resolve(string $class): JobInterface |
342
|
|
|
{ |
343
|
|
|
return $this->resolver->resolve($class); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.