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 | } |
||
36 | |||
37 | 3 | if (null !== $method) { |
|
38 | 1 | $qb->andWhere('a.method = :method') |
|
39 | 1 | ->setParameter(':method', $workerName); |
|
40 | } |
||
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) |
|
130 | { |
||
131 | /** @var EntityManager $objectManager */ |
||
132 | 1 | $objectManager = $this->getObjectManager(); |
|
133 | 1 | $qb = $objectManager->createQueryBuilder()->select('j.status')->from($this->getObjectName(), 'j'); |
|
134 | 1 | $qb->where('j.id = :id')->setParameter(':id', $job->getId()); |
|
135 | |||
136 | 1 | return $qb->getQuery()->getSingleScalarResult(); |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Removes archived jobs older than $olderThan. |
||
141 | * |
||
142 | * @param \DateTime $olderThan |
||
143 | */ |
||
144 | public function pruneArchivedJobs(\DateTime $olderThan) |
||
145 | { |
||
146 | return $this->removeOlderThan($this->getArchiveObjectName(), |
||
147 | 'updatedAt', |
||
148 | $olderThan); |
||
149 | } |
||
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 | $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 | )) |
||
184 | 1 | ->andWhere($qb->expr()->orX( |
|
185 | 1 | $qb->expr()->isNull('j.expiresAt'), |
|
186 | 1 | $qb->expr()->gt('j.expiresAt', ':expiresAt') |
|
187 | )) |
||
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 | } 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() |
|
244 | |||
245 | /** |
||
246 | * @param string $entityName |
||
247 | */ |
||
248 | 1 | protected function getStatusByEntityName($entityName, array &$result) |
|
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) |
|
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: