1 | <?php |
||
11 | class JobManager extends BaseJobManager |
||
12 | { |
||
13 | use CommonTrait; |
||
14 | |||
15 | 3 | public function countJobsByStatus($objectName, $status, $workerName = null, $method = null) |
|
16 | { |
||
17 | /** @var DocumentManager $objectManager */ |
||
18 | 3 | $objectManager = $this->getObjectManager(); |
|
19 | 3 | $qb = $objectManager->createQueryBuilder($objectName); |
|
20 | $qb |
||
21 | 3 | ->find() |
|
22 | 3 | ->field('status')->equals($status); |
|
23 | |||
24 | 3 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
25 | 3 | $query = $qb->getQuery(); |
|
26 | |||
27 | 3 | return $query->count(); |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * @param string|null $workerName |
||
32 | * @param string|null $method |
||
33 | */ |
||
34 | 1 | public function pruneErroneousJobs($workerName = null, $method = null) |
|
35 | { |
||
36 | /** @var DocumentManager $objectManager */ |
||
37 | 1 | $objectManager = $this->getObjectManager(); |
|
38 | 1 | $qb = $objectManager->createQueryBuilder($this->getArchiveObjectName()); |
|
39 | 1 | $qb = $qb->remove(); |
|
40 | 1 | $qb->field('status')->equals(BaseJob::STATUS_ERROR); |
|
41 | 1 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
42 | |||
43 | 1 | $query = $qb->getQuery(); |
|
44 | 1 | $result = $query->execute(); |
|
45 | 1 | if (isset($result['n'])) { |
|
46 | 1 | return $result['n']; |
|
47 | } |
||
48 | |||
49 | return 0; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param Builder $builder |
||
54 | * @param string|null $workerName |
||
55 | * @param string|null $method |
||
56 | */ |
||
57 | 16 | protected function addWorkerNameCriterion(Builder $builder, $workerName = null, $method = null) |
|
58 | { |
||
59 | 16 | if (null !== $workerName) { |
|
60 | 4 | $builder->field('workerName')->equals($workerName); |
|
61 | } |
||
62 | |||
63 | 16 | if (null !== $method) { |
|
64 | 3 | $builder->field('method')->equals($method); |
|
65 | } |
||
66 | 16 | } |
|
67 | |||
68 | /** |
||
69 | * @param null $workerName |
||
70 | * @param null $method |
||
71 | * |
||
72 | * @return int |
||
73 | */ |
||
74 | 2 | protected function updateExpired($workerName = null, $method = null) |
|
92 | |||
93 | /** |
||
94 | * Removes archived jobs older than $olderThan. |
||
95 | * |
||
96 | * @param \DateTime $olderThan |
||
97 | * return int |
||
98 | */ |
||
99 | 1 | public function pruneArchivedJobs(\DateTime $olderThan) |
|
100 | { |
||
101 | 1 | return $this->removeOlderThan($this->getArchiveObjectName(), 'updatedAt', $olderThan); |
|
102 | } |
||
103 | |||
104 | 2 | public function getJobCount($workerName = null, $method = null) |
|
105 | { |
||
106 | /** @var DocumentManager $objectManager */ |
||
107 | 2 | $objectManager = $this->getObjectManager(); |
|
108 | 2 | $qb = $objectManager->createQueryBuilder($this->getObjectName()); |
|
109 | $qb |
||
110 | 2 | ->find(); |
|
111 | |||
112 | 2 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
113 | |||
114 | // Filter |
||
115 | 2 | $date = new \DateTime(); |
|
116 | $qb |
||
117 | 2 | ->addAnd( |
|
118 | 2 | $qb->expr()->addOr($qb->expr()->field('expiresAt')->equals(null), $qb->expr()->field('expiresAt')->gt($date)) |
|
119 | ) |
||
120 | 2 | ->field('locked')->equals(null); |
|
121 | |||
122 | 2 | $query = $qb->getQuery(); |
|
123 | |||
124 | 2 | return $query->count(true); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get Status Jobs. |
||
129 | * |
||
130 | * @param string $documentName |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | 2 | protected function getStatusByDocument($documentName) |
|
185 | |||
186 | 2 | public function getStatus() |
|
204 | |||
205 | /** |
||
206 | * Get the next job to run (can be filtered by workername and method name). |
||
207 | * |
||
208 | * @param string $workerName |
||
209 | * @param string $methodName |
||
210 | * @param bool $prioritize |
||
211 | * |
||
212 | * @return \Dtc\QueueBundle\Model\Job |
||
213 | */ |
||
214 | 9 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
215 | { |
||
216 | /** @var DocumentManager $objectManager */ |
||
217 | 9 | $objectManager = $this->getObjectManager(); |
|
218 | 9 | $qb = $objectManager->createQueryBuilder($this->getObjectName()); |
|
219 | $qb |
||
220 | 9 | ->findAndUpdate() |
|
221 | 9 | ->returnNew(); |
|
222 | |||
223 | 9 | $this->addWorkerNameCriterion($qb, $workerName, $methodName); |
|
224 | 9 | if ($prioritize) { |
|
225 | 9 | $qb->sort([ |
|
226 | 9 | 'priority' => 'desc', |
|
227 | 'whenAt' => 'asc', |
||
228 | ]); |
||
229 | } else { |
||
230 | 1 | $qb->sort('whenAt', 'asc'); |
|
231 | } |
||
232 | |||
233 | // Filter |
||
234 | 9 | $date = new \DateTime(); |
|
235 | $qb |
||
236 | 9 | ->addAnd( |
|
237 | 9 | $qb->expr()->addOr($qb->expr()->field('whenAt')->equals(null), $qb->expr()->field('whenAt')->lte($date)), |
|
238 | 9 | $qb->expr()->addOr($qb->expr()->field('expiresAt')->equals(null), $qb->expr()->field('expiresAt')->gt($date)) |
|
239 | ) |
||
240 | 9 | ->field('status')->equals(BaseJob::STATUS_NEW) |
|
241 | 9 | ->field('locked')->equals(null); |
|
242 | |||
243 | // Update |
||
244 | $qb |
||
245 | 9 | ->field('lockedAt')->set($date) // Set started |
|
246 | 9 | ->field('locked')->set(true) |
|
247 | 9 | ->field('status')->set(BaseJob::STATUS_RUNNING) |
|
248 | 9 | ->field('runId')->set($runId); |
|
249 | |||
250 | 9 | $query = $qb->getQuery(); |
|
251 | |||
252 | 9 | $job = $query->execute(); |
|
253 | |||
254 | 9 | return $job; |
|
255 | } |
||
256 | |||
257 | 1 | protected function updateNearestBatch(\Dtc\QueueBundle\Model\Job $job) |
|
300 | } |
||
301 |