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 |
||
| 19 | class JobManager extends PriorityJobManager |
||
| 20 | { |
||
| 21 | use JobIdTrait; |
||
| 22 | |||
| 23 | /** @var RedisInterface */ |
||
| 24 | protected $redis; |
||
| 25 | |||
| 26 | /** @var string */ |
||
| 27 | protected $cacheKeyPrefix; |
||
| 28 | |||
| 29 | protected $hostname; |
||
| 30 | protected $pid; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param string $cacheKeyPrefix |
||
| 34 | */ |
||
| 35 | 2 | public function __construct(RunManager $runManager, JobTimingManager $jobTimingManager, $jobClass, $cacheKeyPrefix) |
|
| 43 | |||
| 44 | public function setRedis(RedisInterface $redis) |
||
| 48 | |||
| 49 | 20 | protected function getJobCacheKey($jobId) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $jobCrc |
||
| 56 | */ |
||
| 57 | 20 | protected function getJobCrcHashKey($jobCrc) |
|
| 61 | |||
| 62 | 16 | protected function getPriorityQueueCacheKey() |
|
| 66 | |||
| 67 | 20 | protected function getWhenQueueCacheKey() |
|
| 71 | |||
| 72 | 8 | protected function getStatusCacheKey() |
|
| 76 | |||
| 77 | 14 | protected function transferQueues() |
|
| 78 | { |
||
| 79 | // Drains from WhenAt queue into Prioirty Queue |
||
| 80 | 14 | $whenQueue = $this->getWhenQueueCacheKey(); |
|
| 81 | 14 | $priorityQueue = $this->getPriorityQueueCacheKey(); |
|
| 82 | 14 | $microtime = Util::getMicrotimeDecimal(); |
|
| 83 | 14 | while ($jobId = $this->redis->zPopByMaxScore($whenQueue, $microtime)) { |
|
| 84 | 14 | $jobMessage = $this->redis->get($this->getJobCacheKey($jobId)); |
|
| 85 | 14 | if ($jobMessage) { |
|
| 86 | 14 | $job = new Job(); |
|
| 87 | 14 | $job->fromMessage($jobMessage); |
|
|
|
|||
| 88 | 14 | $this->redis->zAdd($priorityQueue, $job->getPriority(), $job->getId()); |
|
| 89 | } |
||
| 90 | } |
||
| 91 | 14 | } |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param Job $job |
||
| 95 | * |
||
| 96 | * @return Job|null |
||
| 97 | */ |
||
| 98 | 2 | protected function batchSave(Job $job) |
|
| 99 | { |
||
| 100 | 2 | $crcHash = $job->getCrcHash(); |
|
| 101 | 2 | $crcCacheKey = $this->getJobCrcHashKey($crcHash); |
|
| 102 | 2 | $result = $this->redis->lrange($crcCacheKey, 0, 1000); |
|
| 103 | 2 | if (is_array($result)) { |
|
| 104 | 2 | foreach ($result as $jobId) { |
|
| 105 | 2 | $jobCacheKey1 = $this->getJobCacheKey($jobId); |
|
| 106 | 2 | if (!($foundJobMessage = $this->redis->get($jobCacheKey1))) { |
|
| 107 | $this->redis->lRem($crcCacheKey, 1, $jobCacheKey1); |
||
| 108 | continue; |
||
| 109 | } |
||
| 110 | |||
| 111 | /// There is one? |
||
| 112 | 2 | if ($foundJobMessage) { |
|
| 113 | 2 | $foundJob = $this->batchFoundJob($job, $jobCacheKey1, $foundJobMessage); |
|
| 114 | 2 | if ($foundJob) { |
|
| 115 | 2 | return $foundJob; |
|
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | return null; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $foundJobCacheKey |
||
| 126 | * @param bool $foundJobMessage |
||
| 127 | */ |
||
| 128 | 2 | protected function batchFoundJob(Job $job, $foundJobCacheKey, $foundJobMessage) |
|
| 129 | { |
||
| 130 | 2 | $when = $job->getWhenUs(); |
|
| 131 | 2 | $crcHash = $job->getCrcHash(); |
|
| 132 | 2 | $crcCacheKey = $this->getJobCrcHashKey($crcHash); |
|
| 133 | |||
| 134 | 2 | $foundJob = new Job(); |
|
| 135 | 2 | $foundJob->fromMessage($foundJobMessage); |
|
| 136 | 2 | $foundWhen = $foundJob->getWhenUs(); |
|
| 137 | |||
| 138 | // Fix this using bcmath |
||
| 139 | 2 | $curtimeU = Util::getMicrotimeDecimal(); |
|
| 140 | 2 | $newFoundWhen = null; |
|
| 141 | 2 | if (bccomp($foundWhen, $curtimeU) > 0 && bccomp($foundWhen, $when) >= 1) { |
|
| 142 | 2 | $newFoundWhen = $when; |
|
| 143 | } |
||
| 144 | 2 | $foundPriority = $foundJob->getPriority(); |
|
| 145 | 2 | $newFoundPriority = null; |
|
| 146 | 2 | if ($foundPriority > $job->getPriority()) { |
|
| 147 | 2 | $newFoundPriority = $job->getPriority(); |
|
| 148 | } |
||
| 149 | |||
| 150 | 2 | return $this->finishBatchFoundJob($foundJob, $foundJobCacheKey, $crcCacheKey, $newFoundWhen, $newFoundPriority); |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $crcCacheKey |
||
| 155 | * @param int|null $newFoundPriority |
||
| 156 | */ |
||
| 157 | 2 | protected function finishBatchFoundJob(Job $foundJob, $foundJobCacheKey, $crcCacheKey, $newFoundWhen, $newFoundPriority) |
|
| 158 | { |
||
| 159 | // Now how do we adjust this job's priority or time? |
||
| 160 | 2 | $adjust = false; |
|
| 161 | 2 | if (isset($newFoundWhen)) { |
|
| 162 | 2 | $foundJob->setWhenUs($newFoundWhen); |
|
| 163 | 2 | $adjust = true; |
|
| 164 | } |
||
| 165 | 2 | if (isset($newFoundPriority)) { |
|
| 166 | 2 | $foundJob->setPriority($newFoundPriority); |
|
| 167 | 2 | $adjust = true; |
|
| 168 | } |
||
| 169 | 2 | if (!$adjust) { |
|
| 170 | 2 | return $foundJob; |
|
| 171 | } |
||
| 172 | |||
| 173 | 2 | return $this->addFoundJob($adjust, $foundJob, $foundJobCacheKey, $crcCacheKey); |
|
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param bool $adjust |
||
| 178 | */ |
||
| 179 | 2 | protected function addFoundJob($adjust, Job $foundJob, $foundJobCacheKey, $crcCacheKey) |
|
| 180 | { |
||
| 181 | 2 | $whenQueue = $this->getWhenQueueCacheKey(); |
|
| 182 | 2 | $result = $this->adjustJob($adjust, $whenQueue, $foundJob, $foundJobCacheKey, $crcCacheKey, $foundJob->getWhenUs()); |
|
| 183 | 2 | if (null !== $result) { |
|
| 184 | 2 | return $result; |
|
| 185 | } |
||
| 186 | if (null === $this->maxPriority) { |
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | |||
| 190 | $priorityQueue = $this->getPriorityQueueCacheKey(); |
||
| 191 | $result = $this->adjustJob($adjust, $priorityQueue, $foundJob, $foundJobCacheKey, $crcCacheKey, $foundJob->getPriority()); |
||
| 192 | |||
| 193 | return $result ?: false; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $queue |
||
| 198 | * @param bool $adjust |
||
| 199 | */ |
||
| 200 | 2 | private function adjustJob($adjust, $queue, Job $foundJob, $foundJobCacheKey, $crcCacheKey, $zScore) |
|
| 201 | { |
||
| 202 | 2 | if ($adjust && $this->redis->zRem($queue, $foundJob->getId()) > 0) { |
|
| 203 | 2 | if (!$this->insertJob($foundJob)) { |
|
| 204 | // Job is expired |
||
| 205 | $this->redis->lRem($crcCacheKey, 1, $foundJobCacheKey); |
||
| 206 | |||
| 207 | return false; |
||
| 208 | } |
||
| 209 | 2 | $this->redis->zAdd($queue, $zScore, $foundJob->getId()); |
|
| 210 | |||
| 211 | 2 | return $foundJob; |
|
| 212 | } |
||
| 213 | |||
| 214 | return null; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 219 | * |
||
| 220 | * @return Job|null |
||
| 221 | * |
||
| 222 | * @throws ClassNotSubclassException |
||
| 223 | * @throws PriorityException |
||
| 224 | */ |
||
| 225 | 20 | public function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
|
| 226 | { |
||
| 227 | 20 | if (!$job instanceof Job) { |
|
| 228 | throw new \InvalidArgumentException('$job must be instance of '.Job::class); |
||
| 229 | } |
||
| 230 | |||
| 231 | 20 | $this->validateSaveable($job); |
|
| 232 | 20 | $this->setJobId($job); |
|
| 233 | |||
| 234 | // Add to whenAt or priority queue? /// optimizaiton... |
||
| 235 | 20 | $whenUs = $job->getWhenUs(); |
|
| 236 | 20 | if (!$whenUs) { |
|
| 237 | $whenUs = Util::getMicrotimeDecimal(); |
||
| 238 | $job->setWhenUs($whenUs); |
||
| 239 | } |
||
| 240 | |||
| 241 | 20 | if (true === $job->getBatch()) { |
|
| 242 | // is there a CRC Hash already for this job |
||
| 243 | 2 | if ($oldJob = $this->batchSave($job)) { |
|
| 244 | 2 | return $oldJob; |
|
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | 20 | return $this->saveJob($job); |
|
| 249 | } |
||
| 250 | |||
| 251 | 20 | protected function saveJob(Job $job) |
|
| 252 | { |
||
| 253 | 20 | $whenQueue = $this->getWhenQueueCacheKey(); |
|
| 254 | 20 | $crcCacheKey = $this->getJobCrcHashKey($job->getCrcHash()); |
|
| 255 | // Save Job |
||
| 256 | 20 | if (!$this->insertJob($job)) { |
|
| 257 | // job is expired |
||
| 258 | 2 | return null; |
|
| 259 | } |
||
| 260 | 20 | $jobId = $job->getId(); |
|
| 261 | 20 | $when = $job->getWhenUs(); |
|
| 262 | // Add Job to CRC list |
||
| 263 | 20 | $this->redis->lPush($crcCacheKey, [$jobId]); |
|
| 264 | |||
| 265 | 20 | $this->redis->zAdd($whenQueue, $when, $jobId); |
|
| 266 | |||
| 267 | 20 | return $job; |
|
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param Job $job |
||
| 272 | * |
||
| 273 | * @return bool false if the job is already expired, true otherwise |
||
| 274 | */ |
||
| 275 | 20 | protected function insertJob(Job $job) |
|
| 276 | { |
||
| 277 | // Save Job |
||
| 278 | 20 | $jobCacheKey = $this->getJobCacheKey($job->getId()); |
|
| 279 | 20 | if ($expiresAt = $job->getExpiresAt()) { |
|
| 280 | 2 | $expiresAtTime = $expiresAt->getTimestamp() - time(); |
|
| 281 | 2 | if ($expiresAtTime <= 0) { |
|
| 282 | 2 | return false; /// ??? job is already expired |
|
| 283 | } |
||
| 284 | $this->redis->setEx($jobCacheKey, $expiresAtTime, $job->toMessage()); |
||
| 285 | |||
| 286 | return true; |
||
| 287 | } |
||
| 288 | 20 | $this->redis->set($jobCacheKey, $job->toMessage()); |
|
| 289 | |||
| 290 | 20 | return true; |
|
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Returns the prioirty in DESCENDING order, except if maxPrioirty is null, then prioirty is 0. |
||
| 295 | * |
||
| 296 | * @param int|null $priority |
||
| 297 | * |
||
| 298 | * @return int |
||
| 299 | */ |
||
| 300 | 20 | protected function calculatePriority($priority) |
|
| 301 | { |
||
| 302 | 20 | $priority = parent::calculatePriority($priority); |
|
| 303 | 20 | if (null === $priority) { |
|
| 304 | 18 | return null === $this->maxPriority ? 0 : $this->maxPriority; |
|
| 305 | } |
||
| 306 | |||
| 307 | 8 | if (null === $this->maxPriority) { |
|
| 308 | return 0; |
||
| 309 | } |
||
| 310 | |||
| 311 | // Redis priority should be in DESC order |
||
| 312 | 8 | return $this->maxPriority - $priority; |
|
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 317 | * |
||
| 318 | * @throws PriorityException |
||
| 319 | * @throws ClassNotSubclassException |
||
| 320 | */ |
||
| 321 | 20 | View Code Duplication | protected function validateSaveable(\Dtc\QueueBundle\Model\Job $job) |
| 322 | { |
||
| 323 | 20 | if (null !== $job->getPriority() && null === $this->maxPriority) { |
|
| 324 | throw new PriorityException('This queue does not support priorities'); |
||
| 325 | } |
||
| 326 | |||
| 327 | 20 | if (!$job instanceof RetryableJob) { |
|
| 328 | throw new ClassNotSubclassException('Job needs to be instance of '.RetryableJob::class); |
||
| 329 | } |
||
| 330 | 20 | } |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @param string|null $workerName |
||
| 334 | * @param string|null $methodName |
||
| 335 | * @param bool $prioritize |
||
| 336 | * |
||
| 337 | * @throws UnsupportedException |
||
| 338 | */ |
||
| 339 | 16 | protected function verifyGetJobArgs($workerName = null, $methodName = null, $prioritize = true) |
|
| 345 | |||
| 346 | 2 | public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
|
| 347 | { |
||
| 348 | 2 | $jobId = $job->getId(); |
|
| 349 | 2 | $priorityQueue = $this->getPriorityQueueCacheKey(); |
|
| 350 | 2 | $whenQueue = $this->getWhenQueueCacheKey(); |
|
| 351 | |||
| 352 | 2 | $deleted = false; |
|
| 353 | |||
| 354 | 2 | if ($this->redis->zRem($priorityQueue, $jobId)) { |
|
| 355 | $deleted = true; |
||
| 356 | 2 | } elseif ($this->redis->zRem($whenQueue, $jobId)) { |
|
| 363 | |||
| 364 | /** |
||
| 365 | * @param string|null $workerName |
||
| 366 | * @param string|null $methodName |
||
| 367 | * @param bool $prioritize |
||
| 368 | * @param mixed $runId |
||
| 369 | * |
||
| 370 | * @throws UnsupportedException |
||
| 371 | * |
||
| 372 | * @return Job|null |
||
| 373 | */ |
||
| 374 | 16 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
| 402 | |||
| 403 | 2 | public function getWaitingJobCount($workerName = null, $methodName = null) |
|
| 414 | |||
| 415 | 4 | View Code Duplication | public function resetJob(RetryableJob $job) |
| 429 | |||
| 430 | 2 | private function collateStatusResults(array &$results, $cacheKey) |
|
| 445 | |||
| 446 | 2 | private function extractStatusResults(array $jobs, array &$results) |
|
| 463 | |||
| 464 | 2 | private function extractStatusHashResults(array $hResults, array &$results) |
|
| 478 | |||
| 479 | 2 | public function getStatus() |
|
| 500 | |||
| 501 | 6 | public function retryableSaveHistory(RetryableJob $job, $retry) |
|
| 511 | } |
||
| 512 |
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: