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 | 3 | public function __construct(ObjectManager $objectManager, |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @return ObjectManager |
||
| 50 | */ |
||
| 51 | 3 | public function getObjectManager() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | 3 | public function getObjectName() |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | 3 | public function getArchiveObjectName() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public function getRunClass() |
||
| 76 | { |
||
| 77 | return $this->runClass; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | public function getRunArchiveClass() |
||
| 84 | { |
||
| 85 | return $this->runArchiveClass; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return ObjectRepository |
||
| 90 | */ |
||
| 91 | 3 | public function getRepository() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $objectName |
||
| 98 | */ |
||
| 99 | abstract protected function countJobsByStatus($objectName, $status, $workerName = null, $method = null); |
||
| 100 | |||
| 101 | public function resetErroneousJobs($workerName = null, $method = null) |
||
| 102 | { |
||
| 103 | $count = $this->countJobsByStatus($this->getArchiveObjectName(), Job::STATUS_ERROR, $workerName, $method); |
||
| 104 | |||
| 105 | $criterion = ['status' => Job::STATUS_ERROR]; |
||
| 106 | $this->addWorkerNameMethod($criterion, $workerName, $method); |
||
| 107 | |||
| 108 | $countProcessed = 0; |
||
| 109 | for ($i = 0; $i < $count; $i += static::FETCH_COUNT) { |
||
| 110 | $countProcessed += $this->resetJobsByCriterion( |
||
| 111 | $criterion, static::FETCH_COUNT, $i); |
||
| 112 | } |
||
| 113 | |||
| 114 | 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 | 1 | protected function addWorkerNameMethod(array &$criterion, $workerName = null, $method = null) |
|
| 128 | { |
||
| 129 | 1 | if (null !== $workerName) { |
|
| 130 | $criterion['workerName'] = $workerName; |
||
| 131 | } |
||
| 132 | 1 | if (null !== $method) { |
|
| 133 | $criterion['method'] = $method; |
||
| 134 | } |
||
| 135 | 1 | } |
|
| 136 | |||
| 137 | 1 | public function pruneExpiredJobs($workerName = null, $method = null) |
|
| 160 | |||
| 161 | 1 | protected function flush() |
|
| 162 | { |
||
| 163 | 1 | $this->getObjectManager()->flush(); |
|
| 165 | |||
| 166 | protected function getStalledJobs($workerName = null, $method = null) |
||
| 167 | { |
||
| 168 | $count = $this->countJobsByStatus($this->getObjectName(), Job::STATUS_RUNNING, $workerName, $method); |
||
| 169 | |||
| 170 | $criterion = ['status' => BaseJob::STATUS_RUNNING]; |
||
| 171 | $this->addWorkerNameMethod($criterion, $workerName, $method); |
||
| 172 | |||
| 173 | $runningJobs = $this->findRunningJobs($criterion, $count); |
||
| 174 | |||
| 175 | return $this->extractStalledJobs($runningJobs); |
||
| 176 | } |
||
| 177 | |||
| 178 | protected function findRunningJobs($criterion, $count) |
||
| 179 | { |
||
| 180 | $repository = $this->getRepository(); |
||
| 181 | $runningJobsById = []; |
||
| 182 | |||
| 183 | for ($i = 0; $i < $count; $i += static::FETCH_COUNT) { |
||
| 184 | $runningJobs = $repository->findBy($criterion, null, static::FETCH_COUNT, $i); |
||
| 185 | if (!empty($runningJobs)) { |
||
| 186 | foreach ($runningJobs as $job) { |
||
| 187 | /** @var RetryableJob $job */ |
||
| 188 | if (null !== $runId = $job->getRunId()) { |
||
| 189 | $runningJobsById[$runId][] = $job; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | return $runningJobsById; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param $runId |
||
| 200 | * @param array $jobs |
||
| 201 | * @param array $stalledJobs |
||
| 202 | */ |
||
| 203 | protected function extractStalledLiveRuns($runId, array $jobs, array &$stalledJobs) |
||
| 204 | { |
||
| 205 | $objectManager = $this->getObjectManager(); |
||
| 206 | $runRepository = $objectManager->getRepository($this->runClass); |
||
| 207 | if ($run = $runRepository->find($runId)) { |
||
| 208 | foreach ($jobs as $job) { |
||
| 209 | if ($run->getCurrentJobId() == $job->getId()) { |
||
| 210 | continue; |
||
| 211 | } |
||
| 212 | $stalledJobs[] = $job; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param array $runningJobsById |
||
| 219 | * |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | protected function extractStalledJobs(array $runningJobsById) |
||
| 223 | { |
||
| 224 | $objectManager = $this->getObjectManager(); |
||
| 225 | /** @var EntityRepository|DocumentRepository $runArchiveRepository */ |
||
| 226 | $runArchiveRepository = $objectManager->getRepository($this->runArchiveClass); |
||
| 227 | |||
| 228 | $stalledJobs = []; |
||
| 229 | foreach (array_keys($runningJobsById) as $runId) { |
||
| 230 | $this->extractStalledLiveRuns($runId, $runningJobsById[$runId], $stalledJobs); |
||
| 231 | /** @var Run $run */ |
||
| 232 | if ($run = $runArchiveRepository->find($runId)) { |
||
| 233 | if ($endTime = $run->getEndedAt()) { |
||
| 234 | // Did it end over an hour ago |
||
| 235 | if ((time() - $endTime->getTimestamp()) > static::STALLED_SECONDS) { |
||
| 236 | $stalledJobs = array_merge($stalledJobs, $runningJobsById[$runId]); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | return $stalledJobs; |
||
| 243 | } |
||
| 244 | |||
| 245 | protected function updateMaxStatus(RetryableJob $job, $status, $max = null, $count = 0) |
||
| 246 | { |
||
| 247 | if (null !== $max && $count >= $max) { |
||
| 248 | $job->setStatus($status); |
||
| 249 | |||
| 250 | return true; |
||
| 251 | } |
||
| 252 | |||
| 253 | return false; |
||
| 254 | } |
||
| 255 | |||
| 256 | abstract protected function getJobCurrentStatus(Job $job); |
||
| 257 | |||
| 258 | protected function runStalledLoop($i, $count, array $stalledJobs, &$countProcessed) |
||
| 259 | { |
||
| 260 | $objectManager = $this->getObjectManager(); |
||
| 261 | for ($j = $i, $max = $i + static::FETCH_COUNT; $j < $max && $j < $count; ++$j) { |
||
| 262 | /* RetryableJob $job */ |
||
| 263 | $job = $stalledJobs[$j]; |
||
| 264 | $status = $this->getJobCurrentStatus($job); |
||
| 265 | |||
| 266 | // Query the data store to make sure the job is still marked running |
||
| 267 | if (BaseJob::STATUS_RUNNING !== $status) { |
||
| 268 | continue; |
||
| 269 | } |
||
| 270 | |||
| 271 | $job->setStalledCount($job->getStalledCount() + 1); |
||
| 272 | if ($this->updateMaxStatus($job, RetryableJob::STATUS_MAX_STALLED, $job->getMaxStalled(), $job->getStalledCount()) || |
||
| 273 | $this->updateMaxStatus($job, RetryableJob::STATUS_MAX_RETRIES, $job->getMaxRetries(), $job->getRetries())) { |
||
| 274 | $objectManager->remove($job); |
||
| 275 | continue; |
||
| 276 | } |
||
| 277 | |||
| 278 | $job->setRetries($job->getRetries() + 1); |
||
| 279 | $job->setStatus(BaseJob::STATUS_NEW); |
||
| 280 | $job->setLocked(null); |
||
| 281 | $job->setLockedAt(null); |
||
| 282 | $objectManager->persist($job); |
||
| 283 | ++$countProcessed; |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | public function resetStalledJobs($workerName = null, $method = null) |
||
| 288 | { |
||
| 289 | $stalledJobs = $this->getStalledJobs($workerName, $method); |
||
| 290 | |||
| 291 | $countProcessed = 0; |
||
| 292 | for ($i = 0, $count = count($stalledJobs); $i < $count; $i += static::FETCH_COUNT) { |
||
| 293 | $this->runStalledLoop($i, $count, $stalledJobs, $countProcessed); |
||
| 294 | $this->flush(); |
||
| 295 | } |
||
| 296 | |||
| 297 | return $countProcessed; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $workerName |
||
| 302 | * @param string $method |
||
| 303 | */ |
||
| 304 | public function pruneStalledJobs($workerName = null, $method = null) |
||
| 305 | { |
||
| 306 | $stalledJobs = $this->getStalledJobs($workerName, $method); |
||
| 307 | $objectManager = $this->getObjectManager(); |
||
| 308 | |||
| 309 | $countProcessed = 0; |
||
| 310 | for ($i = 0, $count = count($stalledJobs); $i < $count; $i += static::FETCH_COUNT) { |
||
| 311 | for ($j = $i, $max = $i + static::FETCH_COUNT; $j < $max && $j < $count; ++$j) { |
||
| 312 | /** @var RetryableJob $job */ |
||
| 313 | $job = $stalledJobs[$j]; |
||
| 314 | $job->setStalledCount($job->getStalledCount() + 1); |
||
| 315 | $job->setStatus(BaseJob::STATUS_ERROR); |
||
| 316 | $job->setMessage('stalled'); |
||
| 317 | $this->updateMaxStatus($job, RetryableJob::STATUS_MAX_STALLED, $job->getMaxStalled(), $job->getStalledCount()); |
||
| 318 | $objectManager->remove($job); |
||
| 319 | ++$countProcessed; |
||
| 320 | } |
||
| 321 | $this->flush(); |
||
| 322 | } |
||
| 323 | |||
| 324 | return $countProcessed; |
||
| 325 | } |
||
| 326 | |||
| 327 | public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
||
| 328 | { |
||
| 329 | $objectManager = $this->getObjectManager(); |
||
| 330 | $objectManager->remove($job); |
||
| 331 | $objectManager->flush(); |
||
| 332 | } |
||
| 333 | |||
| 334 | public function saveHistory(\Dtc\QueueBundle\Model\Job $job) |
||
| 335 | { |
||
| 336 | $this->deleteJob($job); // Should cause job to be archived |
||
| 337 | } |
||
| 338 | |||
| 339 | 1 | protected function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
|
| 340 | { |
||
| 341 | // Generate crc hash for the job |
||
| 342 | 1 | $hashValues = array($job->getClassName(), $job->getMethod(), $job->getWorkerName(), $job->getArgs()); |
|
| 343 | 1 | $crcHash = hash('sha256', serialize($hashValues)); |
|
| 344 | 1 | $job->setCrcHash($crcHash); |
|
| 345 | 1 | $objectManager = $this->getObjectManager(); |
|
| 346 | |||
| 347 | 1 | if (true === $job->getBatch()) { |
|
| 348 | $oldJob = $this->updateNearestBatch($job); |
||
| 349 | if ($oldJob) { |
||
| 350 | return $oldJob; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | // Just save a new job |
||
| 355 | 1 | $this->resetSaveOk(__FUNCTION__); |
|
| 356 | 1 | $objectManager->persist($job); |
|
| 357 | 1 | $objectManager->flush(); |
|
| 358 | |||
| 359 | 1 | return $job; |
|
| 360 | } |
||
| 361 | |||
| 362 | abstract protected function updateNearestBatch(Job $job); |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param string $objectName |
||
| 366 | */ |
||
| 367 | abstract protected function stopIdGenerator($objectName); |
||
| 368 | |||
| 369 | abstract protected function restoreIdGenerator($objectName); |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param array $criterion |
||
| 373 | * @param int $limit |
||
| 374 | * @param int $offset |
||
| 375 | */ |
||
| 376 | private function resetJobsByCriterion( |
||
| 377 | array $criterion, |
||
| 378 | $limit, |
||
| 379 | $offset) |
||
| 380 | { |
||
| 381 | $objectManager = $this->getObjectManager(); |
||
| 382 | $this->resetSaveOk(__FUNCTION__); |
||
| 383 | $objectName = $this->getObjectName(); |
||
| 384 | $archiveObjectName = $this->getArchiveObjectName(); |
||
| 385 | $jobRepository = $objectManager->getRepository($objectName); |
||
| 386 | $jobArchiveRepository = $objectManager->getRepository($archiveObjectName); |
||
| 387 | $className = $jobRepository->getClassName(); |
||
| 388 | $metadata = $objectManager->getClassMetadata($className); |
||
| 389 | $this->stopIdGenerator($objectName); |
||
| 390 | $identifierData = $metadata->getIdentifier(); |
||
| 391 | $idColumn = isset($identifierData[0]) ? $identifierData[0] : 'id'; |
||
| 392 | $results = $jobArchiveRepository->findBy($criterion, [$idColumn => 'ASC'], $limit, $offset); |
||
| 393 | $countProcessed = 0; |
||
| 394 | |||
| 395 | foreach ($results as $jobArchive) { |
||
| 396 | $this->resetJob($jobArchive, $className, $countProcessed); |
||
| 397 | } |
||
| 398 | $objectManager->flush(); |
||
| 399 | |||
| 400 | $this->restoreIdGenerator($objectName); |
||
| 401 | |||
| 402 | return $countProcessed; |
||
| 403 | } |
||
| 404 | |||
| 405 | 1 | protected function resetSaveOk($function) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * @param RetryableJob $jobArchive |
||
| 411 | * @param $className |
||
| 412 | * @param $countProcessed |
||
| 413 | */ |
||
| 414 | protected function resetJob(RetryableJob $jobArchive, $className, &$countProcessed) |
||
| 415 | { |
||
| 416 | $objectManager = $this->getObjectManager(); |
||
| 417 | if ($this->updateMaxStatus($jobArchive, RetryableJob::STATUS_MAX_RETRIES, $jobArchive->getMaxRetries(), $jobArchive->getRetries())) { |
||
| 418 | $objectManager->persist($jobArchive); |
||
| 419 | |||
| 420 | return; |
||
| 440 | } |
||
| 441 |