Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 15 | class JobManager extends BaseJobManager |
||
| 16 | { |
||
| 17 | 14 | protected function transferQueues() |
|
| 18 | { |
||
| 19 | // Drains from WhenAt queue into Prioirty Queue |
||
| 20 | 14 | $whenQueue = $this->getWhenQueueCacheKey(); |
|
| 21 | 14 | $priorityQueue = $this->getPriorityQueueCacheKey(); |
|
| 22 | 14 | $microtime = Util::getMicrotimeDecimal(); |
|
| 23 | 14 | while ($jobId = $this->redis->zPopByMaxScore($whenQueue, $microtime)) { |
|
| 24 | 14 | $jobMessage = $this->redis->get($this->getJobCacheKey($jobId)); |
|
| 25 | 14 | if (is_string($jobMessage)) { |
|
| 26 | 14 | $job = new Job(); |
|
| 27 | 14 | $job->fromMessage($jobMessage); |
|
| 28 | 14 | $this->redis->zAdd($priorityQueue, $job->getPriority(), $job->getId()); |
|
| 29 | 14 | } |
|
| 30 | 14 | } |
|
| 31 | 14 | } |
|
| 32 | |||
| 33 | /** |
||
| 34 | * @param Job $job |
||
| 35 | * |
||
| 36 | * @return Job|null |
||
| 37 | */ |
||
| 38 | 2 | protected function batchSave(Job $job) |
|
| 39 | { |
||
| 40 | 2 | $crcHash = $job->getCrcHash(); |
|
| 41 | 2 | $crcCacheKey = $this->getJobCrcHashKey($crcHash); |
|
| 42 | 2 | $result = $this->redis->lrange($crcCacheKey, 0, 1000); |
|
| 43 | 2 | if (is_array($result)) { |
|
| 44 | 2 | foreach ($result as $jobId) { |
|
| 45 | 2 | $jobCacheKey1 = $this->getJobCacheKey($jobId); |
|
| 46 | 2 | if (!($foundJobMessage = $this->redis->get($jobCacheKey1))) { |
|
| 47 | $this->redis->lRem($crcCacheKey, 1, $jobCacheKey1); |
||
| 48 | continue; |
||
| 49 | } |
||
| 50 | |||
| 51 | /// There is one? |
||
| 52 | 2 | if ($foundJobMessage) { |
|
| 53 | 2 | $foundJob = $this->batchFoundJob($job, $jobCacheKey1, $foundJobMessage); |
|
| 54 | 2 | if ($foundJob) { |
|
| 55 | 2 | return $foundJob; |
|
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | return null; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $foundJobCacheKey |
||
| 66 | * @param bool $foundJobMessage |
||
| 67 | */ |
||
| 68 | 2 | protected function batchFoundJob(Job $job, $foundJobCacheKey, $foundJobMessage) |
|
| 69 | { |
||
| 70 | 2 | $when = $job->getWhenUs(); |
|
| 71 | 2 | $crcHash = $job->getCrcHash(); |
|
| 72 | 2 | $crcCacheKey = $this->getJobCrcHashKey($crcHash); |
|
| 73 | |||
| 74 | 2 | $foundJob = new Job(); |
|
| 75 | 2 | $foundJob->fromMessage($foundJobMessage); |
|
|
|
|||
| 76 | 2 | $foundWhen = $foundJob->getWhenUs(); |
|
| 77 | |||
| 78 | // Fix this using bcmath |
||
| 79 | 2 | $curtimeU = Util::getMicrotimeDecimal(); |
|
| 80 | 2 | $newFoundWhen = null; |
|
| 81 | 2 | if (bccomp($foundWhen, $curtimeU) > 0 && bccomp($foundWhen, $when) >= 1) { |
|
| 82 | 2 | $newFoundWhen = $when; |
|
| 83 | 2 | } |
|
| 84 | 2 | $foundPriority = $foundJob->getPriority(); |
|
| 85 | 2 | $newFoundPriority = null; |
|
| 86 | 2 | if ($foundPriority > $job->getPriority()) { |
|
| 87 | 2 | $newFoundPriority = $job->getPriority(); |
|
| 88 | 2 | } |
|
| 89 | |||
| 90 | 2 | return $this->finishBatchFoundJob($foundJob, $foundJobCacheKey, $crcCacheKey, $newFoundWhen, $newFoundPriority); |
|
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param string $crcCacheKey |
||
| 95 | * @param int|null $newFoundPriority |
||
| 96 | */ |
||
| 97 | 2 | protected function finishBatchFoundJob(Job $foundJob, $foundJobCacheKey, $crcCacheKey, $newFoundWhen, $newFoundPriority) |
|
| 98 | { |
||
| 99 | // Now how do we adjust this job's priority or time? |
||
| 100 | 2 | $adjust = false; |
|
| 101 | 2 | if (isset($newFoundWhen)) { |
|
| 102 | 2 | $foundJob->setWhenUs($newFoundWhen); |
|
| 103 | 2 | $adjust = true; |
|
| 104 | 2 | } |
|
| 105 | 2 | if (isset($newFoundPriority)) { |
|
| 106 | 2 | $foundJob->setPriority($newFoundPriority); |
|
| 107 | 2 | $adjust = true; |
|
| 108 | 2 | } |
|
| 109 | 2 | if (!$adjust) { |
|
| 110 | 2 | return $foundJob; |
|
| 111 | } |
||
| 112 | |||
| 113 | 2 | return $this->addFoundJob($adjust, $foundJob, $foundJobCacheKey, $crcCacheKey); |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param bool $adjust |
||
| 118 | */ |
||
| 119 | 2 | protected function addFoundJob($adjust, Job $foundJob, $foundJobCacheKey, $crcCacheKey) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @param string $queue |
||
| 138 | * @param bool $adjust |
||
| 139 | */ |
||
| 140 | 2 | private function adjustJob($adjust, $queue, Job $foundJob, $foundJobCacheKey, $crcCacheKey, $zScore) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 159 | * |
||
| 160 | * @return Job|null |
||
| 161 | * |
||
| 162 | * @throws ClassNotSubclassException |
||
| 163 | * @throws PriorityException |
||
| 164 | */ |
||
| 165 | 20 | public function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
|
| 166 | { |
||
| 167 | 20 | if (!$job instanceof Job) { |
|
| 168 | throw new \InvalidArgumentException('$job must be instance of '.Job::class); |
||
| 169 | } |
||
| 170 | |||
| 171 | 20 | $this->validateSaveable($job); |
|
| 172 | 20 | $this->setJobId($job); |
|
| 173 | |||
| 174 | // Add to whenAt or priority queue? /// optimizaiton... |
||
| 175 | 20 | $whenUs = $job->getWhenUs(); |
|
| 176 | 20 | if (!$whenUs) { |
|
| 177 | $whenUs = Util::getMicrotimeDecimal(); |
||
| 178 | $job->setWhenUs($whenUs); |
||
| 179 | } |
||
| 180 | |||
| 181 | 20 | if (true === $job->getBatch()) { |
|
| 182 | // is there a CRC Hash already for this job |
||
| 183 | 2 | if ($oldJob = $this->batchSave($job)) { |
|
| 184 | 2 | return $oldJob; |
|
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | 20 | return $this->saveJob($job); |
|
| 189 | } |
||
| 190 | |||
| 191 | 20 | protected function saveJob(Job $job) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @param Job $job |
||
| 212 | * |
||
| 213 | * @return bool false if the job is already expired, true otherwise |
||
| 214 | */ |
||
| 215 | 20 | protected function insertJob(Job $job) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Returns the prioirty in DESCENDING order, except if maxPrioirty is null, then prioirty is 0. |
||
| 235 | * |
||
| 236 | * @param int|null $priority |
||
| 237 | * |
||
| 238 | * @return int |
||
| 239 | */ |
||
| 240 | 20 | protected function calculatePriority($priority) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 257 | * |
||
| 258 | * @throws PriorityException |
||
| 259 | * @throws ClassNotSubclassException |
||
| 260 | */ |
||
| 261 | 20 | View Code Duplication | protected function validateSaveable(\Dtc\QueueBundle\Model\Job $job) |
| 271 | |||
| 272 | 2 | public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param string|null $workerName |
||
| 286 | * @param string|null $methodName |
||
| 287 | * @param bool $prioritize |
||
| 288 | * @param mixed $runId |
||
| 289 | * |
||
| 290 | * @throws UnsupportedException |
||
| 291 | * |
||
| 292 | * @return Job|null |
||
| 293 | */ |
||
| 294 | 16 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
| 295 | { |
||
| 296 | // First thing migrate any jobs from When queue to Prioirty queue |
||
| 297 | |||
| 298 | 16 | $this->verifyGetJobArgs($workerName, $methodName, $prioritize); |
|
| 299 | 14 | if (null !== $this->maxPriority) { |
|
| 300 | 14 | $this->transferQueues(); |
|
| 301 | 14 | $queue = $this->getPriorityQueueCacheKey(); |
|
| 302 | 14 | $jobId = $this->redis->zPop($queue); |
|
| 303 | 14 | } else { |
|
| 304 | $queue = $this->getWhenQueueCacheKey(); |
||
| 305 | $microtime = Util::getMicrotimeDecimal(); |
||
| 306 | $jobId = $this->redis->zPopByMaxScore($queue, $microtime); |
||
| 307 | } |
||
| 308 | |||
| 309 | 14 | if ($jobId) { |
|
| 310 | 14 | return $this->retrieveJob($jobId); |
|
| 311 | } |
||
| 312 | |||
| 313 | 10 | return null; |
|
| 314 | } |
||
| 315 | |||
| 316 | 14 | protected function retrieveJob($jobId) |
|
| 317 | { |
||
| 318 | 14 | $job = null; |
|
| 319 | 14 | $jobMessage = $this->redis->get($this->getJobCacheKey($jobId)); |
|
| 320 | 14 | if (is_string($jobMessage)) { |
|
| 321 | 14 | $job = new Job(); |
|
| 322 | 14 | $job->fromMessage($jobMessage); |
|
| 323 | 14 | $crcCacheKey = $this->getJobCrcHashKey($job->getCrcHash()); |
|
| 324 | 14 | $this->redis->lRem($crcCacheKey, 1, $job->getId()); |
|
| 325 | 14 | $this->redis->del([$this->getJobCacheKey($job->getId())]); |
|
| 326 | 14 | } |
|
| 327 | |||
| 328 | 14 | return $job; |
|
| 329 | } |
||
| 330 | |||
| 331 | 2 | public function getWaitingJobCount($workerName = null, $methodName = null) |
|
| 332 | { |
||
| 333 | 2 | $microtime = Util::getMicrotimeDecimal(); |
|
| 334 | 2 | $count = $this->redis->zCount($this->getWhenQueueCacheKey(), 0, $microtime); |
|
| 335 | |||
| 336 | 2 | if (null !== $this->maxPriority) { |
|
| 337 | 2 | $count += $this->redis->zCount($this->getPriorityQueueCacheKey(), '-inf', '+inf'); |
|
| 338 | 2 | } |
|
| 339 | |||
| 340 | 2 | return $count; |
|
| 341 | } |
||
| 342 | |||
| 343 | 4 | View Code Duplication | public function resetJob(RetryableJob $job) |
| 357 | |||
| 358 | 2 | private function collateStatusResults(array &$results, $cacheKey) |
|
| 359 | { |
||
| 360 | 2 | $cursor = null; |
|
| 361 | 2 | while ($jobs = $this->redis->zScan($cacheKey, $cursor, '', 100)) { |
|
| 362 | 2 | $jobs = $this->redis->mget(array_map(function ($item) { |
|
| 363 | 2 | return $this->getJobCacheKey($item); |
|
| 364 | 2 | }, array_keys($jobs))); |
|
| 365 | 2 | $this->extractStatusResults($jobs, $results); |
|
| 366 | 2 | if (0 === $cursor) { |
|
| 367 | 2 | break; |
|
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | 2 | return $results; |
|
| 372 | } |
||
| 373 | |||
| 374 | 2 | private function extractStatusResults(array $jobs, array &$results) |
|
| 375 | { |
||
| 376 | 2 | foreach ($jobs as $jobMessage) { |
|
| 377 | 2 | if (is_string($jobMessage)) { |
|
| 378 | 2 | $job = new Job(); |
|
| 379 | 2 | $job->fromMessage($jobMessage); |
|
| 380 | 2 | $resultHashKey = $job->getWorkerName().'->'.$job->getMethod().'()'; |
|
| 381 | 2 | if (!isset($results[$resultHashKey][BaseJob::STATUS_NEW])) { |
|
| 382 | 2 | $results[$resultHashKey] = static::getAllStatuses(); |
|
| 383 | 2 | } |
|
| 384 | 2 | if (!isset($results[$resultHashKey][BaseJob::STATUS_NEW])) { |
|
| 385 | $results[$resultHashKey][BaseJob::STATUS_NEW] = 0; |
||
| 386 | } |
||
| 387 | 2 | ++$results[$resultHashKey][BaseJob::STATUS_NEW]; |
|
| 388 | 2 | } |
|
| 389 | 2 | } |
|
| 390 | 2 | } |
|
| 391 | |||
| 392 | 2 | private function extractStatusHashResults(array $hResults, array &$results) |
|
| 393 | { |
||
| 394 | 2 | foreach ($hResults as $key => $value) { |
|
| 395 | 2 | list($workerName, $method, $status) = explode(',', $key); |
|
| 396 | 2 | $resultHashKey = $workerName.'->'.$method.'()'; |
|
| 397 | 2 | if (!isset($results[$resultHashKey])) { |
|
| 398 | $results[$resultHashKey] = static::getAllStatuses(); |
||
| 399 | } |
||
| 400 | 2 | if (!isset($results[$resultHashKey][$status])) { |
|
| 401 | $results[$resultHashKey][$status] = 0; |
||
| 402 | } |
||
| 403 | 2 | $results[$resultHashKey][$status] += $value; |
|
| 404 | 2 | } |
|
| 405 | 2 | } |
|
| 406 | |||
| 407 | 2 | public function getStatus() |
|
| 408 | { |
||
| 409 | 2 | $whenQueueCacheKey = $this->getWhenQueueCacheKey(); |
|
| 410 | 2 | $priorityQueueCacheKey = $this->getPriorityQueueCacheKey(); |
|
| 411 | 2 | $results = []; |
|
| 412 | 2 | $this->collateStatusResults($results, $whenQueueCacheKey); |
|
| 413 | 2 | if (null !== $this->maxPriority) { |
|
| 414 | 2 | $this->collateStatusResults($results, $priorityQueueCacheKey); |
|
| 415 | 2 | } |
|
| 416 | |||
| 417 | 2 | $cacheKey = $this->getStatusCacheKey(); |
|
| 418 | 2 | $cursor = null; |
|
| 419 | 2 | while ($hResults = $this->redis->hScan($cacheKey, $cursor, '', 100)) { |
|
| 420 | 2 | $this->extractStatusHashResults($hResults, $results); |
|
| 421 | 2 | if (0 === $cursor) { |
|
| 422 | 2 | break; |
|
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | 2 | return $results; |
|
| 427 | } |
||
| 428 | |||
| 429 | 6 | public function retryableSaveHistory(RetryableJob $job, $retry) |
|
| 439 | } |
||
| 440 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: