Issues (219)

Manager/StallableJobManager.php (2 issues)

Severity
1
<?php
2
3
namespace Dtc\QueueBundle\Manager;
4
5
use Dtc\QueueBundle\Model\Job;
6
use Dtc\QueueBundle\Model\RetryableJob;
7
use Dtc\QueueBundle\Model\StallableJob;
8
9
abstract class StallableJobManager extends PriorityJobManager
10
{
11
    protected $defaultMaxStalls;
12
13
    abstract protected function stallableSave(StallableJob $job);
14
15
    abstract public function pruneStalledJobs($workerName = null, $method = null, callable $progressCallback = null);
16
17
    abstract public function resetStalledJobs($workerName = null, $method = null, callable $progressCallback = null);
18
19 4
    public static function getAllStatuses()
20
    {
21 4
        $statuses = parent::getAllStatuses();
22 4
        $statuses[StallableJob::STATUS_STALLED] = 0;
23 4
        $statuses[StallableJob::STATUS_MAX_STALLS] = 0;
24
25 4
        return $statuses;
26
    }
27
28 43
    protected function prioritySave(Job $job)
29
    {
30 43
        if (!$job instanceof StallableJob) {
31
            throw new \InvalidArgumentException('Job is not instanceof '.StallableJob::class);
32
        }
33
34 43
        if (!$job->getId()) {
35 43
            if ($job instanceof StallableJob) {
0 ignored issues
show
$job is always a sub-type of Dtc\QueueBundle\Model\StallableJob.
Loading history...
36 43
                $this->setStallableJobDefaults($job);
37
            }
38
        }
39
40 43
        return $this->stallableSave($job);
41
    }
42
43
    /**
44
     * @param $retry true if retry
45
     *
46
     * @return bool true if retry
47
     */
48
    abstract protected function stallableSaveHistory(StallableJob $job, $retry);
49
50
    /**
51
     * @return bool true if retry
52
     *
53
     * @param Job $job
54
     */
55 12
    protected function retryableSaveHistory(RetryableJob $job, $retry)
56
    {
57 12
        if (!$job instanceof StallableJob) {
58
            throw new \InvalidArgumentException('job not instance of '.StallableJob::class);
59
        }
60
61 12
        if ($retry) {
62 4
            return $this->stallableSaveHistory($job, $retry);
63
        }
64
65 12
        if (StallableJob::STATUS_STALLED === $job->getStatus()) {
66 2
            return $this->stallableSaveHistory($job, $this->updateJobStalled($job));
67
        }
68
69 10
        return $this->stallableSaveHistory($job, false);
70
    }
71
72
    /**
73
     * @return bool false if
74
     */
75 2
    private function updateJobStalled(StallableJob $job)
76
    {
77 2
        return $this->updateJobMax($job, 'Stalls', StallableJob::STATUS_MAX_STALLS, true);
78
    }
79
80 43
    protected function setStallableJobDefaults(StallableJob $job)
81
    {
82 43
        if (null === $job->getMaxStalls()) {
0 ignored issues
show
The condition null === $job->getMaxStalls() is always false.
Loading history...
83 43
            $job->setMaxStalls($this->defaultMaxStalls);
84
        }
85 43
    }
86
87
    /**
88
     * @return int|null
89
     */
90 2
    public function getDefaultMaxStalls()
91
    {
92 2
        return $this->defaultMaxStalls;
93
    }
94
95
    /**
96
     * @param int|null $defaultMaxStalls
97
     */
98 2
    public function setDefaultMaxStalls($defaultMaxStalls)
99
    {
100 2
        $this->defaultMaxStalls = $defaultMaxStalls;
101 2
    }
102
}
103