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 | 31 | public function setJobManager(JobManagerInterface $jobManager) |
|
28 | { |
||
29 | 31 | $this->jobManager = $jobManager; |
|
30 | 31 | } |
|
31 | |||
32 | /** |
||
33 | * @return |
||
34 | */ |
||
35 | 105 | public function getJobManager() |
|
36 | { |
||
37 | 105 | return $this->jobManager; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param int|null $time |
||
42 | * @param bool $batch |
||
43 | * @param int|null $priority |
||
44 | * @throws \Exception |
||
45 | 24 | */ |
|
46 | public function at($time = null, $batch = false, $priority = null) |
||
47 | 24 | { |
|
48 | 24 | $timeU = $time; |
|
49 | 1 | if (null === $time) { |
|
50 | 1 | $timeU = Util::getMicrotimeStr(); |
|
51 | 1 | $dateTime = \DateTime::createFromFormat( |
|
52 | 1 | 'U.u', |
|
53 | 1 | $timeU |
|
54 | ); |
||
55 | if (!$dateTime) { |
||
56 | 24 | throw new \Exception("Could not create DateTime object from $timeU"); |
|
57 | 24 | } |
|
58 | 24 | $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
|
59 | 24 | } else { |
|
60 | 24 | $localeInfo = localeconv(); |
|
61 | 2 | $decimalPoint = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : '.'; |
|
62 | $hasDecimalPoint = false !== strpos(strval($time), $decimalPoint); |
||
63 | $hasEnDecimalPoint = '.' === $decimalPoint ? $hasDecimalPoint : strpos(strval($time), '.'); |
||
64 | if (!$hasEnDecimalPoint) { |
||
65 | if ($hasDecimalPoint) { |
||
66 | $dateTime = \DateTime::createFromFormat( |
||
67 | 'U'.$decimalPoint.'u', |
||
68 | 2 | strval($timeU) |
|
69 | 2 | ); |
|
70 | 2 | if (!$dateTime) { |
|
71 | 2 | throw new \Exception("Could not create DateTime object from $timeU"); |
|
72 | } |
||
73 | $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
||
74 | } else { |
||
75 | 22 | $dateTime = \DateTime::createFromFormat( |
|
76 | 22 | 'U', |
|
77 | 22 | strval($timeU) |
|
78 | 22 | ); |
|
79 | if (!$dateTime) { |
||
80 | throw new \Exception("Could not create DateTime object from $timeU"); |
||
81 | } |
||
82 | $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
||
83 | 24 | } |
|
84 | 1 | } else { |
|
85 | $dateTime = \DateTime::createFromFormat( |
||
86 | 24 | 'U.u', |
|
87 | strval($timeU) |
||
88 | 24 | ); |
|
89 | if (!$dateTime) { |
||
90 | throw new \Exception("Could not create DateTime object from $timeU"); |
||
91 | } |
||
92 | $dateTime->setTimezone(new \DateTimeZone(date_default_timezone_get())); |
||
93 | } |
||
94 | } |
||
95 | 21 | ||
96 | if (!($dateTime instanceof \DateTime)) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
97 | 21 | throw new \InvalidArgumentException("Invalid time: $time".($timeU != $time ? " - (micro: $timeU)" : '')); |
|
98 | } |
||
99 | $jobClass = $this->jobManager->getJobClass(); |
||
100 | 22 | ||
101 | return new $jobClass($this, $batch, $priority, $dateTime); |
||
102 | 22 | } |
|
103 | 22 | ||
104 | 22 | /** |
|
105 | 22 | * @param int $delay Amount of time to delay |
|
106 | 22 | * @param int|null $priority |
|
107 | */ |
||
108 | 22 | public function later($delay = 0, $priority = null) |
|
109 | 22 | { |
|
110 | return $this->batchOrLaterDelay($delay, false, $priority); |
||
111 | 22 | } |
|
112 | |||
113 | public function batchOrLaterDelay($delay = 0, $batch = false, $priority = null) |
||
114 | { |
||
115 | $timing = Util::getMicrotimeStr(); |
||
116 | $parts = explode('.', $timing); |
||
117 | $parts[0] = strval(intval($parts[0]) + intval($delay)); |
||
118 | 6 | $localeInfo = localeconv(); |
|
119 | $decimalPoint = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : '.'; |
||
120 | 6 | ||
121 | $job = $this->at(implode($decimalPoint, $parts), $batch, $priority); |
||
122 | $job->setDelay($delay); |
||
123 | |||
124 | return $job; |
||
125 | } |
||
126 | |||
127 | 1 | /** |
|
128 | * @param int $delay Amount of time to delay |
||
129 | 1 | * @param int|null $priority |
|
130 | */ |
||
131 | public function batchLater($delay = 0, $priority = null) |
||
132 | { |
||
133 | return $this->batchOrLaterDelay($delay, true, $priority); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param int|null $time |
||
138 | * @param int|null $priority |
||
139 | */ |
||
140 | public function batchAt($time = null, $priority = null) |
||
141 | { |
||
142 | return $this->at($time, true, $priority); |
||
143 | } |
||
144 | |||
145 | abstract public function getName(); |
||
146 | } |
||
147 |