|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Exception\PriorityException; |
|
6
|
|
|
|
|
7
|
|
|
abstract class PriorityJobManager extends AbstractJobManager |
|
8
|
|
|
{ |
|
9
|
|
|
const PRIORITY_ASC = 'asc'; |
|
10
|
|
|
const PRIORITY_DESC = 'desc'; |
|
11
|
|
|
|
|
12
|
|
|
protected $maxPriority; |
|
13
|
|
|
protected $priorityDirection = self::PRIORITY_DESC; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @return mixed |
|
17
|
|
|
*/ |
|
18
|
30 |
|
public function getMaxPriority() |
|
19
|
|
|
{ |
|
20
|
30 |
|
return $this->maxPriority; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param mixed $maxPriority |
|
25
|
|
|
*/ |
|
26
|
20 |
|
public function setMaxPriority($maxPriority) |
|
27
|
|
|
{ |
|
28
|
20 |
|
$this->maxPriority = $maxPriority; |
|
29
|
20 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return mixed |
|
33
|
|
|
*/ |
|
34
|
19 |
|
public function getPriorityDirection() |
|
35
|
|
|
{ |
|
36
|
19 |
|
return $this->priorityDirection; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param mixed $priorityDirection |
|
41
|
|
|
*/ |
|
42
|
22 |
|
public function setPriorityDirection($priorityDirection) |
|
43
|
|
|
{ |
|
44
|
22 |
|
$this->priorityDirection = $priorityDirection; |
|
45
|
22 |
|
} |
|
46
|
|
|
|
|
47
|
45 |
|
protected function validatePriority($priority) |
|
48
|
|
|
{ |
|
49
|
45 |
|
if (null === $priority) { |
|
50
|
42 |
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
11 |
|
if (!ctype_digit(strval($priority))) { |
|
54
|
|
|
throw new PriorityException("Priority ($priority) needs to be a positive integer"); |
|
55
|
|
|
} |
|
56
|
11 |
|
if (strval(intval($priority)) !== strval($priority)) { |
|
57
|
|
|
throw new PriorityException("Priority ($priority) needs to be less than ".PHP_INT_MAX); |
|
58
|
|
|
} |
|
59
|
11 |
|
$maxPriority = $this->getMaxPriority(); |
|
60
|
11 |
|
if (intval($priority) > $maxPriority) { |
|
61
|
4 |
|
throw new PriorityException("Priority ($priority) must be less than ".$maxPriority); |
|
62
|
|
|
} |
|
63
|
11 |
|
} |
|
64
|
|
|
|
|
65
|
45 |
|
protected function calculatePriority($priority) |
|
66
|
|
|
{ |
|
67
|
45 |
|
if (null === $priority) { |
|
68
|
42 |
|
return $priority; |
|
69
|
|
|
} |
|
70
|
11 |
|
if (self::PRIORITY_DESC === $this->priorityDirection) { |
|
71
|
11 |
|
$priority = $this->maxPriority - $priority; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
11 |
|
return $priority; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function findHigherPriority($priority1, $priority2) |
|
78
|
|
|
{ |
|
79
|
|
|
if (null === $priority1) { |
|
80
|
|
|
return $priority2; |
|
81
|
|
|
} |
|
82
|
|
|
if (null === $priority2) { |
|
83
|
|
|
return $priority1; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (self::PRIORITY_DESC === $this->priorityDirection) { |
|
87
|
|
|
return min($priority1, $priority2); |
|
88
|
|
|
} else { |
|
89
|
|
|
return max($priority1, $priority2); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
abstract protected function prioritySave(Job $job); |
|
94
|
|
|
|
|
95
|
45 |
|
protected function recordTiming(Job $job) |
|
96
|
|
|
{ |
|
97
|
45 |
|
$status = JobTiming::STATUS_INSERT; |
|
98
|
45 |
|
if ($job->getWhenAt() && $job->getWhenAt() > (new \DateTime())) { |
|
99
|
2 |
|
$status = JobTiming::STATUS_INSERT_DELAYED; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
45 |
|
$this->jobTiminigManager->recordTiming($status); |
|
103
|
45 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param Job $job |
|
107
|
|
|
* |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
* |
|
110
|
|
|
* @throws PriorityException |
|
111
|
|
|
*/ |
|
112
|
45 |
|
public function save(Job $job) |
|
113
|
|
|
{ |
|
114
|
45 |
|
$this->validatePriority($job->getPriority()); |
|
115
|
45 |
|
if (!$job->getId()) { // An unsaved job needs it's priority potentially adjusted |
|
116
|
45 |
|
$job->setPriority($this->calculatePriority($job->getPriority())); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
45 |
|
$result = $this->prioritySave($job); |
|
120
|
45 |
|
$this->recordTiming($job); |
|
121
|
|
|
|
|
122
|
45 |
|
return $result; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|