Completed
Push — master ( 0b0f4b...0669e2 )
by Matthew
03:15
created

JobManager::getBeanJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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 Pheanstalk\Pheanstalk;
8
9
class JobManager extends AbstractJobManager
10
{
11
    const DEFAULT_RESERVE_TIMEOUT = 5; // seconds
12
13
    /** @var Pheanstalk */
14
    protected $beanstalkd;
15
16
    protected $tube;
17
18
    protected $reserveTimeout = self::DEFAULT_RESERVE_TIMEOUT;
19
20 2
    public function setBeanstalkd(Pheanstalk $beanstalkd)
21
    {
22 2
        $this->beanstalkd = $beanstalkd;
23 2
    }
24
25
    public function setTube($tube)
26
    {
27
        $this->tube = $tube;
28
    }
29
30
    public function setReserveTimeout($timeout)
31
    {
32
        $this->reserveTimeout = $timeout;
33
    }
34
35 6
    public function save(\Dtc\QueueBundle\Model\Job $job)
36
    {
37
        /** @var Job $job */
38 6
        $message = $job->toMessage();
39 6
        $arguments = [$message, $job->getPriority(), $job->getDelay(), $job->getTtr()];
40 6
        $method = 'put';
41 6
        if ($this->tube) {
42
            array_unshift($arguments, $this->tube);
43
            $method .= 'InTube';
44
        }
45 6
        $jobId = call_user_func_array([$this->beanstalkd, $method], $arguments);
46 6
        $job->setId($jobId);
47
48
        // Ideally we should get this from beanstalk, but to save the roundtrip time, we do this here
49 6
        $job->setBeanJob($this->getBeanJob($jobId, $message));
50
51 6
        return $job;
52
    }
53
54 6
    public function getBeanJob($jobId, $data)
55
    {
56 6
        return new \Pheanstalk\Job($jobId, $data);
57
    }
58
59 6
    /**
60
     * @param string|null     $workerName
61 6
     * @param string|null     $methodName
62
     * @param bool            $prioritize
63
     * @param int|string|null $runId
64
     *
65 6
     * @return Job|null
66 6
     *
67
     * @throws \Exception
68
     */
69
    public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null)
70 6
    {
71
        if ($methodName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $methodName of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
72
            throw new \Exception('Unsupported');
73 6
        }
74 6
75 5
        $beanstalkd = $this->beanstalkd;
76 5
        if ($this->tube) {
77 5
            $beanstalkd = $this->beanstalkd->watch($this->tube);
78 5
        }
79
80 5
        $job = null;
0 ignored issues
show
Unused Code introduced by
$job is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
        do {
82
            $expiredJob = false;
83
            $job = $this->findJob($beanstalkd, $expiredJob, $runId);
84
        } while ($expiredJob);
85 5
86
        return $job;
87 5
    }
88
89 3
    /**
90 3
     * @param Pheanstalk      $beanstalkd
91
     * @param bool            $expiredJob
92 2
     * @param int|string|null $runId
93
     *
94 2
     * @return Job|null
95 2
     */
96 2
    protected function findJob(Pheanstalk $beanstalkd, &$expiredJob, $runId)
97
    {
98
        $beanJob = $beanstalkd->reserve($this->reserveTimeout);
99 1
        if ($beanJob) {
100
            $job = new Job();
101 1
            $job->fromMessage($beanJob->getData());
102 1
            $job->setId($beanJob->getId());
103 1
            $job->setRunId($runId);
104
105 1
            if (($expiresAt = $job->getExpiresAt()) && $expiresAt->getTimestamp() < time()) {
106
                $expiredJob = true;
107 1
                $beanstalkd->delete($beanJob);
108
109 1
                return null;
110
            }
111
            $job->setBeanJob($beanJob);
112
113
            return $job;
114
        }
115
116
        return null;
117
    }
118
119
    public function deleteJob(\Dtc\QueueBundle\Model\Job $job)
120
    {
121
        $this->beanstalkd
122
            ->delete($job);
123
    }
124
125
    // Save History get called upon completion of the job
126
    public function saveHistory(\Dtc\QueueBundle\Model\Job $job)
127
    {
128
        if (BaseJob::STATUS_SUCCESS === $job->getStatus()) {
129
            $this->beanstalkd
130
                ->delete($job);
131
        }
132
    }
133
134
    public function getStats()
135
    {
136
        return $this->beanstalkd->stats();
137
    }
138
}
139