1 | <?php |
||
13 | class JobManager extends DoctrineJobManager |
||
14 | { |
||
15 | use CommonTrait; |
||
16 | const REDUCE_FUNCTION = 'function(k, vals) { |
||
17 | var result = {}; |
||
18 | for (var index in vals) { |
||
19 | var val = vals[index]; |
||
20 | for (var i in val) { |
||
21 | if (result.hasOwnProperty(i)) { |
||
22 | result[i] += val[i]; |
||
23 | } |
||
24 | else { |
||
25 | result[i] = val[i]; |
||
26 | } |
||
27 | } |
||
28 | } |
||
29 | return result; |
||
30 | }'; |
||
31 | |||
32 | 3 | public function countJobsByStatus($objectName, $status, $workerName = null, $method = null) |
|
33 | { |
||
34 | /** @var DocumentManager $objectManager */ |
||
35 | 3 | $objectManager = $this->getObjectManager(); |
|
36 | 3 | $qb = $objectManager->createQueryBuilder($objectName); |
|
37 | $qb |
||
38 | 3 | ->find() |
|
39 | 3 | ->field('status')->equals($status); |
|
40 | |||
41 | 3 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
42 | 3 | $query = $qb->getQuery(); |
|
43 | |||
44 | 3 | return $query->count(); |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param string|null $workerName |
||
49 | * @param string|null $method |
||
50 | */ |
||
51 | 1 | public function pruneExceptionJobs($workerName = null, $method = null) |
|
52 | { |
||
53 | /** @var DocumentManager $objectManager */ |
||
54 | 1 | $objectManager = $this->getObjectManager(); |
|
55 | 1 | $qb = $objectManager->createQueryBuilder($this->getJobArchiveClass()); |
|
56 | 1 | $qb = $qb->remove(); |
|
57 | 1 | $qb->field('status')->equals(BaseJob::STATUS_EXCEPTION); |
|
58 | 1 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
59 | |||
60 | 1 | $query = $qb->getQuery(); |
|
61 | 1 | $result = $query->execute(); |
|
62 | 1 | if (isset($result['n'])) { |
|
63 | 1 | return $result['n']; |
|
64 | } |
||
65 | |||
66 | return 0; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param Builder $builder |
||
71 | * @param string|null $workerName |
||
72 | * @param string|null $method |
||
73 | */ |
||
74 | 17 | protected function addWorkerNameCriterion(Builder $builder, $workerName = null, $method = null) |
|
75 | { |
||
76 | 17 | if (null !== $workerName) { |
|
77 | 6 | $builder->field('workerName')->equals($workerName); |
|
78 | } |
||
79 | |||
80 | 17 | if (null !== $method) { |
|
81 | 5 | $builder->field('method')->equals($method); |
|
82 | } |
||
83 | 17 | } |
|
84 | |||
85 | /** |
||
86 | * @param null $workerName |
||
87 | * @param null $method |
||
88 | * |
||
89 | * @return int |
||
90 | */ |
||
91 | 2 | protected function updateExpired($workerName = null, $method = null) |
|
109 | |||
110 | /** |
||
111 | * Removes archived jobs older than $olderThan. |
||
112 | * |
||
113 | * @param \DateTime $olderThan |
||
114 | * |
||
115 | * @return int |
||
116 | */ |
||
117 | 1 | public function pruneArchivedJobs(\DateTime $olderThan) |
|
118 | { |
||
119 | 1 | return $this->removeOlderThan($this->getJobArchiveClass(), 'updatedAt', $olderThan); |
|
120 | } |
||
121 | |||
122 | 2 | public function getJobCount($workerName = null, $method = null) |
|
123 | { |
||
124 | /** @var DocumentManager $objectManager */ |
||
125 | 2 | $objectManager = $this->getObjectManager(); |
|
126 | 2 | $qb = $objectManager->createQueryBuilder($this->getJobClass()); |
|
127 | $qb |
||
128 | 2 | ->find(); |
|
129 | |||
130 | 2 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
131 | |||
132 | // Filter |
||
133 | 2 | $date = new \DateTime(); |
|
134 | $qb |
||
135 | 2 | ->addAnd( |
|
136 | 2 | $qb->expr()->addOr($qb->expr()->field('expiresAt')->equals(null), $qb->expr()->field('expiresAt')->gt($date)) |
|
137 | ); |
||
138 | 2 | $query = $qb->getQuery(); |
|
139 | |||
140 | 2 | return $query->count(true); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get Status Jobs. |
||
145 | * |
||
146 | * @param string $documentName |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | 3 | protected function getStatusByDocument($documentName) |
|
151 | { |
||
152 | // Run a map reduce function get worker and status break down |
||
153 | 3 | $mapFunc = "function() { |
|
154 | var result = {}; |
||
155 | result[this.status] = 1; |
||
156 | var key = this.worker_name + '->' + this.method + '()'; |
||
157 | emit(key, result); |
||
158 | }"; |
||
159 | 3 | $reduceFunc = self::REDUCE_FUNCTION; |
|
160 | /** @var DocumentManager $objectManager */ |
||
161 | 3 | $objectManager = $this->getObjectManager(); |
|
162 | 3 | $qb = $objectManager->createQueryBuilder($documentName); |
|
163 | 3 | $qb->map($mapFunc) |
|
164 | 3 | ->reduce($reduceFunc); |
|
165 | 3 | $query = $qb->getQuery(); |
|
166 | 3 | $results = $query->execute(); |
|
167 | |||
168 | $allStatus = array( |
||
169 | 3 | BaseJob::STATUS_NEW => 0, |
|
170 | BaseJob::STATUS_RUNNING => 0, |
||
171 | BaseJob::STATUS_SUCCESS => 0, |
||
172 | BaseJob::STATUS_FAILURE => 0, |
||
173 | BaseJob::STATUS_EXCEPTION => 0, |
||
174 | \Dtc\QueueBundle\Model\Job::STATUS_EXPIRED => 0, |
||
175 | RetryableJob::STATUS_MAX_FAILURES => 0, |
||
176 | RetryableJob::STATUS_MAX_EXCEPTIONS => 0, |
||
177 | RetryableJob::STATUS_MAX_RETRIES => 0, |
||
178 | StallableJob::STATUS_MAX_STALLS => 0, |
||
179 | ); |
||
180 | |||
181 | 3 | $status = []; |
|
182 | |||
183 | 3 | foreach ($results as $info) { |
|
184 | 1 | $status[$info['_id']] = $info['value'] + $allStatus; |
|
185 | } |
||
186 | |||
187 | 3 | return $status; |
|
188 | } |
||
189 | |||
190 | 3 | public function getStatus() |
|
191 | { |
||
192 | 3 | $result = $this->getStatusByDocument($this->getJobClass()); |
|
193 | 3 | $status2 = $this->getStatusByDocument($this->getJobArchiveClass()); |
|
194 | 3 | foreach ($status2 as $key => $value) { |
|
195 | 1 | foreach ($value as $k => $v) { |
|
196 | 1 | if (isset($result[$key][$k])) { |
|
197 | 1 | $result[$key][$k] += $v; |
|
198 | } else { |
||
199 | 1 | $result[$key][$k] = $v; |
|
200 | } |
||
201 | } |
||
202 | } |
||
203 | |||
204 | 3 | $finalResult = []; |
|
205 | 3 | foreach ($result as $key => $item) { |
|
206 | 1 | ksort($item); |
|
207 | 1 | $finalResult[$key] = $item; |
|
208 | } |
||
209 | |||
210 | 3 | return $finalResult; |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * Get the next job to run (can be filtered by workername and method name). |
||
215 | * |
||
216 | * @param string $workerName |
||
217 | * @param string $methodName |
||
218 | * @param bool $prioritize |
||
219 | * @param string|null $runId |
||
220 | * |
||
221 | * @return \Dtc\QueueBundle\Model\Job |
||
222 | */ |
||
223 | 10 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
224 | { |
||
225 | 10 | $builder = $this->getJobQueryBuilder($workerName, $methodName, $prioritize); |
|
226 | $builder |
||
227 | 10 | ->findAndUpdate() |
|
228 | 10 | ->returnNew(); |
|
229 | |||
230 | 10 | $date = new \DateTime(); |
|
231 | // Update |
||
232 | $builder |
||
233 | 10 | ->field('startedAt')->set($date) |
|
234 | 10 | ->field('status')->set(BaseJob::STATUS_RUNNING) |
|
235 | 10 | ->field('runId')->set($runId); |
|
236 | |||
237 | 10 | $query = $builder->getQuery(); |
|
238 | |||
239 | 10 | $job = $query->execute(); |
|
240 | |||
241 | 10 | return $job; |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * @param string|null $workerName |
||
246 | * @param string|null $methodName |
||
247 | * @param bool $prioritize |
||
248 | * |
||
249 | * @return Builder |
||
250 | */ |
||
251 | 10 | public function getJobQueryBuilder($workerName = null, $methodName = null, $prioritize = true) |
|
272 | |||
273 | 1 | protected function updateNearestBatch(\Dtc\QueueBundle\Model\Job $job) |
|
274 | { |
||
275 | /** @var DocumentManager $objectManager */ |
||
276 | 1 | $objectManager = $this->getObjectManager(); |
|
277 | 1 | $builder = $objectManager->createQueryBuilder($this->getJobClass()); |
|
278 | 1 | $builder->find(); |
|
279 | |||
280 | 1 | $builder->sort('whenAt', 'asc'); |
|
315 | |||
316 | /** |
||
317 | * @param mixed $builder |
||
318 | */ |
||
319 | 11 | protected function addStandardPredicates($builder) |
|
329 | |||
330 | 1 | public function getWorkersAndMethods() |
|
365 | |||
366 | /** |
||
367 | * @param string $workerName |
||
368 | * @param string $methodName |
||
369 | */ |
||
370 | 2 | public function countLiveJobs($workerName = null, $methodName = null) |
|
382 | |||
383 | /** |
||
384 | * @param string $workerName |
||
385 | * @param string $methodName |
||
386 | * @param \Closure $progressCallback |
||
387 | */ |
||
388 | 1 | public function archiveAllJobs($workerName = null, $methodName = null, $progressCallback) |
|
415 | } |
||
416 |