Completed
Pull Request — master (#30)
by Matthew
13:27 queued 10:46
created

RetryableJobManager::updateJobFailure()   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 1
crap 1
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 41
    public function save(Job $job)
30
    {
31 41
        if (!$job instanceof RetryableJob) {
32
            throw new \InvalidArgumentException('Job is not instanceof '.RetryableJob::class);
33
        }
34
35 41
        if (!$job->getId()) {
36 41
            $this->setBaseRetryableJobDefaults($job);
37
        }
38 41
        $this->recordTiming($job);
39 41
        $job->setUpdatedAt(new \DateTime());
40
41 41
        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 7
    public function saveHistory(Job $job)
50
    {
51 7
        if (!$job instanceof RetryableJob) {
52
            throw new \InvalidArgumentException('job not instance of '.RetryableJob::class);
53
        }
54
55 7
        switch ($job->getStatus()) {
56 7
            case BaseJob::STATUS_FAILURE:
57 3
                return $this->retryableSaveHistory($job, $this->updateJobFailure($job));
58 4
            case BaseJob::STATUS_EXCEPTION:
59 2
                return $this->retryableSaveHistory($job, $this->updateJobException($job));
60
        }
61
62 2
        return $this->retryableSaveHistory($job, false);
63
    }
64
65 2
    private function updateJobException(RetryableJob $job)
66
    {
67 2
        return $this->updateJobMax($job, 'Exceptions', RetryableJob::STATUS_MAX_EXCEPTIONS, $this->autoRetryOnException);
68
    }
69
70 7
    protected function updateJobMax(RetryableJob $job, $type, $maxStatus, $autoRetry)
71
    {
72 7
        $setMethod = 'set'.$type;
73 7
        $getMethod = 'get'.$type;
74 7
        $getMax = 'getMax'.$type;
75 7
        $job->$setMethod(intval($job->$getMethod()) + 1);
76 7
        if (!$this->updateMaxStatus($job, $maxStatus, $job->$getMax(), $job->$getMethod()) &&
77 7
            !$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 7
            if ($autoRetry) {
79 6
                return $this->resetRetryableJob($job);
80
            }
81
        }
82
83 7
        return false;
84
    }
85
86 3
    private function updateJobFailure(RetryableJob $job)
87
    {
88 3
        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 9
    protected function updateMaxStatus(RetryableJob $job, $status, $max = null, $count = 0)
102
    {
103 9
        if (null !== $max && $count >= $max) {
104 8
            $job->setStatus($status);
105
106 8
            return true;
107
        }
108
109 9
        return false;
110
    }
111
112 6
    protected function resetRetryableJob(RetryableJob $job)
113
    {
114 6
        if ($this->resetJob($job)) {
115 6
            $this->getJobTimingManager()->recordTiming(JobTiming::STATUS_INSERT);
116
117 6
            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 41
    protected function setBaseRetryableJobDefaults(RetryableJob $job)
131
    {
132 41
        if (null === $job->getMaxExceptions()) {
133 35
            $job->setMaxExceptions($this->defaultMaxExceptions);
134
        }
135
136 41
        if (null === $job->getMaxRetries()) {
137 35
            $job->setMaxRetries($this->defaultMaxRetries);
138
        }
139
140 41
        if (null === $job->getMaxFailures()) {
141 35
            $job->setMaxFailures($this->defaultMaxFailures);
142
        }
143
144 41
        if (null === $job->getCrcHash()) {
145 41
            $hashValues = array(get_class($job), $job->getMethod(), $job->getWorkerName(), $job->getArgs());
146 41
            $crcHash = hash('sha256', serialize($hashValues));
147 41
            $job->setCrcHash($crcHash);
148
        }
149 41
    }
150
151
    /**
152
     * @return int|null
153
     */
154
    public function getDefaultMaxRetries()
155
    {
156
        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
    public function setDefaultMaxRetries($defaultMaxRetries)
163
    {
164
        $this->defaultMaxRetries = $defaultMaxRetries;
165
    }
166
167
    /**
168
     * @return int|null
169
     */
170
    public function getDefaultMaxFailures()
171
    {
172
        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
    public function setDefaultMaxFailures($defaultMaxFailures)
179
    {
180
        $this->defaultMaxFailures = $defaultMaxFailures;
181
    }
182
183
    /**
184
     * @return bool
185
     */
186
    public function getAutoRetryOnFailure()
187
    {
188
        return $this->autoRetryOnFailure;
189
    }
190
191
    /**
192
     * @param bool $autoRetryOnFailure
193
     */
194 4
    public function setAutoRetryOnFailure($autoRetryOnFailure)
195
    {
196 4
        $this->autoRetryOnFailure = $autoRetryOnFailure;
197 4
    }
198
199
    /**
200
     * @return bool
201
     */
202
    public function getAutoRetryOnException()
203
    {
204
        return $this->autoRetryOnException;
205
    }
206
207
    /**
208
     * @param bool $autoRetryOnException
209
     */
210 2
    public function setAutoRetryOnException($autoRetryOnException)
211
    {
212 2
        $this->autoRetryOnException = $autoRetryOnException;
213 2
    }
214
215
    /**
216
     * @return int|null
217
     */
218
    public function getDefaultMaxExceptions()
219
    {
220
        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
    public function setDefaultMaxExceptions($defaultMaxExceptions)
227
    {
228
        $this->defaultMaxExceptions = $defaultMaxExceptions;
229
    }
230
231 41
    protected function recordTiming(Job $job)
232
    {
233 41
        $status = JobTiming::STATUS_INSERT;
234 41
        if ($job->getWhenAt() && $job->getWhenAt() > (new \DateTime())) {
235 2
            $status = JobTiming::STATUS_INSERT_DELAYED;
236
        }
237
238 41
        $this->getJobTimingManager()->recordTiming($status);
239 41
    }
240
}
241