Complex classes like JobManager 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 JobManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class JobManager extends DoctrineJobManager |
||
| 17 | { |
||
| 18 | use CommonTrait; |
||
| 19 | protected static $saveInsertCalled = null; |
||
| 20 | protected static $resetInsertCalled = null; |
||
| 21 | |||
| 22 | 3 | public function countJobsByStatus($objectName, $status, $workerName = null, $method = null) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @param string|null $workerName |
||
| 55 | * @param string|null $method |
||
| 56 | * |
||
| 57 | * @return int Count of jobs pruned |
||
| 58 | */ |
||
| 59 | 1 | public function pruneExceptionJobs($workerName = null, $method = null) |
|
| 72 | |||
| 73 | 21 | protected function resetSaveOk($function) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $workerName |
||
| 99 | * @param string $method |
||
| 100 | */ |
||
| 101 | 13 | protected function addWorkerNameCriterion(QueryBuilder $queryBuilder, $workerName = null, $method = null) |
|
| 111 | |||
| 112 | 1 | protected function updateExpired($workerName = null, $method = null) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Removes archived jobs older than $olderThan. |
||
| 132 | * |
||
| 133 | * @param \DateTime $olderThan |
||
| 134 | */ |
||
| 135 | 1 | public function pruneArchivedJobs(\DateTime $olderThan) |
|
| 143 | |||
| 144 | 2 | public function getJobCount($workerName = null, $method = null) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Get Jobs statuses. |
||
| 194 | */ |
||
| 195 | 3 | public function getStatus() |
|
| 196 | { |
||
| 197 | 3 | $result = []; |
|
| 198 | 3 | $this->getStatusByEntityName($this->getJobClass(), $result); |
|
| 199 | 3 | $this->getStatusByEntityName($this->getJobArchiveClass(), $result); |
|
| 200 | |||
| 201 | 3 | $finalResult = []; |
|
| 202 | 3 | foreach ($result as $key => $item) { |
|
| 203 | 1 | ksort($item); |
|
| 204 | 1 | foreach ($item as $status => $count) { |
|
| 205 | 1 | if (isset($finalResult[$key][$status])) { |
|
| 206 | $finalResult[$key][$status] += $count; |
||
| 207 | } else { |
||
| 208 | 1 | $finalResult[$key][$status] = $count; |
|
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | 3 | return $finalResult; |
|
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param string $entityName |
||
| 218 | */ |
||
| 219 | 3 | protected function getStatusByEntityName($entityName, array &$result) |
|
| 220 | { |
||
| 221 | /** @var EntityManager $objectManager */ |
||
| 222 | 3 | $objectManager = $this->getObjectManager(); |
|
| 223 | 3 | $result1 = $objectManager->getRepository($entityName)->createQueryBuilder('j')->select('j.workerName, j.method, j.status, count(j) as c') |
|
| 224 | 3 | ->groupBy('j.workerName, j.method, j.status')->getQuery()->getArrayResult(); |
|
| 225 | |||
| 226 | 3 | foreach ($result1 as $item) { |
|
| 227 | 1 | $method = $item['workerName'].'->'.$item['method'].'()'; |
|
| 228 | 1 | if (!isset($result[$method])) { |
|
| 229 | 1 | $result[$method] = [BaseJob::STATUS_NEW => 0, |
|
| 230 | BaseJob::STATUS_RUNNING => 0, |
||
| 231 | BaseJob::STATUS_SUCCESS => 0, |
||
| 232 | BaseJob::STATUS_FAILURE => 0, |
||
| 233 | BaseJob::STATUS_EXCEPTION => 0, |
||
| 234 | StallableJob::STATUS_STALLED => 0, |
||
| 235 | \Dtc\QueueBundle\Model\Job::STATUS_EXPIRED => 0, |
||
| 236 | RetryableJob::STATUS_MAX_FAILURES => 0, |
||
| 237 | RetryableJob::STATUS_MAX_EXCEPTIONS => 0, |
||
| 238 | StallableJob::STATUS_MAX_STALLS => 0, |
||
| 239 | RetryableJob::STATUS_MAX_RETRIES => 0, ]; |
||
| 240 | } |
||
| 241 | 1 | $result[$method][$item['status']] += intval($item['c']); |
|
| 242 | } |
||
| 243 | 3 | } |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Get the next job to run (can be filtered by workername and method name). |
||
| 247 | * |
||
| 248 | * @param string $workerName |
||
| 249 | * @param string $methodName |
||
| 250 | * @param bool $prioritize |
||
| 251 | * @param int $runId |
||
| 252 | * |
||
| 253 | * @return Job|null |
||
| 254 | */ |
||
| 255 | 11 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
| 256 | { |
||
| 257 | do { |
||
| 258 | 11 | $queryBuilder = $this->getJobQueryBuilder($workerName, $methodName, $prioritize); |
|
| 259 | 11 | $queryBuilder->select('j.id'); |
|
| 260 | 11 | $queryBuilder->setMaxResults(100); |
|
| 261 | |||
| 262 | /** @var QueryBuilder $queryBuilder */ |
||
| 263 | 11 | $query = $queryBuilder->getQuery(); |
|
| 264 | 11 | $jobs = $query->getResult(); |
|
| 265 | 11 | if ($jobs) { |
|
|
|
|||
| 266 | 9 | foreach ($jobs as $job) { |
|
| 267 | 9 | if ($job = $this->takeJob($job['id'], $runId)) { |
|
| 268 | 9 | return $job; |
|
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | 6 | } while ($jobs); |
|
| 273 | |||
| 274 | 6 | return null; |
|
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param string|null $workerName |
||
| 279 | * @param string|null $methodName |
||
| 280 | * @param bool $prioritize |
||
| 281 | * |
||
| 282 | * @return QueryBuilder |
||
| 283 | */ |
||
| 284 | 11 | public function getJobQueryBuilder($workerName = null, $methodName = null, $prioritize = true) |
|
| 285 | { |
||
| 286 | /** @var EntityRepository $repository */ |
||
| 287 | 11 | $repository = $this->getRepository(); |
|
| 288 | 11 | $queryBuilder = $repository->createQueryBuilder('j'); |
|
| 289 | 11 | $this->addStandardPredicate($queryBuilder); |
|
| 290 | 11 | $this->addWorkerNameCriterion($queryBuilder, $workerName, $methodName); |
|
| 291 | |||
| 292 | 11 | if ($prioritize) { |
|
| 293 | 11 | $queryBuilder->addOrderBy('j.priority', 'DESC'); |
|
| 294 | 11 | $queryBuilder->addOrderBy('j.whenUs', 'ASC'); |
|
| 295 | } else { |
||
| 296 | 1 | $queryBuilder->orderBy('j.whenUs', 'ASC'); |
|
| 297 | } |
||
| 298 | |||
| 299 | 11 | return $queryBuilder; |
|
| 300 | } |
||
| 301 | |||
| 302 | 12 | protected function addStandardPredicate(QueryBuilder $queryBuilder) |
|
| 303 | { |
||
| 304 | 12 | $dateTime = new \DateTime(); |
|
| 305 | $queryBuilder |
||
| 306 | 12 | ->where('j.status = :status')->setParameter(':status', BaseJob::STATUS_NEW) |
|
| 307 | 12 | ->andWhere($queryBuilder->expr()->orX( |
|
| 308 | 12 | $queryBuilder->expr()->isNull('j.whenUs'), |
|
| 309 | 12 | $queryBuilder->expr()->lte('j.whenUs', ':whenUs') |
|
| 310 | )) |
||
| 311 | 12 | ->andWhere($queryBuilder->expr()->orX( |
|
| 312 | 12 | $queryBuilder->expr()->isNull('j.expiresAt'), |
|
| 313 | 12 | $queryBuilder->expr()->gt('j.expiresAt', ':expiresAt') |
|
| 314 | )) |
||
| 315 | 12 | ->setParameter(':whenUs', Util::getMicrotimeDecimalFormat($dateTime)) |
|
| 316 | 12 | ->setParameter(':expiresAt', $dateTime); |
|
| 317 | 12 | } |
|
| 318 | |||
| 319 | 9 | protected function takeJob($jobId, $runId = null) |
|
| 320 | { |
||
| 321 | /** @var EntityRepository $repository */ |
||
| 322 | 9 | $repository = $this->getRepository(); |
|
| 323 | /** @var QueryBuilder $queryBuilder */ |
||
| 324 | 9 | $queryBuilder = $repository->createQueryBuilder('j'); |
|
| 325 | $queryBuilder |
||
| 326 | 9 | ->update() |
|
| 327 | 9 | ->set('j.status', ':status') |
|
| 328 | 9 | ->setParameter(':status', BaseJob::STATUS_RUNNING); |
|
| 329 | 9 | if (null !== $runId) { |
|
| 330 | $queryBuilder |
||
| 331 | 1 | ->set('j.runId', ':runId') |
|
| 332 | 1 | ->setParameter(':runId', $runId); |
|
| 333 | } |
||
| 334 | 9 | $queryBuilder->set('j.startedAt', ':startedAt') |
|
| 335 | 9 | ->setParameter(':startedAt', new \DateTime()); |
|
| 336 | 9 | $queryBuilder->where('j.id = :id'); |
|
| 337 | 9 | $queryBuilder->setParameter(':id', $jobId); |
|
| 338 | 9 | $resultCount = $queryBuilder->getQuery()->execute(); |
|
| 339 | |||
| 340 | 9 | if (1 === $resultCount) { |
|
| 341 | 9 | return $repository->find($jobId); |
|
| 342 | } |
||
| 343 | |||
| 344 | return null; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Tries to update the nearest job as a batch. |
||
| 349 | * |
||
| 350 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 351 | * |
||
| 352 | * @return null|Job |
||
| 353 | */ |
||
| 354 | 1 | public function updateNearestBatch(\Dtc\QueueBundle\Model\Job $job) |
|
| 355 | { |
||
| 356 | /** @var QueryBuilder $queryBuilder */ |
||
| 357 | 1 | $queryBuilder = $this->getRepository()->createQueryBuilder('j'); |
|
| 358 | 1 | $queryBuilder->select() |
|
| 359 | 1 | ->where('j.crcHash = :crcHash') |
|
| 360 | 1 | ->andWhere('j.status = :status') |
|
| 361 | 1 | ->setParameter(':status', BaseJob::STATUS_NEW) |
|
| 362 | 1 | ->setParameter(':crcHash', $job->getCrcHash()) |
|
| 363 | 1 | ->orderBy('j.whenUs', 'ASC') |
|
| 364 | 1 | ->setMaxResults(1); |
|
| 365 | 1 | $existingJobs = $queryBuilder->getQuery()->execute(); |
|
| 366 | |||
| 367 | 1 | if (empty($existingJobs)) { |
|
| 368 | return null; |
||
| 369 | } |
||
| 370 | /** @var Job $existingJob */ |
||
| 371 | 1 | $existingJob = $existingJobs[0]; |
|
| 372 | |||
| 373 | 1 | $newPriority = max($job->getPriority(), $existingJob->getPriority()); |
|
| 374 | 1 | $newWhenUs = $existingJob->getWhenUs(); |
|
| 375 | 1 | $bcResult = bccomp($job->getWhenUs(), $existingJob->getWhenUs()); |
|
| 376 | 1 | if ($bcResult < 0) { |
|
| 377 | 1 | $newWhenUs = $job->getWhenUs(); |
|
| 378 | } |
||
| 379 | |||
| 380 | 1 | $this->updateBatchJob($existingJob, $newPriority, $newWhenUs); |
|
| 381 | |||
| 382 | 1 | return $existingJob; |
|
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param int $newPriority |
||
| 387 | * @param string $newWhenUs |
||
| 388 | */ |
||
| 389 | 1 | protected function updateBatchJob(Job $existingJob, $newPriority, $newWhenUs) |
|
| 417 | |||
| 418 | 1 | public function getWorkersAndMethods() |
|
| 419 | { |
||
| 420 | /** @var EntityRepository $repository */ |
||
| 421 | 1 | $repository = $this->getRepository(); |
|
| 422 | 1 | $queryBuilder = $repository->createQueryBuilder('j'); |
|
| 423 | 1 | $this->addStandardPredicate($queryBuilder); |
|
| 424 | $queryBuilder |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param string $workerName |
||
| 441 | * @param string $methodName |
||
| 442 | */ |
||
| 443 | 2 | public function countLiveJobs($workerName = null, $methodName = null) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * @param string $workerName |
||
| 457 | * @param string $methodName |
||
| 458 | * @param \Closure $progressCallback |
||
| 459 | */ |
||
| 460 | 1 | public function archiveAllJobs($workerName = null, $methodName = null, $progressCallback) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Move jobs in 'archive' status to the archive table. |
||
| 480 | * |
||
| 481 | * This is a bit of a hack to run a lower level query so as to process the INSERT INTO SELECT |
||
| 482 | * All on the server as "INSERT INTO SELECT" is not supported natively in Doctrine. |
||
| 483 | * |
||
| 484 | * @param string|null $workerName |
||
| 485 | * @param string|null $methodName |
||
| 486 | * @param \Closure $progressCallback |
||
| 487 | */ |
||
| 488 | 1 | protected function runArchive($workerName = null, $methodName = null, $progressCallback) |
|
| 517 | } |
||
| 518 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.