Completed
Push — master ( 7fae80...8ffdf8 )
by Matthew
07:25
created

BaseJobManager::resetJob()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
nc 2
cc 2
eloc 10
nop 1
crap 2
1
<?php
2
3
namespace Dtc\QueueBundle\Redis;
4
5
use Dtc\QueueBundle\Manager\JobIdTrait;
6
use Dtc\QueueBundle\Manager\JobTimingManager;
7
use Dtc\QueueBundle\Manager\PriorityJobManager;
8
use Dtc\QueueBundle\Manager\RunManager;
9
use Dtc\QueueBundle\Manager\VerifyTrait;
10
use Dtc\QueueBundle\Model\BaseJob;
11
use Dtc\QueueBundle\Model\RetryableJob;
12
use Dtc\QueueBundle\Util\Util;
13
14
/**
15
 * For future implementation.
16
 */
17
abstract class BaseJobManager extends PriorityJobManager
18
{
19
    use JobIdTrait;
20
    use VerifyTrait;
21
22
    /** @var RedisInterface */
23
    protected $redis;
24
25
    /** @var string */
26
    protected $cacheKeyPrefix;
27
28
    protected $hostname;
29
    protected $pid;
30
31
    /**
32
     * @param string $cacheKeyPrefix
33
     */
34 3
    public function __construct(RunManager $runManager, JobTimingManager $jobTimingManager, $jobClass, $cacheKeyPrefix)
35
    {
36 3
        $this->cacheKeyPrefix = $cacheKeyPrefix;
37 3
        $this->hostname = gethostname() ?: '';
38 3
        $this->pid = getmypid();
39
40 3
        parent::__construct($runManager, $jobTimingManager, $jobClass);
41 3
    }
42
43
    public function setRedis(RedisInterface $redis)
44
    {
45
        $this->redis = $redis;
46
    }
47
48 28
    protected function getJobCacheKey($jobId)
49
    {
50 28
        return $this->cacheKeyPrefix.'_job_'.$jobId;
51
    }
52
53
    /**
54
     * @param string $jobCrc
55
     */
56 28
    protected function getJobCrcHashKey($jobCrc)
57
    {
58 28
        return $this->cacheKeyPrefix.'_job_crc_'.$jobCrc;
59
    }
60
61 14
    protected function getPriorityQueueCacheKey()
62
    {
63 14
        return $this->cacheKeyPrefix.'_priority';
64
    }
65
66 28
    protected function getWhenQueueCacheKey()
67
    {
68 28
        return $this->cacheKeyPrefix.'_when';
69
    }
70
71 18
    protected function getStatusCacheKey()
72
    {
73 18
        return $this->cacheKeyPrefix.'_status';
74
    }
75
76
    abstract protected function saveJob(Job $job);
77
78 9 View Code Duplication
    public function resetJob(RetryableJob $job)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80 9
        if (!$job instanceof Job) {
81 3
            throw new \InvalidArgumentException('$job must be instance of '.Job::class);
82
        }
83 6
        $job->setStatus(BaseJob::STATUS_NEW);
84 6
        $job->setMessage(null);
85 6
        $job->setStartedAt(null);
86 6
        $job->setRetries($job->getRetries() + 1);
87 6
        $job->setUpdatedAt(Util::getMicrotimeDateTime());
88 6
        $this->saveJob($job);
89
90 6
        return true;
91
    }
92
93 3
    public function deleteJob(\Dtc\QueueBundle\Model\Job $job)
94
    {
95 3
        $jobId = $job->getId();
96 3
        $priorityQueue = $this->getPriorityQueueCacheKey();
97 3
        $whenQueue = $this->getWhenQueueCacheKey();
98
99 3
        $this->redis->zRem($priorityQueue, $jobId);
100 3
        $this->redis->zRem($whenQueue, $jobId);
101 3
        $this->redis->del([$this->getJobCacheKey($jobId)]);
102 3
        $this->redis->lRem($this->getJobCrcHashKey($job->getCrcHash()), 1, $jobId);
103 3
    }
104
}
105