Completed
Pull Request — master (#30)
by Matthew
06:35
created

RetryableJobManager::resetRetryableJob()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace Dtc\QueueBundle\Manager;
4
5
use Dtc\QueueBundle\Model\BaseJob;
6
use Dtc\QueueBundle\Model\RetryableJob;
7
use Dtc\QueueBundle\Model\Job;
8
use Dtc\QueueBundle\Model\JobTiming;
9
10
abstract class RetryableJobManager extends AbstractJobManager
11
{
12
    protected $defaultMaxRetries;
13
    protected $defaultMaxFailures;
14
    protected $defaultMaxExceptions;
15
16
    protected $autoRetryOnFailure;
17
    protected $autoRetryOnException;
18
19
    abstract protected function retryableSave(RetryableJob $job);
20
21
    /**
22
     * @param RetryableJob $job
23
     * @param $retry bool
24
     *
25
     * @return
26
     */
27
    abstract protected function retryableSaveHistory(RetryableJob $job, $retry);
28
29 76
    public function save(Job $job)
30
    {
31 76
        if (!$job instanceof RetryableJob) {
32 3
            throw new \InvalidArgumentException('Job is not instanceof '.RetryableJob::class);
33
        }
34
35 76
        if (!$job->getId()) {
36 76
            $this->setBaseRetryableJobDefaults($job);
37
        }
38 76
        $this->recordTiming($job);
39 76
        $job->setUpdatedAt(new \DateTime());
40
41 76
        return $this->retryableSave($job);
42
    }
43
44
    /**
45
     * @return bool true if retry
46
     *
47
     * @param bool $retry true if the job was retried, false if not
0 ignored issues
show
Bug introduced by
There is no parameter named $retry. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
48
     */
49 20
    public function saveHistory(Job $job)
50
    {
51 20
        if (!$job instanceof RetryableJob) {
52 3
            throw new \InvalidArgumentException('job not instance of '.RetryableJob::class);
53
        }
54
55 20
        switch ($job->getStatus()) {
56 20
            case BaseJob::STATUS_FAILURE:
57 9
                return $this->retryableSaveHistory($job, $this->updateJobFailure($job));
58 11
            case BaseJob::STATUS_EXCEPTION:
59 6
                return $this->retryableSaveHistory($job, $this->updateJobException($job));
60
        }
61
62 5
        return $this->retryableSaveHistory($job, false);
63
    }
64
65 6
    private function updateJobException(RetryableJob $job)
66
    {
67 6
        return $this->updateJobMax($job, 'Exceptions', RetryableJob::STATUS_MAX_EXCEPTIONS, $this->autoRetryOnException);
68
    }
69
70 17
    protected function updateJobMax(RetryableJob $job, $type, $maxStatus, $autoRetry)
71
    {
72 17
        $setMethod = 'set'.$type;
73 17
        $getMethod = 'get'.$type;
74 17
        $getMax = 'getMax'.$type;
75 17
        $job->$setMethod(intval($job->$getMethod()) + 1);
76 17
        if (!$this->updateMaxStatus($job, $maxStatus, $job->$getMax(), $job->$getMethod()) &&
77 17
            !$this->updateMaxStatus($job, RetryableJob::STATUS_MAX_RETRIES, $job->getMaxRetries(), $job->getRetries())) {
0 ignored issues
show
Bug introduced by
It seems like $job->getMaxRetries() targeting Dtc\QueueBundle\Model\Re...bleJob::getMaxRetries() can also be of type integer; however, Dtc\QueueBundle\Manager\...ager::updateMaxStatus() does only seem to accept null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
78 16
            if ($autoRetry) {
79 14
                return $this->resetRetryableJob($job);
80
            }
81
        }
82
83 17
        return false;
84
    }
85
86 9
    private function updateJobFailure(RetryableJob $job)
87
    {
88 9
        return $this->updateJobMax($job, 'Failures', RetryableJob::STATUS_MAX_FAILURES, $this->autoRetryOnFailure);
89
    }
90
91
    /**
92
     * Determine if we've hit a max retry condition.
93
     *
94
     * @param RetryableJob $job
95
     * @param $status
96
     * @param null $max
97
     * @param int  $count
98
     *
99
     * @return bool
100
     */
101 19
    protected function updateMaxStatus(RetryableJob $job, $status, $max = null, $count = 0)
102
    {
103 19
        if (null !== $max && $count >= $max) {
104 17
            $job->setStatus($status);
105
106 17
            return true;
107
        }
108
109 18
        return false;
110
    }
111
112 14
    protected function resetRetryableJob(RetryableJob $job)
113
    {
114 14
        if ($this->resetJob($job)) {
115 14
            $this->getJobTimingManager()->recordTiming(JobTiming::STATUS_INSERT);
116
117 14
            return true;
118
        }
119
120
        return false;
121
    }
122
123
    /**
124
     * @param RetryableJob $retryableJob
0 ignored issues
show
Bug introduced by
There is no parameter named $retryableJob. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
125
     *
126
     * @return bool true if the job was successfully "reset", i.e. re-queued
127
     */
128
    abstract protected function resetJob(RetryableJob $job);
129
130 76
    protected function setBaseRetryableJobDefaults(RetryableJob $job)
131
    {
132 76
        if (null === $job->getMaxExceptions()) {
133 43
            $job->setMaxExceptions($this->defaultMaxExceptions);
134
        }
135
136 76
        if (null === $job->getMaxRetries()) {
137 43
            $job->setMaxRetries($this->defaultMaxRetries);
138
        }
139
140 76
        if (null === $job->getMaxFailures()) {
141 43
            $job->setMaxFailures($this->defaultMaxFailures);
142
        }
143
144 76
        if (null === $job->getCrcHash()) {
145 76
            $hashValues = array(get_class($job), $job->getMethod(), $job->getWorkerName(), $job->getArgs());
146 76
            $crcHash = hash('sha256', serialize($hashValues));
147 76
            $job->setCrcHash($crcHash);
148
        }
149 76
    }
150
151
    /**
152
     * @return int|null
153
     */
154 6
    public function getDefaultMaxRetries()
155
    {
156 6
        return $this->defaultMaxRetries;
157
    }
158
159
    /**
160
     * @param int|null $defaultMaxRetry
0 ignored issues
show
Bug introduced by
There is no parameter named $defaultMaxRetry. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
161
     */
162 6
    public function setDefaultMaxRetries($defaultMaxRetries)
163
    {
164 6
        $this->defaultMaxRetries = $defaultMaxRetries;
165 6
    }
166
167
    /**
168
     * @return int|null
169
     */
170 6
    public function getDefaultMaxFailures()
171
    {
172 6
        return $this->defaultMaxFailures;
173
    }
174
175
    /**
176
     * @param int|null $defaultMaxFailure
0 ignored issues
show
Documentation introduced by
There is no parameter named $defaultMaxFailure. Did you maybe mean $defaultMaxFailures?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
177
     */
178 6
    public function setDefaultMaxFailures($defaultMaxFailures)
179
    {
180 6
        $this->defaultMaxFailures = $defaultMaxFailures;
181 6
    }
182
183
    /**
184
     * @return bool
185
     */
186 6
    public function getAutoRetryOnFailure()
187
    {
188 6
        return $this->autoRetryOnFailure;
189
    }
190
191
    /**
192
     * @param bool $autoRetryOnFailure
193
     */
194 18
    public function setAutoRetryOnFailure($autoRetryOnFailure)
195
    {
196 18
        $this->autoRetryOnFailure = $autoRetryOnFailure;
197 18
    }
198
199
    /**
200
     * @return bool
201
     */
202 6
    public function getAutoRetryOnException()
203
    {
204 6
        return $this->autoRetryOnException;
205
    }
206
207
    /**
208
     * @param bool $autoRetryOnException
209
     */
210 12
    public function setAutoRetryOnException($autoRetryOnException)
211
    {
212 12
        $this->autoRetryOnException = $autoRetryOnException;
213 12
    }
214
215
    /**
216
     * @return int|null
217
     */
218 6
    public function getDefaultMaxExceptions()
219
    {
220 6
        return $this->defaultMaxExceptions;
221
    }
222
223
    /**
224
     * @param int|null $defaultMaxException
0 ignored issues
show
Documentation introduced by
There is no parameter named $defaultMaxException. Did you maybe mean $defaultMaxExceptions?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
225
     */
226 6
    public function setDefaultMaxExceptions($defaultMaxExceptions)
227
    {
228 6
        $this->defaultMaxExceptions = $defaultMaxExceptions;
229 6
    }
230
231 76
    protected function recordTiming(Job $job)
232
    {
233 76
        $status = JobTiming::STATUS_INSERT;
234 76
        if ($job->getWhenAt() && $job->getWhenAt() > (new \DateTime())) {
235 4
            $status = JobTiming::STATUS_INSERT_DELAYED;
236
        }
237
238 76
        $this->getJobTimingManager()->recordTiming($status);
239 76
    }
240
}
241