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