1 | <?php |
||
12 | class JobManager extends BaseJobManager |
||
13 | { |
||
14 | use CommonTrait; |
||
15 | |||
16 | 3 | public function countJobsByStatus($objectName, $status, $workerName = null, $method = null) |
|
30 | |||
31 | /** |
||
32 | * @param string $objectName |
||
33 | */ |
||
34 | 12 | public function stopIdGenerator($objectName) |
|
38 | |||
39 | 1 | public function restoreIdGenerator($objectName) |
|
43 | |||
44 | /** |
||
45 | * @param string|null $workerName |
||
46 | * @param string|null $method |
||
47 | */ |
||
48 | 1 | public function pruneErroneousJobs($workerName = null, $method = null) |
|
65 | |||
66 | /** |
||
67 | * @param Builder $builder |
||
68 | * @param string|null $workerName |
||
69 | * @param string|null $method |
||
70 | */ |
||
71 | 15 | protected function addWorkerNameCriterion(Builder $builder, $workerName = null, $method = null) |
|
81 | |||
82 | /** |
||
83 | * @param null $workerName |
||
84 | * @param null $method |
||
85 | * |
||
86 | * @return int |
||
87 | */ |
||
88 | 2 | protected function updateExpired($workerName = null, $method = null) |
|
106 | |||
107 | /** |
||
108 | * Removes archived jobs older than $olderThan. |
||
109 | * |
||
110 | * @param \DateTime $olderThan |
||
111 | * return int |
||
112 | */ |
||
113 | 1 | public function pruneArchivedJobs(\DateTime $olderThan) |
|
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 | 2 | protected function getStatusByDocument($documentName) |
|
152 | { |
||
153 | // Run a map reduce function get worker and status break down |
||
154 | 2 | $mapFunc = "function() { |
|
155 | var result = {}; |
||
156 | result[this.status] = 1; |
||
157 | var key = this.worker_name + '->' + this.method + '()'; |
||
158 | emit(key, result); |
||
159 | }"; |
||
160 | 2 | $reduceFunc = 'function(k, vals) { |
|
161 | var result = {}; |
||
162 | for (var index in vals) { |
||
163 | var val = vals[index]; |
||
164 | for (var i in val) { |
||
165 | if (result.hasOwnProperty(i)) { |
||
166 | result[i] += val[i]; |
||
167 | } |
||
168 | else { |
||
169 | result[i] = val[i]; |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | return result; |
||
174 | }'; |
||
175 | /** @var DocumentManager $objectManager */ |
||
176 | 2 | $objectManager = $this->getObjectManager(); |
|
177 | 2 | $qb = $objectManager->createQueryBuilder($documentName); |
|
178 | 2 | $qb->map($mapFunc) |
|
179 | 2 | ->reduce($reduceFunc); |
|
180 | 2 | $query = $qb->getQuery(); |
|
181 | 2 | $results = $query->execute(); |
|
182 | |||
183 | $allStatus = array( |
||
184 | 2 | BaseJob::STATUS_ERROR => 0, |
|
185 | 2 | BaseJob::STATUS_NEW => 0, |
|
186 | 2 | RetryableJob::STATUS_EXPIRED => 0, |
|
187 | 2 | RetryableJob::STATUS_MAX_ERROR => 0, |
|
188 | 2 | RetryableJob::STATUS_MAX_RETRIES => 0, |
|
189 | 2 | RetryableJob::STATUS_MAX_STALLED => 0, |
|
190 | 2 | BaseJob::STATUS_RUNNING => 0, |
|
191 | 2 | BaseJob::STATUS_SUCCESS => 0, |
|
192 | ); |
||
193 | |||
194 | 2 | $status = []; |
|
195 | |||
196 | 2 | foreach ($results as $info) { |
|
197 | 1 | $status[$info['_id']] = $info['value'] + $allStatus; |
|
198 | } |
||
199 | |||
200 | 2 | return $status; |
|
201 | } |
||
202 | |||
203 | 2 | public function getStatus() |
|
221 | |||
222 | /** |
||
223 | * Get the next job to run (can be filtered by workername and method name). |
||
224 | * |
||
225 | * @param string $workerName |
||
226 | * @param string $methodName |
||
227 | * @param bool $prioritize |
||
228 | * |
||
229 | * @return \Dtc\QueueBundle\Model\Job |
||
230 | */ |
||
231 | 8 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
270 | } |
||
271 |