1 | <?php |
||
12 | class JobManager extends BaseJobManager |
||
13 | { |
||
14 | use CommonTrait; |
||
15 | const REDUCE_FUNCTION = 'function(k, vals) { |
||
16 | var result = {}; |
||
17 | for (var index in vals) { |
||
18 | var val = vals[index]; |
||
19 | for (var i in val) { |
||
20 | if (result.hasOwnProperty(i)) { |
||
21 | result[i] += val[i]; |
||
22 | } |
||
23 | else { |
||
24 | result[i] = val[i]; |
||
25 | } |
||
26 | } |
||
27 | } |
||
28 | return result; |
||
29 | }'; |
||
30 | |||
31 | 3 | public function countJobsByStatus($objectName, $status, $workerName = null, $method = null) |
|
32 | { |
||
33 | /** @var DocumentManager $objectManager */ |
||
34 | 3 | $objectManager = $this->getObjectManager(); |
|
35 | 3 | $qb = $objectManager->createQueryBuilder($objectName); |
|
36 | $qb |
||
37 | 3 | ->find() |
|
38 | 3 | ->field('status')->equals($status); |
|
39 | |||
40 | 3 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
41 | 3 | $query = $qb->getQuery(); |
|
42 | |||
43 | 3 | return $query->count(); |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param string|null $workerName |
||
48 | * @param string|null $method |
||
49 | */ |
||
50 | 1 | public function pruneErroneousJobs($workerName = null, $method = null) |
|
51 | { |
||
52 | /** @var DocumentManager $objectManager */ |
||
53 | 1 | $objectManager = $this->getObjectManager(); |
|
54 | 1 | $qb = $objectManager->createQueryBuilder($this->getJobArchiveClass()); |
|
55 | 1 | $qb = $qb->remove(); |
|
56 | 1 | $qb->field('status')->equals(BaseJob::STATUS_ERROR); |
|
57 | 1 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
58 | |||
59 | 1 | $query = $qb->getQuery(); |
|
60 | 1 | $result = $query->execute(); |
|
61 | 1 | if (isset($result['n'])) { |
|
62 | 1 | return $result['n']; |
|
63 | } |
||
64 | |||
65 | return 0; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param Builder $builder |
||
70 | * @param string|null $workerName |
||
71 | * @param string|null $method |
||
72 | */ |
||
73 | 18 | protected function addWorkerNameCriterion(Builder $builder, $workerName = null, $method = null) |
|
74 | { |
||
75 | 18 | if (null !== $workerName) { |
|
76 | 6 | $builder->field('workerName')->equals($workerName); |
|
77 | } |
||
78 | |||
79 | 18 | if (null !== $method) { |
|
80 | 5 | $builder->field('method')->equals($method); |
|
81 | } |
||
82 | 18 | } |
|
83 | |||
84 | /** |
||
85 | * @param null $workerName |
||
86 | * @param null $method |
||
87 | * |
||
88 | * @return int |
||
89 | */ |
||
90 | 2 | protected function updateExpired($workerName = null, $method = null) |
|
91 | { |
||
92 | /** @var DocumentManager $objectManager */ |
||
93 | 2 | $objectManager = $this->getObjectManager(); |
|
94 | 2 | $qb = $objectManager->createQueryBuilder($this->getJobClass()); |
|
95 | 2 | $qb = $qb->updateMany(); |
|
96 | 2 | $qb->field('expiresAt')->lte(new \DateTime()); |
|
97 | 2 | $qb->field('status')->equals(BaseJob::STATUS_NEW); |
|
98 | 2 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
99 | 2 | $qb->field('status')->set(\Dtc\QueueBundle\Model\Job::STATUS_EXPIRED); |
|
100 | 2 | $query = $qb->getQuery(); |
|
101 | 2 | $result = $query->execute(); |
|
102 | 2 | if (isset($result['n'])) { |
|
103 | 2 | return $result['n']; |
|
104 | } |
||
105 | |||
106 | return 0; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Removes archived jobs older than $olderThan. |
||
111 | * |
||
112 | * @param \DateTime $olderThan |
||
113 | * |
||
114 | * @return int |
||
115 | */ |
||
116 | 1 | public function pruneArchivedJobs(\DateTime $olderThan) |
|
117 | { |
||
118 | 1 | return $this->removeOlderThan($this->getJobArchiveClass(), 'updatedAt', $olderThan); |
|
119 | } |
||
120 | |||
121 | 2 | public function getJobCount($workerName = null, $method = null) |
|
143 | |||
144 | /** |
||
145 | * Get Status Jobs. |
||
146 | * |
||
147 | * @param string $documentName |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | 3 | protected function getStatusByDocument($documentName) |
|
188 | |||
189 | 3 | public function getStatus() |
|
211 | |||
212 | /** |
||
213 | * Get the next job to run (can be filtered by workername and method name). |
||
214 | * |
||
215 | * @param string $workerName |
||
216 | * @param string $methodName |
||
217 | * @param bool $prioritize |
||
218 | * @param string|null $runId |
||
219 | * |
||
220 | * @return \Dtc\QueueBundle\Model\Job |
||
221 | */ |
||
222 | 11 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
243 | |||
244 | /** |
||
245 | * @param string|null $workerName |
||
246 | * @param string|null $methodName |
||
247 | * @param bool $prioritize |
||
248 | * |
||
249 | * @return Builder |
||
250 | */ |
||
251 | 11 | public function getJobQueryBuilder($workerName = null, $methodName = null, $prioritize = true) |
|
272 | |||
273 | 1 | protected function updateNearestBatch(\Dtc\QueueBundle\Model\Job $job) |
|
316 | |||
317 | /** |
||
318 | * @param mixed $builder |
||
319 | */ |
||
320 | 12 | protected function addStandardPredicates($builder) |
|
331 | |||
332 | 1 | public function getWorkersAndMethods() |
|
333 | { |
||
334 | /** @var DocumentManager $documentManager */ |
||
335 | 1 | $documentManager = $this->getObjectManager(); |
|
336 | |||
337 | 1 | if (!method_exists($documentManager, 'createAggregationBuilder')) { |
|
338 | return []; |
||
339 | } |
||
340 | |||
341 | 1 | $aggregationBuilder = $documentManager->createAggregationBuilder($this->getJobClass()); |
|
342 | |||
343 | 1 | $this->addStandardPredicates($aggregationBuilder->match()); |
|
344 | |||
345 | 1 | $aggregationBuilder->group() |
|
346 | 1 | ->field('id') |
|
347 | 1 | ->expression( |
|
348 | 1 | $aggregationBuilder->expr() |
|
349 | 1 | ->field('workerName')->expression('$workerName') |
|
350 | 1 | ->field('method')->expression('$method') |
|
351 | ); |
||
352 | 1 | $results = $aggregationBuilder->execute()->toArray(); |
|
353 | |||
354 | 1 | if (!$results) { |
|
355 | 1 | return []; |
|
356 | } |
||
357 | |||
358 | $workersMethods = []; |
||
359 | foreach ($results as $result) { |
||
360 | if (isset($result['_id'])) { |
||
361 | $workersMethods[$result['_id']['worker_name']][] = $result['_id']['method']; |
||
362 | } |
||
363 | } |
||
364 | |||
365 | return $workersMethods; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @param string $workerName |
||
370 | * @param string $methodName |
||
371 | */ |
||
372 | 2 | public function countLiveJobs($workerName = null, $methodName = null) |
|
384 | |||
385 | /** |
||
386 | * @param string $workerName |
||
387 | * @param string $methodName |
||
388 | * @param \Closure $progressCallback |
||
389 | */ |
||
390 | 1 | public function archiveAllJobs($workerName = null, $methodName = null, $progressCallback) |
|
417 | } |
||
418 |