Completed
Push — master ( b7b31c...0ef8ec )
by Matthew
04:34
created

JobManager::saveHistory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Dtc\QueueBundle\Beanstalkd;
4
5
use Dtc\QueueBundle\Model\AbstractJobManager;
6
use Dtc\QueueBundle\Model\Job as BaseJob;
7
use Dtc\QueueBundle\Exception\UnsupportedException;
8
use Pheanstalk\Pheanstalk;
9
10
class JobManager extends AbstractJobManager
11
{
12
    const DEFAULT_RESERVE_TIMEOUT = 5; // seconds
13
14
    /** @var Pheanstalk */
15
    protected $beanstalkd;
16
17
    protected $tube;
18
19
    protected $reserveTimeout = self::DEFAULT_RESERVE_TIMEOUT;
20
21 2
    public function setBeanstalkd(Pheanstalk $beanstalkd)
22
    {
23 2
        $this->beanstalkd = $beanstalkd;
24 2
    }
25
26
    public function setTube($tube)
27
    {
28
        $this->tube = $tube;
29
    }
30
31
    public function setReserveTimeout($timeout)
32
    {
33
        $this->reserveTimeout = $timeout;
34
    }
35
36 5
    public function save(\Dtc\QueueBundle\Model\Job $job)
37
    {
38
        /** @var Job $job */
39 5
        $message = $job->toMessage();
40 5
        $arguments = [$message, $job->getPriority(), $job->getDelay(), $job->getTtr()];
41 5
        $method = 'put';
42 5
        if ($this->tube) {
43
            array_unshift($arguments, $this->tube);
44
            $method .= 'InTube';
45
        }
46 5
        $jobId = call_user_func_array([$this->beanstalkd, $method], $arguments);
47 5
        $job->setId($jobId);
48
49
        // Ideally we should get this from beanstalk, but to save the roundtrip time, we do this here
50 5
        $job->setBeanJob($this->getBeanJob($jobId, $message));
51
52 5
        return $job;
53
    }
54
55 5
    public function getBeanJob($jobId, $data)
56
    {
57 5
        return new \Pheanstalk\Job($jobId, $data);
58
    }
59
60
    /**
61
     * @param string|null     $workerName
62
     * @param string|null     $methodName
63
     * @param bool            $prioritize
64
     * @param int|string|null $runId
65
     *
66
     * @return Job|null
67
     *
68
     * @throws UnsupportedException
69
     */
70 6
    public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null)
71
    {
72 6
        if (null !== $methodName) {
73
            throw new UnsupportedException('Unsupported');
74
        }
75 6
        if (null !== $workerName) {
76 1
            throw new UnsupportedException('Unsupported');
77
        }
78
79 5
        $beanstalkd = $this->beanstalkd;
80 5
        if ($this->tube) {
81
            $beanstalkd = $this->beanstalkd->watch($this->tube);
82
        }
83
84
        do {
85 5
            $expiredJob = false;
86 5
            $job = $this->findJob($beanstalkd, $expiredJob, $runId);
87 5
        } while ($expiredJob);
88
89 5
        return $job;
90
    }
91
92
    /**
93
     * @param Pheanstalk      $beanstalkd
94
     * @param bool            $expiredJob
95
     * @param int|string|null $runId
96
     *
97
     * @return Job|null
98
     */
99 5
    protected function findJob(Pheanstalk $beanstalkd, &$expiredJob, $runId)
100
    {
101 5
        $beanJob = $beanstalkd->reserve($this->reserveTimeout);
102 5
        if ($beanJob) {
103 4
            $job = new Job();
104 4
            $job->fromMessage($beanJob->getData());
105 4
            $job->setId($beanJob->getId());
106 4
            $job->setRunId($runId);
107
108 4
            if (($expiresAt = $job->getExpiresAt()) && $expiresAt->getTimestamp() < time()) {
109
                $expiredJob = true;
110
                $beanstalkd->delete($beanJob);
111
112
                return null;
113
            }
114 4
            $job->setBeanJob($beanJob);
115
116 4
            return $job;
117
        }
118
119 3
        return null;
120
    }
121
122 2
    public function deleteJob(\Dtc\QueueBundle\Model\Job $job)
123
    {
124 2
        $this->beanstalkd
125 2
            ->delete($job);
126 2
    }
127
128
    // Save History get called upon completion of the job
129 1
    public function saveHistory(\Dtc\QueueBundle\Model\Job $job)
130
    {
131 1
        if (BaseJob::STATUS_SUCCESS === $job->getStatus()) {
132 1
            $this->beanstalkd
133 1
                ->delete($job);
134
        }
135 1
    }
136
137 1
    public function getStats()
138
    {
139 1
        return $this->beanstalkd->stats();
140
    }
141
}
142