|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Manager\JobManagerInterface; |
|
6
|
|
|
use Dtc\QueueBundle\Util\Util; |
|
7
|
|
|
|
|
8
|
|
|
abstract class Worker |
|
9
|
|
|
{ |
|
10
|
|
|
const RESULT_SUCCESS = 0; |
|
11
|
|
|
const RESULT_FAILURE = 1; |
|
12
|
|
|
|
|
13
|
|
|
/** @var JobManagerInterface */ |
|
14
|
|
|
private $jobManager; |
|
15
|
|
|
private $currentJob; |
|
16
|
|
|
|
|
17
|
4 |
|
public function setCurrentJob(BaseJob $job) |
|
18
|
|
|
{ |
|
19
|
4 |
|
$this->currentJob = $job; |
|
20
|
4 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function getCurrentJob() |
|
23
|
|
|
{ |
|
24
|
|
|
return $this->currentJob; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param JobManagerInterface $jobManager |
|
29
|
|
|
*/ |
|
30
|
28 |
|
public function setJobManager(JobManagerInterface $jobManager) |
|
31
|
|
|
{ |
|
32
|
28 |
|
$this->jobManager = $jobManager; |
|
33
|
28 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return |
|
37
|
|
|
*/ |
|
38
|
57 |
|
public function getJobManager() |
|
39
|
|
|
{ |
|
40
|
57 |
|
return $this->jobManager; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param int|null $time |
|
45
|
|
|
* @param bool $batch |
|
46
|
|
|
* @param int|null $priority |
|
47
|
|
|
*/ |
|
48
|
15 |
|
public function at($time = null, $batch = false, $priority = null) |
|
49
|
|
|
{ |
|
50
|
15 |
|
$timeU = $time; |
|
51
|
15 |
|
if (null === $time) { |
|
52
|
|
|
$timeU = Util::getMicrotimeStr(); |
|
53
|
15 |
|
} elseif (false === strpos(strval($time), '.')) { |
|
54
|
2 |
|
$timeU = strval($time).'.000000'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
15 |
|
$dateTime = \DateTime::createFromFormat('U.u', (string) $timeU); |
|
58
|
15 |
|
if (!$dateTime) { |
|
59
|
|
|
throw new \InvalidArgumentException("Invalid time: $time".($timeU != $time ? " - (micro: $timeU)" : '')); |
|
60
|
|
|
} |
|
61
|
15 |
|
$jobClass = $this->jobManager->getJobClass(); |
|
62
|
|
|
|
|
63
|
15 |
|
return new $jobClass($this, $batch, $priority, $dateTime); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param int $delay Amount of time to delay |
|
68
|
|
|
* @param int|null $priority |
|
69
|
|
|
*/ |
|
70
|
12 |
|
public function later($delay = 0, $priority = null) |
|
71
|
|
|
{ |
|
72
|
12 |
|
return $this->batchOrLaterDelay($delay, false, $priority); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
13 |
|
public function batchOrLaterDelay($delay = 0, $batch = false, $priority = null) |
|
76
|
|
|
{ |
|
77
|
13 |
|
$job = $this->at(microtime(true) + $delay, $batch, $priority); |
|
78
|
13 |
|
$job->setDelay($delay); |
|
79
|
|
|
|
|
80
|
13 |
|
return $job; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param int $delay Amount of time to delay |
|
85
|
|
|
* @param int|null $priority |
|
86
|
|
|
*/ |
|
87
|
3 |
|
public function batchLater($delay = 0, $priority = null) |
|
88
|
|
|
{ |
|
89
|
3 |
|
return $this->batchOrLaterDelay($delay, true, $priority); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param int|null $time |
|
94
|
|
|
* @param int|null $priority |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function batchAt($time = null, $priority = null) |
|
97
|
|
|
{ |
|
98
|
1 |
|
return $this->at($time, true, $priority); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
abstract public function getName(); |
|
102
|
|
|
} |
|
103
|
|
|
|