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 | 36 | public function getObjectManager() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | 34 | 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 | 27 | 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 | 8 | 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 | } |
||
| 214 | } |
||
| 215 | 4 | } |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @param array $runningJobsById |
||
| 219 | * |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | 4 | protected function extractStalledJobs(array $runningJobsById) |
|
| 244 | |||
| 245 | 6 | protected function updateMaxStatus(RetryableJob $job, $status, $max = null, $count = 0) |
|
| 255 | |||
| 256 | abstract protected function getJobCurrentStatus(Job $job); |
||
| 257 | |||
| 258 | 2 | protected function runStalledLoop($i, $count, array $stalledJobs, &$countProcessed) |
|
| 259 | { |
||
| 260 | 2 | $objectManager = $this->getObjectManager(); |
|
| 261 | 2 | for ($j = $i, $max = $i + static::FETCH_COUNT; $j < $max && $j < $count; ++$j) { |
|
| 262 | /* RetryableJob $job */ |
||
| 263 | 2 | $job = $stalledJobs[$j]; |
|
| 264 | 2 | $status = $this->getJobCurrentStatus($job); |
|
| 265 | |||
| 266 | // Query the data store to make sure the job is still marked running |
||
| 267 | 2 | if (BaseJob::STATUS_RUNNING !== $status) { |
|
| 268 | continue; |
||
| 269 | } |
||
| 270 | |||
| 271 | 2 | $job->setStalledCount($job->getStalledCount() + 1); |
|
| 272 | 2 | if ($this->updateMaxStatus($job, RetryableJob::STATUS_MAX_STALLED, $job->getMaxStalled(), $job->getStalledCount())) { |
|
| 273 | 2 | $objectManager->remove($job); |
|
| 274 | 2 | continue; |
|
| 275 | 2 | } elseif ($this->updateMaxStatus($job, RetryableJob::STATUS_MAX_RETRIES, $job->getMaxRetries(), $job->getRetries())) { |
|
| 276 | 2 | $objectManager->remove($job); |
|
| 277 | 2 | continue; |
|
| 278 | } |
||
| 279 | |||
| 280 | 2 | $job->setRetries($job->getRetries() + 1); |
|
| 281 | 2 | $job->setStatus(BaseJob::STATUS_NEW); |
|
| 282 | 2 | $job->setLocked(null); |
|
| 283 | 2 | $job->setLockedAt(null); |
|
| 284 | 2 | $objectManager->persist($job); |
|
| 285 | 2 | ++$countProcessed; |
|
| 286 | } |
||
| 287 | 2 | } |
|
| 288 | |||
| 289 | 2 | public function resetStalledJobs($workerName = null, $method = null) |
|
| 290 | { |
||
| 291 | 2 | $stalledJobs = $this->getStalledJobs($workerName, $method); |
|
| 292 | |||
| 293 | 2 | $countProcessed = 0; |
|
| 294 | 2 | for ($i = 0, $count = count($stalledJobs); $i < $count; $i += static::FETCH_COUNT) { |
|
| 295 | 2 | $this->runStalledLoop($i, $count, $stalledJobs, $countProcessed); |
|
| 296 | 2 | $this->flush(); |
|
| 297 | } |
||
| 298 | |||
| 299 | 2 | return $countProcessed; |
|
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $workerName |
||
| 304 | * @param string $method |
||
| 305 | */ |
||
| 306 | 2 | public function pruneStalledJobs($workerName = null, $method = null) |
|
| 307 | { |
||
| 308 | 2 | $stalledJobs = $this->getStalledJobs($workerName, $method); |
|
| 309 | 2 | $objectManager = $this->getObjectManager(); |
|
| 310 | |||
| 311 | 2 | $countProcessed = 0; |
|
| 312 | 2 | for ($i = 0, $count = count($stalledJobs); $i < $count; $i += static::FETCH_COUNT) { |
|
| 313 | 2 | for ($j = $i, $max = $i + static::FETCH_COUNT; $j < $max && $j < $count; ++$j) { |
|
| 314 | /** @var RetryableJob $job */ |
||
| 315 | 2 | $job = $stalledJobs[$j]; |
|
| 316 | 2 | $job->setStalledCount($job->getStalledCount() + 1); |
|
| 317 | 2 | $job->setStatus(BaseJob::STATUS_ERROR); |
|
| 318 | 2 | $job->setMessage('stalled'); |
|
| 319 | 2 | $this->updateMaxStatus($job, RetryableJob::STATUS_MAX_STALLED, $job->getMaxStalled(), $job->getStalledCount()); |
|
| 320 | 2 | $objectManager->remove($job); |
|
| 321 | 2 | ++$countProcessed; |
|
| 322 | } |
||
| 323 | 2 | $this->flush(); |
|
| 324 | } |
||
| 325 | |||
| 326 | 2 | return $countProcessed; |
|
| 327 | } |
||
| 328 | |||
| 329 | 10 | public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
|
| 330 | { |
||
| 331 | 10 | $objectManager = $this->getObjectManager(); |
|
| 332 | 10 | $objectManager->remove($job); |
|
| 333 | 10 | $objectManager->flush(); |
|
| 334 | 10 | } |
|
| 335 | |||
| 336 | 2 | public function saveHistory(\Dtc\QueueBundle\Model\Job $job) |
|
| 337 | { |
||
| 338 | 2 | $this->deleteJob($job); // Should cause job to be archived |
|
| 339 | 2 | } |
|
| 340 | |||
| 341 | 31 | protected function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
|
| 342 | { |
||
| 343 | // Generate crc hash for the job |
||
| 344 | 31 | $hashValues = array($job->getClassName(), $job->getMethod(), $job->getWorkerName(), $job->getArgs()); |
|
| 345 | 31 | $crcHash = hash('sha256', serialize($hashValues)); |
|
| 346 | 31 | $job->setCrcHash($crcHash); |
|
| 347 | 31 | $objectManager = $this->getObjectManager(); |
|
| 348 | |||
| 349 | 31 | if (true === $job->getBatch()) { |
|
| 350 | 2 | $oldJob = $this->updateNearestBatch($job); |
|
| 351 | 2 | if ($oldJob) { |
|
| 352 | 2 | return $oldJob; |
|
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | // Just save a new job |
||
| 357 | 31 | $this->resetSaveOk(__FUNCTION__); |
|
| 358 | 31 | $objectManager->persist($job); |
|
| 359 | 31 | $objectManager->flush(); |
|
| 360 | |||
| 361 | 31 | return $job; |
|
| 362 | } |
||
| 363 | |||
| 364 | abstract protected function updateNearestBatch(Job $job); |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $objectName |
||
| 368 | */ |
||
| 369 | abstract protected function stopIdGenerator($objectName); |
||
| 370 | |||
| 371 | abstract protected function restoreIdGenerator($objectName); |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param array $criterion |
||
| 375 | * @param int $limit |
||
| 376 | * @param int $offset |
||
| 377 | */ |
||
| 378 | 2 | private function resetJobsByCriterion( |
|
| 406 | |||
| 407 | 17 | protected function resetSaveOk($function) |
|
| 408 | { |
||
| 409 | 17 | } |
|
| 410 | |||
| 411 | /** |
||
| 412 | * @param RetryableJob $jobArchive |
||
| 413 | * @param $className |
||
| 414 | * @param $countProcessed |
||
| 415 | */ |
||
| 416 | 2 | protected function resetJob(RetryableJob $jobArchive, $className, &$countProcessed) |
|
| 442 | } |
||
| 443 |