Complex classes like BaseJobManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseJobManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | abstract class BaseJobManager extends ArchivableJobManager |
||
20 | { |
||
21 | /** Number of jobs to prune / reset / gather at a time */ |
||
22 | const FETCH_COUNT = 100; |
||
23 | |||
24 | /** Number of seconds before a job is considered stalled if the runner is no longer active */ |
||
25 | const STALLED_SECONDS = 1800; |
||
26 | |||
27 | /** |
||
28 | * @var ObjectManager |
||
29 | */ |
||
30 | protected $objectManager; |
||
31 | |||
32 | /** |
||
33 | * BaseJobManager constructor. |
||
34 | * |
||
35 | * @param RunManager $runManager |
||
36 | * @param JobTimingManager $jobTimingManager |
||
37 | * @param ObjectManager $objectManager |
||
38 | * @param $jobClass |
||
39 | * @param $jobArchiveClass |
||
40 | */ |
||
41 | 20 | public function __construct( |
|
42 | RunManager $runManager, |
||
43 | JobTimingManager $jobTimingManager, |
||
44 | ObjectManager $objectManager, |
||
45 | $jobClass, |
||
46 | $jobArchiveClass |
||
47 | ) { |
||
48 | 20 | $this->objectManager = $objectManager; |
|
49 | 20 | parent::__construct($runManager, $jobTimingManager, $jobClass, $jobArchiveClass); |
|
50 | 20 | } |
|
51 | |||
52 | /** |
||
53 | * @return ObjectManager |
||
54 | */ |
||
55 | 53 | public function getObjectManager() |
|
56 | { |
||
57 | 53 | return $this->objectManager; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return ObjectRepository |
||
62 | */ |
||
63 | 40 | public function getRepository() |
|
64 | { |
||
65 | 40 | return $this->getObjectManager()->getRepository($this->getJobClass()); |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param string $objectName |
||
70 | */ |
||
71 | abstract protected function countJobsByStatus($objectName, $status, $workerName = null, $method = null); |
||
72 | |||
73 | 2 | public function resetErroneousJobs($workerName = null, $method = null) |
|
74 | { |
||
75 | 2 | $count = $this->countJobsByStatus($this->getJobArchiveClass(), Job::STATUS_ERROR, $workerName, $method); |
|
76 | |||
77 | 2 | $criterion = ['status' => Job::STATUS_ERROR]; |
|
78 | 2 | $this->addWorkerNameMethod($criterion, $workerName, $method); |
|
79 | |||
80 | 2 | $countProcessed = 0; |
|
81 | 2 | for ($i = 0; $i < $count; $i += static::FETCH_COUNT) { |
|
82 | 2 | $countProcessed += $this->resetJobsByCriterion( |
|
83 | 2 | $criterion, |
|
84 | 2 | static::FETCH_COUNT, |
|
85 | 2 | $i |
|
86 | ); |
||
87 | } |
||
88 | |||
89 | 2 | return $countProcessed; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Sets the status to Job::STATUS_EXPIRED for those jobs that are expired. |
||
94 | * |
||
95 | * @param null $workerName |
||
96 | * @param null $method |
||
97 | * |
||
98 | * @return mixed |
||
99 | */ |
||
100 | abstract protected function updateExpired($workerName = null, $method = null); |
||
101 | |||
102 | 9 | protected function addWorkerNameMethod(array &$criterion, $workerName = null, $method = null) |
|
103 | { |
||
104 | 9 | if (null !== $workerName) { |
|
105 | 4 | $criterion['workerName'] = $workerName; |
|
106 | } |
||
107 | 9 | if (null !== $method) { |
|
108 | 4 | $criterion['method'] = $method; |
|
109 | } |
||
110 | 9 | } |
|
111 | |||
112 | 3 | public function pruneExpiredJobs($workerName = null, $method = null) |
|
113 | { |
||
114 | 3 | $count = $this->updateExpired($workerName, $method); |
|
115 | 3 | $criterion = ['status' => Job::STATUS_EXPIRED]; |
|
116 | 3 | $this->addWorkerNameMethod($criterion, $workerName, $method); |
|
117 | 3 | $objectManager = $this->getObjectManager(); |
|
118 | 3 | $repository = $this->getRepository(); |
|
119 | 3 | $finalCount = 0; |
|
120 | 3 | for ($i = 0; $i < $count; $i += static::FETCH_COUNT) { |
|
121 | 3 | $expiredJobs = $repository->findBy($criterion, null, static::FETCH_COUNT, $i); |
|
122 | 3 | $innerCount = 0; |
|
123 | 3 | if (!empty($expiredJobs)) { |
|
124 | 3 | foreach ($expiredJobs as $expiredJob) { |
|
125 | /* @var Job $expiredJob */ |
||
126 | 3 | $expiredJob->setStatus(Job::STATUS_EXPIRED); |
|
127 | 3 | $objectManager->remove($expiredJob); |
|
128 | 3 | ++$finalCount; |
|
129 | 3 | ++$innerCount; |
|
130 | } |
||
131 | } |
||
132 | 3 | $this->flush(); |
|
133 | 3 | for ($j = 0; $j < $innerCount; ++$j) { |
|
134 | 3 | $this->jobTiminigManager->recordTiming(JobTiming::STATUS_FINISHED_EXPIRED); |
|
135 | } |
||
136 | } |
||
137 | |||
138 | 3 | return $finalCount; |
|
139 | } |
||
140 | |||
141 | 9 | protected function flush() |
|
142 | { |
||
143 | 9 | $this->getObjectManager()->flush(); |
|
144 | 9 | } |
|
145 | |||
146 | 4 | protected function getStalledJobs($workerName = null, $method = null) |
|
147 | { |
||
148 | 4 | $count = $this->countJobsByStatus($this->getJobClass(), Job::STATUS_RUNNING, $workerName, $method); |
|
149 | |||
150 | 4 | $criterion = ['status' => BaseJob::STATUS_RUNNING]; |
|
151 | 4 | $this->addWorkerNameMethod($criterion, $workerName, $method); |
|
152 | |||
153 | 4 | $runningJobs = $this->findRunningJobs($criterion, $count); |
|
154 | |||
155 | 4 | return $this->extractStalledJobs($runningJobs); |
|
156 | } |
||
157 | |||
158 | 4 | protected function findRunningJobs($criterion, $count) |
|
159 | { |
||
160 | 4 | $repository = $this->getRepository(); |
|
161 | 4 | $runningJobsById = []; |
|
162 | |||
163 | 4 | for ($i = 0; $i < $count; $i += static::FETCH_COUNT) { |
|
164 | 4 | $runningJobs = $repository->findBy($criterion, null, static::FETCH_COUNT, $i); |
|
165 | 4 | if (!empty($runningJobs)) { |
|
166 | 4 | foreach ($runningJobs as $job) { |
|
167 | /** @var RetryableJob $job */ |
||
168 | 4 | if (null !== $runId = $job->getRunId()) { |
|
169 | 4 | $runningJobsById[$runId][] = $job; |
|
170 | } |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | |||
175 | 4 | return $runningJobsById; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param $runId |
||
180 | * @param array $jobs |
||
181 | * @param array $stalledJobs |
||
182 | */ |
||
183 | 4 | protected function extractStalledLiveRuns($runId, array $jobs, array &$stalledJobs) |
|
184 | { |
||
185 | 4 | $objectManager = $this->getObjectManager(); |
|
186 | 4 | $runRepository = $objectManager->getRepository($this->getRunManager()->getRunClass()); |
|
187 | 4 | if ($run = $runRepository->find($runId)) { |
|
188 | 2 | foreach ($jobs as $job) { |
|
189 | 2 | if ($run->getCurrentJobId() == $job->getId()) { |
|
190 | 2 | continue; |
|
191 | } |
||
192 | 2 | $stalledJobs[] = $job; |
|
193 | } |
||
194 | } |
||
195 | 4 | } |
|
196 | |||
197 | /** |
||
198 | * @param array $runningJobsById |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | 4 | protected function extractStalledJobs(array $runningJobsById) |
|
203 | { |
||
204 | 4 | $stalledJobs = []; |
|
205 | 4 | foreach (array_keys($runningJobsById) as $runId) { |
|
206 | 4 | $this->extractStalledLiveRuns($runId, $runningJobsById[$runId], $stalledJobs); |
|
207 | 4 | $this->extractStalledJobsRunArchive($runningJobsById, $stalledJobs, $runId); |
|
208 | } |
||
209 | |||
210 | 4 | return $stalledJobs; |
|
211 | } |
||
212 | |||
213 | 4 | protected function extractStalledJobsRunArchive(array $runningJobsById, array &$stalledJobs, $runId) |
|
214 | { |
||
215 | 4 | $runManager = $this->getRunManager(); |
|
216 | 4 | if (!method_exists($runManager, 'getObjectManager')) { |
|
217 | return; |
||
218 | } |
||
219 | 4 | if (!method_exists($runManager, 'getRunArchiveClass')) { |
|
220 | return; |
||
221 | } |
||
222 | |||
223 | /** @var EntityRepository|DocumentRepository $runArchiveRepository */ |
||
224 | 4 | $runArchiveRepository = $runManager->getObjectManager()->getRepository($runManager->getRunArchiveClass()); |
|
225 | /** @var Run $run */ |
||
226 | 4 | if ($run = $runArchiveRepository->find($runId)) { |
|
227 | 4 | if ($endTime = $run->getEndedAt()) { |
|
228 | // Did it end over an hour ago |
||
229 | 4 | if ((time() - $endTime->getTimestamp()) > static::STALLED_SECONDS) { |
|
230 | 4 | $stalledJobs = array_merge($stalledJobs, $runningJobsById[$runId]); |
|
231 | } |
||
232 | } |
||
233 | } |
||
234 | 4 | } |
|
235 | |||
236 | 6 | protected function updateMaxStatus(RetryableJob $job, $status, $max = null, $count = 0) |
|
237 | { |
||
238 | 6 | if (null !== $max && $count >= $max) { |
|
239 | 4 | $job->setStatus($status); |
|
240 | |||
241 | 4 | return true; |
|
242 | } |
||
243 | |||
244 | 6 | return false; |
|
245 | } |
||
246 | |||
247 | 2 | protected function runStalledLoop($i, $count, array $stalledJobs, &$countProcessed) |
|
248 | { |
||
249 | 2 | $objectManager = $this->getObjectManager(); |
|
250 | 2 | $newCount = 0; |
|
251 | 2 | for ($j = $i, $max = $i + static::FETCH_COUNT; $j < $max && $j < $count; ++$j) { |
|
252 | /* RetryableJob $job */ |
||
253 | 2 | $job = $stalledJobs[$j]; |
|
254 | 2 | $job->setStalledCount($job->getStalledCount() + 1); |
|
255 | 2 | if ($this->updateMaxStatus($job, RetryableJob::STATUS_MAX_STALLED, $job->getMaxStalled(), $job->getStalledCount()) || |
|
256 | 2 | $this->updateMaxStatus($job, RetryableJob::STATUS_MAX_RETRIES, $job->getMaxRetries(), $job->getRetries())) { |
|
257 | 2 | $objectManager->remove($job); |
|
258 | 2 | $this->jobTiminigManager->recordTiming(JobTiming::STATUS_FINISHED_STALLED); |
|
259 | 2 | continue; |
|
260 | } |
||
261 | |||
262 | 2 | $job->setRetries($job->getRetries() + 1); |
|
263 | 2 | $job->setStatus(BaseJob::STATUS_NEW); |
|
264 | 2 | $job->setLocked(null); |
|
265 | 2 | $job->setLockedAt(null); |
|
266 | 2 | $objectManager->persist($job); |
|
267 | 2 | ++$newCount; |
|
268 | 2 | ++$countProcessed; |
|
269 | } |
||
270 | |||
271 | 2 | return $newCount; |
|
272 | } |
||
273 | |||
274 | 2 | public function resetStalledJobs($workerName = null, $method = null) |
|
275 | { |
||
276 | 2 | $stalledJobs = $this->getStalledJobs($workerName, $method); |
|
277 | |||
278 | 2 | $countProcessed = 0; |
|
279 | 2 | for ($i = 0, $count = count($stalledJobs); $i < $count; $i += static::FETCH_COUNT) { |
|
280 | 2 | $newCount = $this->runStalledLoop($i, $count, $stalledJobs, $countProcessed); |
|
281 | 2 | $this->flush(); |
|
282 | 2 | for ($j = 0; $j < $newCount; ++$j) { |
|
283 | 2 | $this->jobTiminigManager->recordTiming(JobTiming::STATUS_FINISHED_STALLED); |
|
284 | 2 | $this->jobTiminigManager->recordTiming(JobTiming::STATUS_INSERT); |
|
285 | } |
||
286 | } |
||
287 | |||
288 | 2 | return $countProcessed; |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param string $workerName |
||
293 | * @param string $method |
||
294 | */ |
||
295 | 2 | public function pruneStalledJobs($workerName = null, $method = null) |
|
296 | { |
||
297 | 2 | $stalledJobs = $this->getStalledJobs($workerName, $method); |
|
298 | 2 | $objectManager = $this->getObjectManager(); |
|
299 | |||
300 | 2 | $countProcessed = 0; |
|
301 | 2 | for ($i = 0, $count = count($stalledJobs); $i < $count; $i += static::FETCH_COUNT) { |
|
302 | 2 | for ($j = $i, $max = $i + static::FETCH_COUNT; $j < $max && $j < $count; ++$j) { |
|
303 | /** @var RetryableJob $job */ |
||
304 | 2 | $job = $stalledJobs[$j]; |
|
305 | 2 | $job->setStalledCount($job->getStalledCount() + 1); |
|
306 | 2 | $job->setStatus(BaseJob::STATUS_ERROR); |
|
307 | 2 | $job->setMessage('stalled'); |
|
308 | 2 | $this->updateMaxStatus($job, RetryableJob::STATUS_MAX_STALLED, $job->getMaxStalled(), $job->getStalledCount()); |
|
309 | 2 | $objectManager->remove($job); |
|
310 | 2 | ++$countProcessed; |
|
311 | } |
||
312 | 2 | $this->flush(); |
|
313 | } |
||
314 | |||
315 | 2 | return $countProcessed; |
|
316 | } |
||
317 | |||
318 | 12 | public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
|
324 | |||
325 | 2 | public function saveHistory(\Dtc\QueueBundle\Model\Job $job) |
|
329 | |||
330 | 39 | protected function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
|
331 | { |
||
332 | // Generate crc hash for the job |
||
333 | 39 | $hashValues = array($job->getClassName(), $job->getMethod(), $job->getWorkerName(), $job->getArgs()); |
|
334 | 39 | $crcHash = hash('sha256', serialize($hashValues)); |
|
335 | 39 | $job->setCrcHash($crcHash); |
|
336 | 39 | $objectManager = $this->getObjectManager(); |
|
337 | |||
338 | 39 | if (true === $job->getBatch()) { |
|
339 | 2 | $oldJob = $this->updateNearestBatch($job); |
|
340 | 2 | if ($oldJob) { |
|
341 | 2 | return $oldJob; |
|
342 | } |
||
343 | } |
||
344 | |||
345 | // Just save a new job |
||
346 | 39 | $this->resetSaveOk(__FUNCTION__); |
|
347 | 39 | $objectManager->persist($job); |
|
348 | 39 | $objectManager->flush(); |
|
349 | |||
350 | 39 | return $job; |
|
351 | } |
||
352 | |||
353 | abstract protected function updateNearestBatch(Job $job); |
||
354 | |||
355 | /** |
||
356 | * @param string $objectName |
||
357 | */ |
||
358 | abstract protected function stopIdGenerator($objectName); |
||
359 | |||
360 | abstract protected function restoreIdGenerator($objectName); |
||
361 | |||
362 | /** |
||
363 | * @param array $criterion |
||
364 | * @param int $limit |
||
365 | * @param int $offset |
||
366 | */ |
||
367 | 2 | private function resetJobsByCriterion( |
|
368 | array $criterion, |
||
369 | $limit, |
||
370 | $offset |
||
371 | ) { |
||
372 | 2 | $objectManager = $this->getObjectManager(); |
|
373 | 2 | $this->resetSaveOk(__FUNCTION__); |
|
374 | 2 | $objectName = $this->getJobClass(); |
|
375 | 2 | $archiveObjectName = $this->getJobArchiveClass(); |
|
376 | 2 | $jobRepository = $objectManager->getRepository($objectName); |
|
377 | 2 | $jobArchiveRepository = $objectManager->getRepository($archiveObjectName); |
|
378 | 2 | $className = $jobRepository->getClassName(); |
|
379 | 2 | $metadata = $objectManager->getClassMetadata($className); |
|
380 | 2 | $this->stopIdGenerator($objectName); |
|
381 | 2 | $identifierData = $metadata->getIdentifier(); |
|
382 | 2 | $idColumn = isset($identifierData[0]) ? $identifierData[0] : 'id'; |
|
383 | 2 | $results = $jobArchiveRepository->findBy($criterion, [$idColumn => 'ASC'], $limit, $offset); |
|
384 | 2 | $countProcessed = 0; |
|
385 | |||
386 | 2 | foreach ($results as $jobArchive) { |
|
387 | 2 | $this->resetJob($jobArchive, $className, $countProcessed); |
|
388 | } |
||
389 | 2 | $objectManager->flush(); |
|
390 | |||
391 | 2 | $this->restoreIdGenerator($objectName); |
|
392 | |||
393 | 2 | return $countProcessed; |
|
394 | } |
||
395 | |||
396 | 20 | protected function resetSaveOk($function) |
|
399 | |||
400 | /** |
||
401 | * @param null $workerName |
||
402 | * @param null $methodName |
||
403 | * @param bool $prioritize |
||
404 | */ |
||
405 | abstract public function getJobQueryBuilder($workerName = null, $methodName = null, $prioritize = true); |
||
406 | |||
407 | /** |
||
408 | * @param RetryableJob $jobArchive |
||
409 | * @param $className |
||
410 | * @param $countProcessed |
||
411 | */ |
||
412 | 2 | protected function resetJob(RetryableJob $jobArchive, $className, &$countProcessed) |
|
413 | { |
||
414 | 2 | $objectManager = $this->getObjectManager(); |
|
415 | 2 | if ($this->updateMaxStatus($jobArchive, RetryableJob::STATUS_MAX_RETRIES, $jobArchive->getMaxRetries(), $jobArchive->getRetries())) { |
|
416 | 2 | $objectManager->persist($jobArchive); |
|
417 | |||
418 | 2 | return; |
|
419 | } |
||
420 | |||
421 | /** @var RetryableJob $job */ |
||
422 | 2 | $job = new $className(); |
|
423 | |||
424 | 2 | Util::copy($jobArchive, $job); |
|
425 | 2 | $job->setStatus(BaseJob::STATUS_NEW); |
|
426 | 2 | $job->setLocked(null); |
|
427 | 2 | $job->setLockedAt(null); |
|
428 | 2 | $job->setMessage(null); |
|
429 | 2 | $job->setFinishedAt(null); |
|
430 | 2 | $job->setStartedAt(null); |
|
431 | 2 | $job->setElapsed(null); |
|
432 | 2 | $job->setRetries($job->getRetries() + 1); |
|
433 | 2 | $objectManager->persist($job); |
|
434 | 2 | $objectManager->remove($jobArchive); |
|
435 | 2 | $this->jobTiminigManager->recordTiming(JobTiming::STATUS_INSERT); |
|
436 | 2 | ++$countProcessed; |
|
437 | 2 | } |
|
438 | |||
439 | abstract public function getWorkersAndMethods(); |
||
440 | |||
441 | abstract public function countLiveJobs($workerName = null, $methodName = null); |
||
442 | |||
443 | abstract public function archiveAllJobs($workerName = null, $methodName = null, $progressCallback); |
||
444 | } |
||
445 |