1 | <?php |
||
12 | class JobManager extends BaseJobManager |
||
13 | { |
||
14 | use CommonTrait; |
||
15 | |||
16 | 3 | public function countJobsByStatus($objectName, $status, $workerName = null, $method = null) |
|
17 | { |
||
18 | /** @var DocumentManager $objectManager */ |
||
19 | 3 | $objectManager = $this->getObjectManager(); |
|
20 | 3 | $qb = $objectManager->createQueryBuilder($objectName); |
|
21 | $qb |
||
22 | 3 | ->find() |
|
23 | 3 | ->field('status')->equals($status); |
|
24 | |||
25 | 3 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
26 | 3 | $query = $qb->getQuery(); |
|
27 | |||
28 | 3 | return $query->count(); |
|
29 | } |
||
30 | |||
31 | /** |
||
32 | * @param string|null $workerName |
||
33 | * @param string|null $method |
||
34 | */ |
||
35 | 1 | public function pruneErroneousJobs($workerName = null, $method = null) |
|
36 | { |
||
37 | /** @var DocumentManager $objectManager */ |
||
38 | 1 | $objectManager = $this->getObjectManager(); |
|
39 | 1 | $qb = $objectManager->createQueryBuilder($this->getArchiveObjectName()); |
|
40 | 1 | $qb = $qb->remove(); |
|
41 | 1 | $qb->field('status')->equals(BaseJob::STATUS_ERROR); |
|
42 | 1 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
43 | |||
44 | 1 | $query = $qb->getQuery(); |
|
45 | 1 | $result = $query->execute(); |
|
46 | 1 | if (isset($result['n'])) { |
|
47 | 1 | return $result['n']; |
|
48 | } |
||
49 | |||
50 | return 0; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param Builder $builder |
||
55 | * @param string|null $workerName |
||
56 | * @param string|null $method |
||
57 | */ |
||
58 | 15 | protected function addWorkerNameCriterion(Builder $builder, $workerName = null, $method = null) |
|
59 | { |
||
60 | 15 | if (null !== $workerName) { |
|
61 | 4 | $builder->field('workerName')->equals($workerName); |
|
62 | } |
||
63 | |||
64 | 15 | if (null !== $method) { |
|
65 | 3 | $builder->field('method')->equals($method); |
|
66 | } |
||
67 | 15 | } |
|
68 | |||
69 | /** |
||
70 | * @param null $workerName |
||
71 | * @param null $method |
||
72 | * |
||
73 | * @return int |
||
74 | */ |
||
75 | 2 | protected function updateExpired($workerName = null, $method = null) |
|
93 | |||
94 | /** |
||
95 | * Removes archived jobs older than $olderThan. |
||
96 | * |
||
97 | * @param \DateTime $olderThan |
||
98 | * return int |
||
99 | */ |
||
100 | 1 | public function pruneArchivedJobs(\DateTime $olderThan) |
|
104 | |||
105 | 2 | public function getJobCount($workerName = null, $method = null) |
|
127 | |||
128 | 1 | protected function getJobCurrentStatus(\Dtc\QueueBundle\Model\Job $job) |
|
139 | |||
140 | /** |
||
141 | * Get Status Jobs. |
||
142 | * |
||
143 | * @param string $documentName |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | 2 | protected function getStatusByDocument($documentName) |
|
148 | { |
||
149 | // Run a map reduce function get worker and status break down |
||
150 | 2 | $mapFunc = "function() { |
|
151 | var result = {}; |
||
152 | result[this.status] = 1; |
||
153 | var key = this.worker_name + '->' + this.method + '()'; |
||
154 | emit(key, result); |
||
155 | }"; |
||
156 | 2 | $reduceFunc = 'function(k, vals) { |
|
157 | var result = {}; |
||
158 | for (var index in vals) { |
||
159 | var val = vals[index]; |
||
160 | for (var i in val) { |
||
161 | if (result.hasOwnProperty(i)) { |
||
162 | result[i] += val[i]; |
||
163 | } |
||
164 | else { |
||
165 | result[i] = val[i]; |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | return result; |
||
170 | }'; |
||
171 | /** @var DocumentManager $objectManager */ |
||
172 | 2 | $objectManager = $this->getObjectManager(); |
|
173 | 2 | $qb = $objectManager->createQueryBuilder($documentName); |
|
174 | 2 | $qb->map($mapFunc) |
|
175 | 2 | ->reduce($reduceFunc); |
|
176 | 2 | $query = $qb->getQuery(); |
|
177 | 2 | $results = $query->execute(); |
|
178 | |||
179 | $allStatus = array( |
||
180 | 2 | BaseJob::STATUS_ERROR => 0, |
|
181 | BaseJob::STATUS_NEW => 0, |
||
182 | RetryableJob::STATUS_EXPIRED => 0, |
||
183 | RetryableJob::STATUS_MAX_ERROR => 0, |
||
184 | RetryableJob::STATUS_MAX_RETRIES => 0, |
||
185 | RetryableJob::STATUS_MAX_STALLED => 0, |
||
186 | BaseJob::STATUS_RUNNING => 0, |
||
187 | BaseJob::STATUS_SUCCESS => 0, |
||
188 | ); |
||
189 | |||
190 | 2 | $status = []; |
|
191 | |||
192 | 2 | foreach ($results as $info) { |
|
193 | 1 | $status[$info['_id']] = $info['value'] + $allStatus; |
|
194 | } |
||
195 | |||
196 | 2 | return $status; |
|
197 | } |
||
198 | |||
199 | 2 | public function getStatus() |
|
200 | { |
||
201 | 2 | $result = $this->getStatusByDocument($this->getObjectName()); |
|
202 | 2 | $status2 = $this->getStatusByDocument($this->getArchiveObjectName()); |
|
203 | 2 | foreach ($status2 as $key => $value) { |
|
204 | 1 | foreach ($value as $k => $v) { |
|
205 | 1 | $result[$key][$k] += $v; |
|
206 | } |
||
207 | } |
||
208 | |||
209 | 2 | $finalResult = []; |
|
210 | 2 | foreach ($result as $key => $item) { |
|
211 | 1 | ksort($item); |
|
212 | 1 | $finalResult[$key] = $item; |
|
213 | } |
||
214 | |||
215 | 2 | return $finalResult; |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * Get the next job to run (can be filtered by workername and method name). |
||
220 | * |
||
221 | * @param string $workerName |
||
222 | * @param string $methodName |
||
223 | * @param bool $prioritize |
||
224 | * |
||
225 | * @return \Dtc\QueueBundle\Model\Job |
||
226 | */ |
||
227 | 8 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
269 | |||
270 | 1 | protected function updateNearestBatch(\Dtc\QueueBundle\Model\Job $job) |
|
271 | { |
||
313 | } |
||
314 |