1 | <?php |
||
15 | class JobManager extends BaseJobManager |
||
16 | { |
||
17 | use CommonTrait; |
||
18 | protected static $saveInsertCalled = null; |
||
19 | protected static $resetInsertCalled = null; |
||
20 | |||
21 | 3 | public function countJobsByStatus($objectName, $status, $workerName = null, $method = null) |
|
22 | { |
||
23 | /** @var EntityManager $objectManager */ |
||
24 | 3 | $objectManager = $this->getObjectManager(); |
|
25 | |||
26 | $qb = $objectManager |
||
27 | 3 | ->createQueryBuilder() |
|
28 | 3 | ->select('count(a.id)') |
|
29 | 3 | ->from($objectName, 'a') |
|
30 | 3 | ->where('a.status = :status'); |
|
31 | |||
32 | 3 | if (null !== $workerName) { |
|
33 | 1 | $qb->andWhere('a.workerName = :workerName') |
|
34 | 1 | ->setParameter(':workerName', $workerName); |
|
35 | 1 | } |
|
36 | |||
37 | 3 | if (null !== $method) { |
|
38 | 1 | $qb->andWhere('a.method = :method') |
|
39 | 1 | ->setParameter(':method', $workerName); |
|
40 | 1 | } |
|
41 | |||
42 | 3 | $count = $qb->setParameter(':status', $status) |
|
43 | 3 | ->getQuery()->getSingleScalarResult(); |
|
44 | |||
45 | 3 | if (!$count) { |
|
46 | 1 | return 0; |
|
47 | } |
||
48 | |||
49 | 3 | return $count; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param string|null $workerName |
||
54 | * @param string|null $method |
||
55 | * |
||
56 | * @return int Count of jobs pruned |
||
57 | */ |
||
58 | 1 | public function pruneErroneousJobs($workerName = null, $method = null) |
|
71 | |||
72 | 6 | protected function resetSaveOk($function) |
|
95 | |||
96 | /** |
||
97 | * @param string $workerName |
||
98 | * @param string $method |
||
99 | */ |
||
100 | 2 | protected function addWorkerNameCriterion(QueryBuilder $queryBuilder, $workerName = null, $method = null) |
|
110 | |||
111 | protected function updateExpired($workerName = null, $method = null) |
||
112 | { |
||
113 | /** @var EntityManager $objectManager */ |
||
114 | $objectManager = $this->getObjectManager(); |
||
115 | $qb = $objectManager->createQueryBuilder()->update($this->getObjectName(), 'j'); |
||
116 | $qb->set('j.status', ':newStatus'); |
||
117 | $qb->where('j.expiresAt <= :expiresAt') |
||
118 | ->setParameter(':expiresAt', new \DateTime()); |
||
119 | $qb->andWhere('j.status = :status') |
||
120 | ->setParameter(':status', BaseJob::STATUS_NEW) |
||
121 | ->setParameter(':newStatus', Job::STATUS_EXPIRED); |
||
122 | |||
123 | $this->addWorkerNameCriterion($qb, $workerName, $method); |
||
124 | $query = $qb->getQuery(); |
||
125 | |||
126 | return intval($query->execute()); |
||
127 | } |
||
128 | |||
129 | 1 | protected function getJobCurrentStatus(\Dtc\QueueBundle\Model\Job $job) |
|
138 | |||
139 | /** |
||
140 | * Removes archived jobs older than $olderThan. |
||
141 | * |
||
142 | * @param \DateTime $olderThan |
||
143 | */ |
||
144 | public function pruneArchivedJobs(\DateTime $olderThan) |
||
150 | |||
151 | 1 | public function getJobCount($workerName = null, $method = null) |
|
152 | { |
||
153 | /** @var EntityManager $objectManager */ |
||
154 | 1 | $objectManager = $this->getObjectManager(); |
|
155 | 1 | $qb = $objectManager->createQueryBuilder(); |
|
156 | |||
157 | 1 | $qb = $qb->select('count(j)')->from($this->getObjectName(), 'j'); |
|
158 | |||
159 | 1 | $where = 'where'; |
|
160 | 1 | if (null !== $workerName) { |
|
161 | if (null !== $method) { |
||
162 | $qb->where($qb->expr()->andX( |
||
163 | $qb->expr()->eq('j.workerName', ':workerName'), |
||
164 | $qb->expr()->eq('j.method', ':method') |
||
165 | )) |
||
166 | ->setParameter(':method', $method); |
||
167 | } else { |
||
168 | $qb->where('j.workerName = :workerName'); |
||
169 | } |
||
170 | $qb->setParameter(':workerName', $workerName); |
||
171 | $where = 'andWhere'; |
||
172 | 1 | } elseif (null !== $method) { |
|
173 | 1 | $qb->where('j.method = :method')->setParameter(':method', $method); |
|
174 | $where = 'andWhere'; |
||
175 | } |
||
176 | |||
177 | 1 | $dateTime = new \DateTime(); |
|
178 | // Filter |
||
179 | $qb |
||
180 | 1 | ->$where($qb->expr()->orX( |
|
181 | 1 | $qb->expr()->isNull('j.whenAt'), |
|
182 | 1 | $qb->expr()->lte('j.whenAt', ':whenAt') |
|
183 | 1 | )) |
|
184 | 1 | ->andWhere($qb->expr()->orX( |
|
185 | 1 | $qb->expr()->isNull('j.expiresAt'), |
|
186 | 1 | $qb->expr()->gt('j.expiresAt', ':expiresAt') |
|
187 | 1 | )) |
|
188 | 1 | ->andWhere('j.locked is NULL') |
|
189 | 1 | ->setParameter(':whenAt', $dateTime) |
|
190 | 1 | ->setParameter(':expiresAt', $dateTime); |
|
191 | |||
192 | 1 | $query = $qb->getQuery(); |
|
193 | |||
194 | 1 | return $query->getSingleScalarResult(); |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * For ORM it's prudent to wrap things in a transaction. |
||
199 | * |
||
200 | * @param $i |
||
201 | * @param $count |
||
202 | * @param array $stalledJobs |
||
203 | * @param $countProcessed |
||
204 | */ |
||
205 | 1 | protected function runStalledLoop($i, $count, array $stalledJobs, &$countProcessed) |
|
206 | { |
||
207 | /** @var EntityManager $objectManager */ |
||
208 | 1 | $objectManager = $this->getObjectManager(); |
|
209 | try { |
||
210 | 1 | $objectManager->beginTransaction(); |
|
211 | 1 | parent::runStalledLoop($i, $count, $stalledJobs, $countProcessed); |
|
212 | 1 | $objectManager->commit(); |
|
213 | 1 | } catch (\Exception $exception) { |
|
214 | $objectManager->rollback(); |
||
215 | |||
216 | // Try again |
||
217 | parent::runStalledLoop($i, $count, $stalledJobs, $countProcessed); |
||
218 | } |
||
219 | 1 | } |
|
220 | |||
221 | /** |
||
222 | * Get Jobs statuses. |
||
223 | */ |
||
224 | 1 | public function getStatus() |
|
225 | { |
||
226 | 1 | $result = []; |
|
227 | 1 | $this->getStatusByEntityName($this->getObjectName(), $result); |
|
228 | 1 | $this->getStatusByEntityName($this->getArchiveObjectName(), $result); |
|
229 | |||
230 | 1 | $finalResult = []; |
|
231 | 1 | foreach ($result as $key => $item) { |
|
232 | ksort($item); |
||
233 | foreach ($item as $status => $count) { |
||
234 | if (isset($finalResult[$key][$status])) { |
||
235 | $finalResult[$key][$status] += $count; |
||
236 | } else { |
||
237 | $finalResult[$key][$status] = $count; |
||
238 | } |
||
239 | } |
||
240 | 1 | } |
|
241 | |||
242 | 1 | return $finalResult; |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * @param string $entityName |
||
247 | */ |
||
248 | 1 | protected function getStatusByEntityName($entityName, array &$result) |
|
249 | { |
||
250 | /** @var EntityManager $objectManager */ |
||
251 | 1 | $objectManager = $this->getObjectManager(); |
|
252 | 1 | $result1 = $objectManager->getRepository($entityName)->createQueryBuilder('j')->select('j.workerName, j.method, j.status, count(j) as c') |
|
253 | 1 | ->groupBy('j.workerName, j.method, j.status')->getQuery()->getArrayResult(); |
|
254 | |||
255 | 1 | foreach ($result1 as $item) { |
|
256 | $method = $item['workerName'].'->'.$item['method'].'()'; |
||
257 | if (!isset($result[$method])) { |
||
258 | $result[$method] = [BaseJob::STATUS_NEW => 0, |
||
259 | BaseJob::STATUS_RUNNING => 0, |
||
260 | RetryableJob::STATUS_EXPIRED => 0, |
||
261 | RetryableJob::STATUS_MAX_ERROR => 0, |
||
262 | RetryableJob::STATUS_MAX_STALLED => 0, |
||
263 | RetryableJob::STATUS_MAX_RETRIES => 0, |
||
264 | BaseJob::STATUS_SUCCESS => 0, |
||
265 | BaseJob::STATUS_ERROR => 0, ]; |
||
266 | } |
||
267 | $result[$method][$item['status']] += intval($item['c']); |
||
268 | 1 | } |
|
269 | 1 | } |
|
270 | |||
271 | /** |
||
272 | * Get the next job to run (can be filtered by workername and method name). |
||
273 | * |
||
274 | * @param string $workerName |
||
275 | * @param string $methodName |
||
276 | * @param bool $prioritize |
||
277 | * |
||
278 | * @return Job|null |
||
279 | */ |
||
280 | 1 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
281 | { |
||
282 | /** @var EntityManager $objectManager */ |
||
283 | 1 | $objectManager = $this->getObjectManager(); |
|
284 | |||
285 | 1 | $objectManager->beginTransaction(); |
|
286 | |||
287 | /** @var EntityRepository $repository */ |
||
288 | 1 | $repository = $this->getRepository(); |
|
289 | 1 | $qb = $repository->createQueryBuilder('j'); |
|
290 | 1 | $dateTime = new \DateTime(); |
|
291 | $qb |
||
292 | 1 | ->select('j') |
|
293 | 1 | ->where('j.status = :status')->setParameter(':status', BaseJob::STATUS_NEW) |
|
294 | 1 | ->andWhere('j.locked is NULL') |
|
295 | 1 | ->andWhere($qb->expr()->orX( |
|
296 | 1 | $qb->expr()->isNull('j.whenAt'), |
|
297 | 1 | $qb->expr()->lte('j.whenAt', ':whenAt') |
|
298 | 1 | )) |
|
299 | 1 | ->andWhere($qb->expr()->orX( |
|
300 | 1 | $qb->expr()->isNull('j.expiresAt'), |
|
301 | 1 | $qb->expr()->gt('j.expiresAt', ':expiresAt') |
|
302 | 1 | )) |
|
303 | 1 | ->setParameter(':whenAt', $dateTime) |
|
304 | 1 | ->setParameter(':expiresAt', $dateTime); |
|
305 | |||
306 | 1 | $this->addWorkerNameCriterion($qb, $workerName, $methodName); |
|
307 | |||
308 | 1 | if ($prioritize) { |
|
309 | 1 | $qb->add('orderBy', 'j.priority DESC, j.whenAt ASC'); |
|
|
|||
310 | 1 | } else { |
|
311 | $qb->orderBy('j.whenAt', 'ASC'); |
||
312 | } |
||
313 | 1 | $qb->setMaxResults(1); |
|
314 | |||
315 | /** @var QueryBuilder $qb */ |
||
316 | 1 | $query = $qb->getQuery(); |
|
317 | 1 | $query->setLockMode(LockMode::PESSIMISTIC_WRITE); |
|
318 | 1 | $jobs = $query->getResult(); |
|
319 | |||
320 | 1 | if ($jobs) { |
|
321 | /** @var Job $job */ |
||
322 | $job = $jobs[0]; |
||
323 | $job->setLocked(true); |
||
324 | $job->setLockedAt(new \DateTime()); |
||
325 | $job->setStatus(BaseJob::STATUS_RUNNING); |
||
326 | $job->setRunId($runId); |
||
327 | $objectManager->commit(); |
||
328 | $objectManager->flush(); |
||
329 | |||
330 | return $job; |
||
331 | } |
||
332 | |||
333 | 1 | $objectManager->rollback(); |
|
334 | |||
335 | 1 | return null; |
|
336 | } |
||
337 | |||
338 | /** |
||
339 | * Tries to update the nearest job as a batch. |
||
340 | * |
||
341 | * @param \Dtc\QueueBundle\Model\Job $job |
||
342 | * |
||
343 | * @return mixed|null |
||
344 | */ |
||
345 | 1 | public function updateNearestBatch(\Dtc\QueueBundle\Model\Job $job) |
|
382 | } |
||
383 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: