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 | 9 | public function __construct(ObjectManager $objectManager, |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @return ObjectManager |
||
| 50 | */ |
||
| 51 | 32 | public function getObjectManager() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | 30 | public function getObjectName() |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | 23 | 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 | 23 | 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) |
|
| 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 | 4 | protected function getStalledJobs($workerName = null, $method = null) |
|
| 172 | |||
| 173 | 4 | protected function findRunningJobs($criterion, $count) |
|
| 192 | |||
| 193 | 4 | protected function extractStalledJobs(array $runningJobsById) |
|
| 218 | |||
| 219 | 6 | protected function updateMaxStatus(RetryableJob $job, $status, $max = null, $count = 0) |
|
| 229 | |||
| 230 | 2 | public function resetStalledJobs($workerName = null, $method = null) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $workerName |
||
| 265 | * @param string $method |
||
| 266 | */ |
||
| 267 | 2 | public function pruneStalledJobs($workerName = null, $method = null) |
|
| 289 | |||
| 290 | 10 | public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
|
| 296 | |||
| 297 | 2 | public function saveHistory(\Dtc\QueueBundle\Model\Job $job) |
|
| 298 | { |
||
| 299 | 2 | $this->deleteJob($job); // Should cause job to be archived |
|
| 300 | 2 | } |
|
| 301 | |||
| 302 | 29 | public function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
|
| 303 | { |
||
| 304 | // Todo: Serialize args |
||
| 305 | |||
| 306 | // Generate crc hash for the job |
||
| 307 | 29 | $hashValues = array($job->getClassName(), $job->getMethod(), $job->getWorkerName(), $job->getArgs()); |
|
| 308 | 29 | $crcHash = hash('sha256', serialize($hashValues)); |
|
| 309 | 29 | $job->setCrcHash($crcHash); |
|
| 310 | 29 | $objectManager = $this->getObjectManager(); |
|
| 311 | |||
| 312 | 29 | if (true === $job->getBatch()) { |
|
| 313 | // See if similar job that hasn't run exists |
||
| 314 | $criteria = array('crcHash' => $crcHash, 'status' => BaseJob::STATUS_NEW); |
||
| 315 | $oldJob = $this->getRepository()->findOneBy($criteria); |
||
| 316 | |||
| 317 | if ($oldJob) { |
||
| 318 | // Old job exists - just override fields Set higher priority |
||
| 319 | $oldJob->setPriority($this->findHigherPriority($job->getPriority(), $oldJob->getPriority())); |
||
| 320 | $oldJob->setWhenAt(min($job->getWhenAt(), $oldJob->getWhenAt())); |
||
| 321 | $oldJob->setBatch(true); |
||
| 322 | $objectManager->persist($oldJob); |
||
| 323 | $objectManager->flush(); |
||
| 324 | |||
| 325 | return $oldJob; |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | // Just save a new job |
||
| 330 | 29 | $this->resetSaveOk(__FUNCTION__); |
|
| 331 | 29 | $objectManager->persist($job); |
|
| 332 | 29 | $objectManager->flush(); |
|
| 333 | |||
| 334 | 29 | return $job; |
|
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param string $objectName |
||
| 339 | */ |
||
| 340 | abstract protected function stopIdGenerator($objectName); |
||
| 341 | |||
| 342 | abstract protected function restoreIdGenerator($objectName); |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param array $criterion |
||
| 346 | * @param int $limit |
||
| 347 | * @param int $offset |
||
| 348 | */ |
||
| 349 | 2 | private function resetJobsByCriterion( |
|
| 377 | |||
| 378 | 16 | protected function resetSaveOk($function) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @param RetryableJob $jobArchive |
||
| 384 | * @param $className |
||
| 385 | * @param $countProcessed |
||
| 386 | */ |
||
| 387 | 2 | protected function resetJob(RetryableJob $jobArchive, $className, &$countProcessed) |
|
| 413 | } |
||
| 414 |