Completed
Pull Request — master (#30)
by Matthew
19:54
created

RetryableJobManager   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 4
dl 0
loc 231
rs 8.8
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
retryableSave() 0 1 ?
retryableSaveHistory() 0 1 ?
A save() 0 14 3
A saveHistory() 0 15 4
A updateJobException() 0 4 1
A updateJobMax() 0 15 4
A updateJobFailure() 0 4 1
A updateMaxStatus() 0 10 3
A resetRetryableJob() 0 10 2
resetJob() 0 1 ?
B setBaseRetryableJobDefaults() 0 20 5
A getDefaultMaxRetries() 0 4 1
A setDefaultMaxRetries() 0 4 1
A getDefaultMaxFailures() 0 4 1
A setDefaultMaxFailures() 0 4 1
A getAutoRetryOnFailure() 0 4 1
A setAutoRetryOnFailure() 0 4 1
A getAutoRetryOnException() 0 4 1
A setAutoRetryOnException() 0 4 1
A getDefaultMaxExceptions() 0 4 1
A setDefaultMaxExceptions() 0 4 1
A recordTiming() 0 9 3
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
    public function save(Job $job)
30
    {
31
        if (!$job instanceof RetryableJob) {
32
            throw new \InvalidArgumentException('Job is not instanceof '.RetryableJob::class);
33
        }
34
35
        if (!$job->getId()) {
36
            $this->setBaseRetryableJobDefaults($job);
37
        }
38
        $this->recordTiming($job);
39
        $job->setUpdatedAt(new \DateTime());
40
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
    public function saveHistory(Job $job)
50
    {
51
        if (!$job instanceof RetryableJob) {
52
            throw new \InvalidArgumentException('job not instance of '.RetryableJob::class);
53
        }
54
55
        switch ($job->getStatus()) {
56
            case BaseJob::STATUS_FAILURE:
57
                return $this->retryableSaveHistory($job, $this->updateJobFailure($job));
58
            case BaseJob::STATUS_EXCEPTION:
59
                return $this->retryableSaveHistory($job, $this->updateJobException($job));
60
        }
61
62
        return $this->retryableSaveHistory($job, false);
63
    }
64
65
    private function updateJobException(RetryableJob $job)
66
    {
67
        return $this->updateJobMax($job, 'Exceptions', RetryableJob::STATUS_MAX_EXCEPTIONS, $this->autoRetryOnException);
68
    }
69
70
    protected function updateJobMax(RetryableJob $job, $type, $maxStatus, $autoRetry)
71
    {
72
        $setMethod = 'set'.$type;
73
        $getMethod = 'get'.$type;
74
        $getMax = 'getMax'.$type;
75
        $job->$setMethod(intval($job->$getMethod()) + 1);
76
        if (!$this->updateMaxStatus($job, $maxStatus, $job->$getMax(), $job->$getMethod()) &&
77
            !$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
            if ($autoRetry) {
79
                return $this->resetRetryableJob($job);
80
            }
81
        }
82
83
        return false;
84
    }
85
86
    private function updateJobFailure(RetryableJob $job)
87
    {
88
        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
    protected function updateMaxStatus(RetryableJob $job, $status, $max = null, $count = 0)
102
    {
103
        if (null !== $max && $count >= $max) {
104
            $job->setStatus($status);
105
106
            return true;
107
        }
108
109
        return false;
110
    }
111
112
    protected function resetRetryableJob(RetryableJob $job)
113
    {
114
        if ($this->resetJob($job)) {
115
            $this->getJobTimingManager()->recordTiming(JobTiming::STATUS_INSERT);
116
117
            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
    protected function setBaseRetryableJobDefaults(RetryableJob $job)
131
    {
132
        if (null === $job->getMaxExceptions()) {
133
            $job->setMaxExceptions($this->defaultMaxExceptions);
134
        }
135
136
        if (null === $job->getMaxRetries()) {
137
            $job->setMaxRetries($this->defaultMaxRetries);
138
        }
139
140
        if (null === $job->getMaxFailures()) {
141
            $job->setMaxFailures($this->defaultMaxFailures);
142
        }
143
144
        if (null === $job->getCrcHash()) {
145
            $hashValues = array(get_class($job), $job->getMethod(), $job->getWorkerName(), $job->getArgs());
146
            $crcHash = hash('sha256', serialize($hashValues));
147
            $job->setCrcHash($crcHash);
148
        }
149
    }
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
    public function setAutoRetryOnFailure($autoRetryOnFailure)
195
    {
196
        $this->autoRetryOnFailure = $autoRetryOnFailure;
197
    }
198
199
    /**
200
     * @return bool
201
     */
202
    public function getAutoRetryOnException()
203
    {
204
        return $this->autoRetryOnException;
205
    }
206
207
    /**
208
     * @param bool $autoRetryOnException
209
     */
210
    public function setAutoRetryOnException($autoRetryOnException)
211
    {
212
        $this->autoRetryOnException = $autoRetryOnException;
213
    }
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
    protected function recordTiming(Job $job)
232
    {
233
        $status = JobTiming::STATUS_INSERT;
234
        if ($job->getWhenAt() && $job->getWhenAt() > (new \DateTime())) {
235
            $status = JobTiming::STATUS_INSERT_DELAYED;
236
        }
237
238
        $this->getJobTimingManager()->recordTiming($status);
239
    }
240
}
241