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 |
||
| 16 | abstract class BaseJobManager extends PriorityJobManager |
||
| 17 | { |
||
| 18 | /** Number of jobs to prune / reset / gather at a time */ |
||
| 19 | const FETCH_COUNT = 100; |
||
| 20 | |||
| 21 | /** Number of seconds before a job is considered stalled if the runner is no longer active */ |
||
| 22 | const STALLED_SECONDS = 1800; |
||
| 23 | protected $objectManager; |
||
| 24 | protected $objectName; |
||
| 25 | protected $archiveObjectName; |
||
| 26 | protected $runClass; |
||
| 27 | protected $runArchiveClass; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param string $objectName |
||
| 31 | * @param string $archiveObjectName |
||
| 32 | * @param string $runClass |
||
| 33 | * @param string $runArchiveClass |
||
| 34 | */ |
||
| 35 | 11 | public function __construct(ObjectManager $objectManager, |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @return ObjectManager |
||
| 50 | */ |
||
| 51 | 38 | public function getObjectManager() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | 36 | public function getObjectName() |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | 24 | public function getArchiveObjectName() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | 4 | public function getRunClass() |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | 5 | public function getRunArchiveClass() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @return ObjectRepository |
||
| 90 | */ |
||
| 91 | 28 | public function getRepository() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $objectName |
||
| 98 | */ |
||
| 99 | abstract protected function countJobsByStatus($objectName, $status, $workerName = null, $method = null); |
||
| 100 | |||
| 101 | 2 | public function resetErroneousJobs($workerName = null, $method = null) |
|
| 102 | { |
||
| 103 | 2 | $count = $this->countJobsByStatus($this->getArchiveObjectName(), Job::STATUS_ERROR, $workerName, $method); |
|
| 104 | |||
| 105 | 2 | $criterion = ['status' => Job::STATUS_ERROR]; |
|
| 106 | 2 | $this->addWorkerNameMethod($criterion, $workerName, $method); |
|
| 107 | |||
| 108 | 2 | $countProcessed = 0; |
|
| 109 | 2 | for ($i = 0; $i < $count; $i += static::FETCH_COUNT) { |
|
| 110 | 2 | $countProcessed += $this->resetJobsByCriterion( |
|
| 111 | 2 | $criterion, static::FETCH_COUNT, $i); |
|
| 112 | 2 | } |
|
| 113 | |||
| 114 | 2 | return $countProcessed; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Sets the status to Job::STATUS_EXPIRED for those jobs that are expired. |
||
| 119 | * |
||
| 120 | * @param null $workerName |
||
| 121 | * @param null $method |
||
| 122 | * |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | abstract protected function updateExpired($workerName = null, $method = null); |
||
| 126 | |||
| 127 | 9 | protected function addWorkerNameMethod(array &$criterion, $workerName = null, $method = null) |
|
| 136 | |||
| 137 | 3 | public function pruneExpiredJobs($workerName = null, $method = null) |
|
| 160 | |||
| 161 | 7 | protected function flush() |
|
| 165 | |||
| 166 | 4 | protected function getStalledJobs($workerName = null, $method = null) |
|
| 177 | |||
| 178 | 4 | protected function findRunningJobs($criterion, $count) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @param $runId |
||
| 200 | * @param array $jobs |
||
| 201 | * @param array $stalledJobs |
||
| 202 | */ |
||
| 203 | 4 | protected function extractStalledLiveRuns($runId, array $jobs, array &$stalledJobs) |
|
| 204 | { |
||
| 205 | 4 | $objectManager = $this->getObjectManager(); |
|
| 206 | 4 | $runRepository = $objectManager->getRepository($this->runClass); |
|
| 207 | 4 | if ($run = $runRepository->find($runId)) { |
|
| 208 | 2 | foreach ($jobs as $job) { |
|
| 209 | 2 | if ($run->getCurrentJobId() == $job->getId()) { |
|
| 210 | 2 | continue; |
|
| 211 | } |
||
| 212 | 2 | $stalledJobs[] = $job; |
|
| 213 | 2 | } |
|
| 214 | 2 | } |
|
| 215 | 4 | } |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @param array $runningJobsById |
||
| 219 | * |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | 4 | protected function extractStalledJobs(array $runningJobsById) |
|
| 223 | { |
||
| 224 | 4 | $objectManager = $this->getObjectManager(); |
|
| 225 | /** @var EntityRepository|DocumentRepository $runArchiveRepository */ |
||
| 226 | 4 | $runArchiveRepository = $objectManager->getRepository($this->runArchiveClass); |
|
| 227 | |||
| 228 | 4 | $stalledJobs = []; |
|
| 229 | 4 | foreach (array_keys($runningJobsById) as $runId) { |
|
| 230 | 4 | $this->extractStalledLiveRuns($runId, $runningJobsById[$runId], $stalledJobs); |
|
| 231 | /** @var Run $run */ |
||
| 232 | 4 | if ($run = $runArchiveRepository->find($runId)) { |
|
| 233 | 4 | if ($endTime = $run->getEndedAt()) { |
|
| 234 | // Did it end over an hour ago |
||
| 235 | 4 | if ((time() - $endTime->getTimestamp()) > static::STALLED_SECONDS) { |
|
| 236 | 4 | $stalledJobs = array_merge($stalledJobs, $runningJobsById[$runId]); |
|
| 237 | 4 | } |
|
| 238 | 4 | } |
|
| 239 | 4 | } |
|
| 240 | 4 | } |
|
| 241 | |||
| 242 | 4 | return $stalledJobs; |
|
| 243 | } |
||
| 244 | |||
| 245 | 6 | protected function updateMaxStatus(RetryableJob $job, $status, $max = null, $count = 0) |
|
| 255 | |||
| 256 | 2 | protected function runStalledLoop($i, $count, array $stalledJobs, &$countProcessed) |
|
| 257 | { |
||
| 277 | |||
| 278 | 2 | public function resetStalledJobs($workerName = null, $method = null) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $workerName |
||
| 293 | * @param string $method |
||
| 294 | */ |
||
| 295 | 2 | public function pruneStalledJobs($workerName = null, $method = null) |
|
| 317 | |||
| 318 | 10 | public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
|
| 324 | |||
| 325 | 2 | public function saveHistory(\Dtc\QueueBundle\Model\Job $job) |
|
| 329 | |||
| 330 | 33 | protected function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
|
| 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( |
|
| 395 | |||
| 396 | 17 | 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) |
|
| 438 | } |
||
| 439 |