1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Model; |
4
|
|
|
|
5
|
|
|
class Job |
6
|
|
|
{ |
7
|
|
|
const STATUS_SUCCESS = 'success'; |
8
|
|
|
const STATUS_ERROR = 'error'; |
9
|
|
|
const STATUS_NEW = 'new'; |
10
|
|
|
|
11
|
|
|
protected $id; |
12
|
|
|
protected $workerName; |
13
|
|
|
protected $className; |
14
|
|
|
protected $method; |
15
|
|
|
protected $args; |
16
|
|
|
protected $batch; |
17
|
|
|
protected $status; |
18
|
|
|
protected $message; |
19
|
|
|
protected $priority; |
20
|
|
|
protected $crcHash; |
21
|
|
|
protected $locked; |
22
|
|
|
protected $lockedAt; |
23
|
|
|
protected $when; |
24
|
|
|
protected $expire; |
25
|
|
|
protected $createdAt; |
26
|
|
|
protected $updatedAt; |
27
|
|
|
protected $delay; |
28
|
|
|
protected $startedAt; |
29
|
|
|
protected $finishedAt; |
30
|
|
|
protected $maxDuration; |
31
|
|
|
protected $elapsed; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var JobManagerInterface |
35
|
|
|
*/ |
36
|
|
|
protected $jobManager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return |
40
|
|
|
*/ |
41
|
|
|
public function getMessage() |
42
|
|
|
{ |
43
|
|
|
return $this->message; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $message |
48
|
|
|
*/ |
49
|
|
|
public function setMessage($message) |
50
|
|
|
{ |
51
|
|
|
$this->message = $message; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return $status |
|
|
|
|
56
|
|
|
*/ |
57
|
|
|
public function getStatus() |
58
|
|
|
{ |
59
|
|
|
return $this->status; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return $locked |
|
|
|
|
64
|
|
|
*/ |
65
|
|
|
public function getLocked() |
66
|
|
|
{ |
67
|
|
|
return $this->locked; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return $lockedAt |
|
|
|
|
72
|
|
|
*/ |
73
|
|
|
public function getLockedAt() |
74
|
|
|
{ |
75
|
|
|
return $this->lockedAt; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return $expire |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
public function getExpire() |
82
|
|
|
{ |
83
|
|
|
return $this->expire; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $status |
88
|
|
|
*/ |
89
|
|
|
public function setStatus($status) |
90
|
|
|
{ |
91
|
|
|
$this->status = $status; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $locked |
96
|
|
|
*/ |
97
|
|
|
public function setLocked($locked) |
98
|
|
|
{ |
99
|
|
|
$this->locked = $locked; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $lockedAt |
104
|
|
|
*/ |
105
|
|
|
public function setLockedAt($lockedAt) |
106
|
|
|
{ |
107
|
|
|
$this->lockedAt = $lockedAt; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param $expire |
112
|
|
|
*/ |
113
|
|
|
public function setExpire($expire) |
114
|
|
|
{ |
115
|
|
|
$this->expire = $expire; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return $id |
|
|
|
|
120
|
|
|
*/ |
121
|
|
|
public function getId() |
122
|
|
|
{ |
123
|
|
|
return $this->id; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return $workerName |
|
|
|
|
128
|
|
|
*/ |
129
|
|
|
public function getWorkerName() |
130
|
|
|
{ |
131
|
|
|
return $this->workerName; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return $className |
|
|
|
|
136
|
|
|
*/ |
137
|
|
|
public function getClassName() |
138
|
|
|
{ |
139
|
|
|
return $this->className; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return $method |
|
|
|
|
144
|
|
|
*/ |
145
|
|
|
public function getMethod() |
146
|
|
|
{ |
147
|
|
|
return $this->method; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function getArgs() |
154
|
|
|
{ |
155
|
|
|
return $this->args; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return $batch |
|
|
|
|
160
|
|
|
*/ |
161
|
|
|
public function getBatch() |
162
|
|
|
{ |
163
|
|
|
return $this->batch; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return $priority |
|
|
|
|
168
|
|
|
*/ |
169
|
|
|
public function getPriority() |
170
|
|
|
{ |
171
|
|
|
return $this->priority; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return $crcHash |
|
|
|
|
176
|
|
|
*/ |
177
|
|
|
public function getCrcHash() |
178
|
|
|
{ |
179
|
|
|
return $this->crcHash; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return $when |
|
|
|
|
184
|
|
|
*/ |
185
|
|
|
public function getWhen() |
186
|
|
|
{ |
187
|
|
|
return $this->when; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return $createdAt |
|
|
|
|
192
|
|
|
*/ |
193
|
|
|
public function getCreatedAt() |
194
|
|
|
{ |
195
|
|
|
return $this->createdAt; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return $updatedAt |
|
|
|
|
200
|
|
|
*/ |
201
|
|
|
public function getUpdatedAt() |
202
|
|
|
{ |
203
|
|
|
return $this->updatedAt; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return $jobManager |
|
|
|
|
208
|
|
|
*/ |
209
|
|
|
public function getJobManager() |
210
|
|
|
{ |
211
|
|
|
return $this->jobManager; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param $id |
216
|
|
|
*/ |
217
|
|
|
public function setId($id) |
218
|
|
|
{ |
219
|
|
|
$this->id = $id; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param $workerName |
224
|
|
|
*/ |
225
|
|
|
public function setWorkerName($workerName) |
226
|
|
|
{ |
227
|
|
|
$this->workerName = $workerName; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param string $className |
232
|
|
|
*/ |
233
|
|
|
public function setClassName($className) |
234
|
|
|
{ |
235
|
|
|
$this->className = $className; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param $method |
240
|
|
|
*/ |
241
|
|
|
public function setMethod($method) |
242
|
|
|
{ |
243
|
|
|
$this->method = $method; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return mixed |
248
|
|
|
*/ |
249
|
|
|
public function getStartedAt() |
250
|
|
|
{ |
251
|
|
|
return $this->startedAt; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param mixed $startedAt |
256
|
|
|
*/ |
257
|
|
|
public function setStartedAt(\DateTime $startedAt) |
258
|
|
|
{ |
259
|
|
|
$this->startedAt = $startedAt; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return mixed |
264
|
|
|
*/ |
265
|
|
|
public function getFinishedAt() |
266
|
|
|
{ |
267
|
|
|
return $this->finishedAt; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @param mixed $finishedAt |
272
|
|
|
*/ |
273
|
|
|
public function setFinishedAt($finishedAt) |
274
|
|
|
{ |
275
|
|
|
$this->finishedAt = $finishedAt; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return mixed |
280
|
|
|
*/ |
281
|
|
|
public function getMaxDuration() |
282
|
|
|
{ |
283
|
|
|
return $this->maxDuration; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @param mixed $maxDuration |
288
|
|
|
*/ |
289
|
|
|
public function setMaxDuration($maxDuration) |
290
|
|
|
{ |
291
|
|
|
$this->maxDuration = $maxDuration; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param $args |
296
|
|
|
*/ |
297
|
|
|
public function setArgs($args) |
298
|
|
|
{ |
299
|
|
|
if (!$this->recursiveValidArgs($args)) { |
300
|
|
|
throw new \Exception('Args must not contain object'); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$this->args = $args; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
protected function recursiveValidArgs($args) |
307
|
|
|
{ |
308
|
|
|
if (is_array($args)) { |
309
|
|
|
foreach ($args as $key => $value) { |
310
|
|
|
if (!$this->recursiveValidArgs($value)) { |
311
|
|
|
return false; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return true; |
316
|
|
|
} else { |
317
|
|
|
return !is_object($args); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @param $batch |
323
|
|
|
*/ |
324
|
|
|
public function setBatch($batch) |
325
|
|
|
{ |
326
|
|
|
$this->batch = $batch; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param $priority |
331
|
|
|
*/ |
332
|
|
|
public function setPriority($priority) |
333
|
|
|
{ |
334
|
|
|
$this->priority = $priority; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @param $crcHash |
339
|
|
|
*/ |
340
|
|
|
public function setCrcHash($crcHash) |
341
|
|
|
{ |
342
|
|
|
$this->crcHash = $crcHash; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @param $when |
347
|
|
|
*/ |
348
|
|
|
public function setWhen($when) |
349
|
|
|
{ |
350
|
|
|
$this->when = $when; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param $createdAt |
355
|
|
|
*/ |
356
|
|
|
public function setCreatedAt($createdAt) |
357
|
|
|
{ |
358
|
|
|
$this->createdAt = $createdAt; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* @param $updatedAt |
363
|
|
|
*/ |
364
|
|
|
public function setUpdatedAt($updatedAt) |
365
|
|
|
{ |
366
|
|
|
$this->updatedAt = $updatedAt; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* @param $jobManager |
371
|
|
|
*/ |
372
|
|
|
public function setJobManager($jobManager) |
373
|
|
|
{ |
374
|
|
|
$this->jobManager = $jobManager; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
protected $worker; |
378
|
|
|
|
379
|
|
|
public function __construct(Worker $worker = null, $batch = false, $priority = 10, \DateTime $when = null) |
380
|
|
|
{ |
381
|
|
|
$this->worker = $worker; |
382
|
|
|
if ($worker) { |
383
|
|
|
$this->jobManager = $worker->getJobManager(); |
384
|
|
|
$this->className = get_class($worker); |
385
|
|
|
$this->workerName = $worker->getName(); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
$this->when = $when; |
389
|
|
|
$this->batch = $batch ? true : false; |
390
|
|
|
$this->priority = $priority; |
391
|
|
|
$this->status = self::STATUS_NEW; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
public function __call($method, $args) |
395
|
|
|
{ |
396
|
|
|
$this->method = $method; |
397
|
|
|
$this->setArgs($args); |
398
|
|
|
$this->createdAt = new \DateTime(); |
399
|
|
|
$this->updatedAt = new \DateTime(); |
400
|
|
|
|
401
|
|
|
// Make sure the method exists - job should not be created |
402
|
|
|
if (!method_exists($this->worker, $method)) { |
403
|
|
|
throw new \Exception("{$this->className}->{$method}() does not exist"); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
$this->jobManager->save($this); |
407
|
|
|
|
408
|
|
|
return $this; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @return $delay |
|
|
|
|
413
|
|
|
*/ |
414
|
|
|
public function getDelay() |
415
|
|
|
{ |
416
|
|
|
return $this->delay; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* @return $worker |
|
|
|
|
421
|
|
|
*/ |
422
|
|
|
public function getWorker() |
423
|
|
|
{ |
424
|
|
|
return $this->worker; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* @param $delay |
429
|
|
|
*/ |
430
|
|
|
public function setDelay($delay) |
431
|
|
|
{ |
432
|
|
|
$this->delay = $delay; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* @param Worker $worker |
437
|
|
|
*/ |
438
|
|
|
public function setWorker($worker) |
439
|
|
|
{ |
440
|
|
|
$this->worker = $worker; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* @return $elapsed |
|
|
|
|
445
|
|
|
*/ |
446
|
|
|
public function getElapsed() |
447
|
|
|
{ |
448
|
|
|
return $this->elapsed; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* @param $elapsed |
453
|
|
|
*/ |
454
|
|
|
public function setElapsed($elapsed) |
455
|
|
|
{ |
456
|
|
|
$this->elapsed = $elapsed; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
public function toMessage() |
460
|
|
|
{ |
461
|
|
|
$arr = array( |
462
|
|
|
'worker' => $this->getWorkerName(), |
463
|
|
|
'args' => $this->getArgs(), |
464
|
|
|
'method' => $this->getMethod(), |
465
|
|
|
); |
466
|
|
|
|
467
|
|
|
return json_encode($arr); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
public function fromMessage($message) |
471
|
|
|
{ |
472
|
|
|
$arr = json_decode($message, true); |
473
|
|
|
$this->setWorkerName($arr['worker']); |
474
|
|
|
$this->setArgs($arr['args']); |
475
|
|
|
$this->setMethod($arr['method']); |
476
|
|
|
} |
477
|
|
|
} |
478
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.