1 | <?php |
||
14 | class JobManager extends BaseJobManager |
||
15 | { |
||
16 | use CommonTrait; |
||
17 | protected static $saveInsertCalled = null; |
||
18 | protected static $resetInsertCalled = null; |
||
19 | |||
20 | 3 | public function countJobsByStatus($objectName, $status, $workerName = null, $method = null) |
|
21 | { |
||
22 | /** @var EntityManager $objectManager */ |
||
23 | 3 | $objectManager = $this->getObjectManager(); |
|
24 | |||
25 | $queryBuilder = $objectManager |
||
26 | 3 | ->createQueryBuilder() |
|
27 | 3 | ->select('count(a.id)') |
|
28 | 3 | ->from($objectName, 'a') |
|
29 | 3 | ->where('a.status = :status'); |
|
30 | |||
31 | 3 | if (null !== $workerName) { |
|
32 | 1 | $queryBuilder->andWhere('a.workerName = :workerName') |
|
33 | 1 | ->setParameter(':workerName', $workerName); |
|
34 | 1 | } |
|
35 | |||
36 | 3 | if (null !== $method) { |
|
37 | 1 | $queryBuilder->andWhere('a.method = :method') |
|
38 | 1 | ->setParameter(':method', $workerName); |
|
39 | 1 | } |
|
40 | |||
41 | 3 | $count = $queryBuilder->setParameter(':status', $status) |
|
42 | 3 | ->getQuery()->getSingleScalarResult(); |
|
43 | |||
44 | 3 | if (!$count) { |
|
45 | 1 | return 0; |
|
46 | } |
||
47 | |||
48 | 3 | return $count; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param string|null $workerName |
||
53 | * @param string|null $method |
||
54 | * |
||
55 | * @return int Count of jobs pruned |
||
56 | */ |
||
57 | 1 | public function pruneErroneousJobs($workerName = null, $method = null) |
|
70 | |||
71 | 16 | protected function resetSaveOk($function) |
|
72 | 1 | { |
|
73 | 16 | $objectManager = $this->getObjectManager(); |
|
74 | 16 | $splObjectHash = spl_object_hash($objectManager); |
|
75 | |||
76 | 16 | if ('save' === $function) { |
|
77 | $compare = static::$resetInsertCalled; |
||
78 | } else { |
||
79 | 16 | $compare = static::$saveInsertCalled; |
|
80 | } |
||
81 | |||
82 | 16 | if ($splObjectHash === $compare) { |
|
83 | // Insert SQL is cached... |
||
84 | $msg = "Can't call save and reset within the same process cycle (or using the same EntityManager)"; |
||
85 | throw new LogicException($msg); |
||
86 | } |
||
87 | |||
88 | 16 | if ('save' === $function) { |
|
89 | static::$saveInsertCalled = spl_object_hash($objectManager); |
||
90 | } else { |
||
91 | 16 | static::$resetInsertCalled = spl_object_hash($objectManager); |
|
92 | } |
||
93 | 16 | } |
|
94 | |||
95 | /** |
||
96 | * @param string $workerName |
||
97 | * @param string $method |
||
98 | */ |
||
99 | 10 | protected function addWorkerNameCriterion(QueryBuilder $queryBuilder, $workerName = null, $method = null) |
|
100 | { |
||
101 | 10 | if (null !== $workerName) { |
|
102 | 3 | $queryBuilder->andWhere('j.workerName = :workerName')->setParameter(':workerName', $workerName); |
|
103 | 3 | } |
|
104 | |||
105 | 10 | if (null !== $method) { |
|
106 | 2 | $queryBuilder->andWhere('j.method = :method')->setParameter(':method', $method); |
|
107 | 2 | } |
|
108 | 10 | } |
|
109 | |||
110 | 1 | protected function updateExpired($workerName = null, $method = null) |
|
127 | |||
128 | /** |
||
129 | * Removes archived jobs older than $olderThan. |
||
130 | * |
||
131 | * @param \DateTime $olderThan |
||
132 | */ |
||
133 | 1 | public function pruneArchivedJobs(\DateTime $olderThan) |
|
139 | |||
140 | 2 | public function getJobCount($workerName = null, $method = null) |
|
141 | { |
||
142 | /** @var EntityManager $objectManager */ |
||
185 | |||
186 | /** |
||
187 | * Get Jobs statuses. |
||
188 | */ |
||
189 | 2 | public function getStatus() |
|
190 | { |
||
191 | 2 | $result = []; |
|
192 | 2 | $this->getStatusByEntityName($this->getObjectName(), $result); |
|
193 | 2 | $this->getStatusByEntityName($this->getArchiveObjectName(), $result); |
|
194 | |||
195 | 2 | $finalResult = []; |
|
196 | 2 | foreach ($result as $key => $item) { |
|
197 | 1 | ksort($item); |
|
198 | 1 | foreach ($item as $status => $count) { |
|
199 | 1 | if (isset($finalResult[$key][$status])) { |
|
200 | $finalResult[$key][$status] += $count; |
||
201 | } else { |
||
202 | 1 | $finalResult[$key][$status] = $count; |
|
203 | } |
||
204 | 1 | } |
|
205 | 2 | } |
|
206 | |||
207 | 2 | return $finalResult; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param string $entityName |
||
212 | */ |
||
213 | 2 | protected function getStatusByEntityName($entityName, array &$result) |
|
214 | { |
||
215 | /** @var EntityManager $objectManager */ |
||
216 | 2 | $objectManager = $this->getObjectManager(); |
|
217 | 2 | $result1 = $objectManager->getRepository($entityName)->createQueryBuilder('j')->select('j.workerName, j.method, j.status, count(j) as c') |
|
218 | 2 | ->groupBy('j.workerName, j.method, j.status')->getQuery()->getArrayResult(); |
|
219 | |||
220 | 2 | foreach ($result1 as $item) { |
|
221 | 1 | $method = $item['workerName'].'->'.$item['method'].'()'; |
|
222 | 1 | if (!isset($result[$method])) { |
|
223 | 1 | $result[$method] = [BaseJob::STATUS_NEW => 0, |
|
224 | 1 | BaseJob::STATUS_RUNNING => 0, |
|
225 | 1 | RetryableJob::STATUS_EXPIRED => 0, |
|
226 | 1 | RetryableJob::STATUS_MAX_ERROR => 0, |
|
227 | 1 | RetryableJob::STATUS_MAX_STALLED => 0, |
|
228 | 1 | RetryableJob::STATUS_MAX_RETRIES => 0, |
|
229 | 1 | BaseJob::STATUS_SUCCESS => 0, |
|
230 | 1 | BaseJob::STATUS_ERROR => 0, ]; |
|
231 | 1 | } |
|
232 | 1 | $result[$method][$item['status']] += intval($item['c']); |
|
233 | 2 | } |
|
234 | 2 | } |
|
235 | |||
236 | /** |
||
237 | * Get the next job to run (can be filtered by workername and method name). |
||
238 | * |
||
239 | * @param string $workerName |
||
240 | * @param string $methodName |
||
241 | * @param bool $prioritize |
||
242 | * |
||
243 | * @return Job|null |
||
244 | */ |
||
245 | 8 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
256 | |||
257 | /** |
||
258 | * @param null $workerName |
||
259 | * @param null $methodName |
||
260 | * @param bool $prioritize |
||
261 | * |
||
262 | * @return QueryBuilder |
||
263 | */ |
||
264 | 8 | public function getJobQueryBuilder($workerName = null, $methodName = null, $prioritize = true) |
|
265 | { |
||
266 | /** @var EntityRepository $repository */ |
||
267 | 8 | $repository = $this->getRepository(); |
|
268 | 8 | $queryBuilder = $repository->createQueryBuilder('j'); |
|
269 | 8 | $dateTime = new \DateTime(); |
|
270 | $queryBuilder |
||
271 | 8 | ->select('j.id') |
|
272 | 8 | ->where('j.status = :status')->setParameter(':status', BaseJob::STATUS_NEW) |
|
273 | 8 | ->andWhere('j.locked is NULL') |
|
274 | 8 | ->andWhere($queryBuilder->expr()->orX( |
|
275 | 8 | $queryBuilder->expr()->isNull('j.whenAt'), |
|
276 | 8 | $queryBuilder->expr()->lte('j.whenAt', ':whenAt') |
|
277 | 8 | )) |
|
278 | 8 | ->andWhere($queryBuilder->expr()->orX( |
|
279 | 8 | $queryBuilder->expr()->isNull('j.expiresAt'), |
|
280 | 8 | $queryBuilder->expr()->gt('j.expiresAt', ':expiresAt') |
|
281 | 8 | )) |
|
282 | 8 | ->setParameter(':whenAt', $dateTime) |
|
283 | 8 | ->setParameter(':expiresAt', $dateTime); |
|
284 | |||
285 | 8 | $this->addWorkerNameCriterion($queryBuilder, $workerName, $methodName); |
|
286 | |||
287 | 8 | if ($prioritize) { |
|
288 | 8 | $queryBuilder->addOrderBy('j.priority', 'DESC'); |
|
289 | 8 | $queryBuilder->addOrderBy('j.whenAt', 'ASC'); |
|
290 | 8 | } else { |
|
291 | 1 | $queryBuilder->orderBy('j.whenAt', 'ASC'); |
|
292 | } |
||
293 | |||
294 | 8 | return $queryBuilder; |
|
295 | } |
||
296 | |||
297 | 8 | protected function takeJob($jobs, $runId = null) |
|
298 | { |
||
299 | 8 | if (isset($jobs[0]['id'])) { |
|
300 | /** @var EntityRepository $repository */ |
||
301 | 7 | $repository = $this->getRepository(); |
|
302 | /** @var QueryBuilder $queryBuilder */ |
||
303 | 7 | $queryBuilder = $repository->createQueryBuilder('j'); |
|
304 | $queryBuilder |
||
305 | 7 | ->update() |
|
306 | 7 | ->set('j.locked', ':locked') |
|
307 | 7 | ->setParameter(':locked', true) |
|
308 | 7 | ->set('j.lockedAt', ':lockedAt') |
|
309 | 7 | ->setParameter(':lockedAt', new \DateTime()) |
|
310 | 7 | ->set('j.status', ':status') |
|
311 | 7 | ->setParameter(':status', BaseJob::STATUS_RUNNING); |
|
312 | 7 | if (null !== $runId) { |
|
313 | $queryBuilder |
||
314 | 1 | ->set('j.runId', ':runId') |
|
315 | 1 | ->setParameter(':runId', $runId); |
|
316 | 1 | } |
|
317 | 7 | $queryBuilder->where('j.id = :id'); |
|
318 | 7 | $queryBuilder->andWhere('j.locked is NULL'); |
|
319 | 7 | $queryBuilder->setParameter(':id', $jobs[0]['id']); |
|
320 | 7 | $resultCount = $queryBuilder->getQuery()->execute(); |
|
321 | |||
322 | 7 | if (1 === $resultCount) { |
|
323 | 7 | return $repository->find($jobs[0]['id']); |
|
324 | } |
||
325 | } |
||
326 | |||
327 | 4 | return null; |
|
328 | } |
||
329 | |||
330 | /** |
||
331 | * Tries to update the nearest job as a batch. |
||
332 | * |
||
333 | * @param \Dtc\QueueBundle\Model\Job $job |
||
334 | * |
||
335 | * @return mixed|null |
||
336 | */ |
||
337 | 1 | public function updateNearestBatch(\Dtc\QueueBundle\Model\Job $job) |
|
363 | |||
364 | 1 | protected function updateBatchJob(Job $existingJob, $newPriority, $newWhenAt) |
|
365 | { |
||
366 | 1 | $existingPriority = $existingJob->getPriority(); |
|
367 | 1 | $existingWhenAt = $existingJob->getWhenAt(); |
|
393 | } |
||
394 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.