1 | <?php |
||
11 | class JobManager extends BaseJobManager |
||
12 | { |
||
13 | 3 | public function countJobsByStatus($objectName, $status, $workerName = null, $method = null) |
|
14 | { |
||
15 | /** @var DocumentManager $objectManager */ |
||
16 | 3 | $objectManager = $this->getObjectManager(); |
|
17 | 3 | $qb = $objectManager->createQueryBuilder($objectName); |
|
18 | $qb |
||
19 | 3 | ->find() |
|
20 | 3 | ->field('status')->equals($status); |
|
21 | |||
22 | 3 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
|
23 | 3 | $query = $qb->getQuery(); |
|
24 | |||
25 | 3 | return $query->count(); |
|
26 | } |
||
27 | |||
28 | /** |
||
29 | * @param string $objectName |
||
30 | */ |
||
31 | 8 | public function stopIdGenerator($objectName) |
|
35 | |||
36 | 1 | public function restoreIdGenerator($objectName) |
|
40 | |||
41 | /** |
||
42 | * @param string|null $workerName |
||
43 | * @param string|null $method |
||
44 | */ |
||
45 | 1 | public function pruneErroneousJobs($workerName = null, $method = null) |
|
62 | |||
63 | 10 | protected function addWorkerNameCriterion(Builder $builder, $workerName = null, $method = null) |
|
73 | |||
74 | 1 | protected function updateExpired($workerName = null, $method = null) |
|
92 | |||
93 | /** |
||
94 | * Removes archived jobs older than $olderThan. |
||
95 | * |
||
96 | * @param \DateTime $olderThan |
||
97 | */ |
||
98 | 1 | public function pruneArchivedJobs(\DateTime $olderThan) |
|
115 | |||
116 | 1 | public function getJobCount($workerName = null, $method = null) |
|
138 | |||
139 | /** |
||
140 | * Get Status Jobs. |
||
141 | * |
||
142 | * @param string $documentName |
||
143 | */ |
||
144 | protected function getStatusByDocument($documentName) |
||
145 | { |
||
146 | // Run a map reduce function get worker and status break down |
||
147 | $mapFunc = "function() { |
||
148 | var result = {}; |
||
149 | result[this.status] = 1; |
||
150 | var key = this.worker_name + '->' + this.method + '()'; |
||
151 | emit(key, result); |
||
152 | }"; |
||
153 | $reduceFunc = 'function(k, vals) { |
||
154 | var result = {}; |
||
155 | for (var index in vals) { |
||
156 | var val = vals[index]; |
||
157 | for (var i in val) { |
||
158 | if (result.hasOwnProperty(i)) { |
||
159 | result[i] += val[i]; |
||
160 | } |
||
161 | else { |
||
162 | result[i] = val[i]; |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | return result; |
||
167 | }'; |
||
168 | /** @var DocumentManager $objectManager */ |
||
169 | $objectManager = $this->getObjectManager(); |
||
170 | $qb = $objectManager->createQueryBuilder($documentName); |
||
171 | $qb->map($mapFunc) |
||
172 | ->reduce($reduceFunc); |
||
173 | $query = $qb->getQuery(); |
||
174 | $results = $query->execute(); |
||
175 | |||
176 | $allStatus = array( |
||
177 | BaseJob::STATUS_ERROR => 0, |
||
178 | BaseJob::STATUS_NEW => 0, |
||
179 | BaseJob::STATUS_RUNNING => 0, |
||
180 | BaseJob::STATUS_SUCCESS => 0, |
||
181 | ); |
||
182 | |||
183 | $status = []; |
||
184 | |||
185 | foreach ($results as $info) { |
||
186 | $status[$info['_id']] = $info['value'] + $allStatus; |
||
187 | } |
||
188 | |||
189 | return $status; |
||
190 | } |
||
191 | |||
192 | public function getStatus() |
||
210 | |||
211 | /** |
||
212 | * Get the next job to run (can be filtered by workername and method name). |
||
213 | * |
||
214 | * @param string $workerName |
||
215 | * @param string $methodName |
||
216 | * @param bool $prioritize |
||
217 | * |
||
218 | * @return \Dtc\QueueBundle\Model\Job |
||
219 | */ |
||
220 | 5 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
259 | } |
||
260 |