1 | <?php |
||
9 | abstract class PriorityJobManager extends RetryableJobManager |
||
10 | { |
||
11 | const PRIORITY_ASC = 'asc'; |
||
12 | const PRIORITY_DESC = 'desc'; |
||
13 | |||
14 | protected $maxPriority; |
||
15 | protected $priorityDirection = self::PRIORITY_DESC; |
||
16 | |||
17 | /** |
||
18 | * @return mixed |
||
19 | */ |
||
20 | 38 | public function getMaxPriority() |
|
24 | |||
25 | /** |
||
26 | * @param mixed $maxPriority |
||
27 | */ |
||
28 | 20 | public function setMaxPriority($maxPriority) |
|
32 | |||
33 | /** |
||
34 | * @return mixed |
||
35 | */ |
||
36 | 19 | public function getPriorityDirection() |
|
40 | |||
41 | /** |
||
42 | * @param mixed $priorityDirection |
||
43 | */ |
||
44 | 24 | public function setPriorityDirection($priorityDirection) |
|
48 | |||
49 | 69 | protected function validatePriority($priority) |
|
50 | { |
||
51 | 69 | if (null === $priority) { |
|
52 | 64 | return; |
|
53 | } |
||
54 | |||
55 | 19 | if (!ctype_digit(strval($priority))) { |
|
56 | throw new PriorityException("Priority ($priority) needs to be a positive integer"); |
||
57 | } |
||
58 | 19 | if (strval(intval($priority)) !== strval($priority)) { |
|
59 | throw new PriorityException("Priority ($priority) needs to be less than ".PHP_INT_MAX); |
||
60 | } |
||
61 | 19 | $maxPriority = $this->getMaxPriority(); |
|
62 | 19 | if (intval($priority) > $maxPriority) { |
|
63 | 8 | throw new PriorityException("Priority ($priority) must be less than ".$maxPriority); |
|
64 | } |
||
65 | 19 | } |
|
66 | |||
67 | /** |
||
68 | * Returns the prioirty in ASCENDING order regardless of the User's choice of direction |
||
69 | * (for storing RabbitMQ, Mysql, others). |
||
70 | * |
||
71 | * @param $priority |
||
72 | * |
||
73 | * @return mixed |
||
74 | */ |
||
75 | 69 | protected function calculatePriority($priority) |
|
76 | { |
||
77 | 69 | if (null === $priority) { |
|
78 | 64 | return $priority; |
|
79 | } |
||
80 | 19 | if (self::PRIORITY_DESC === $this->priorityDirection) { |
|
81 | 19 | $priority = $this->maxPriority - $priority; |
|
82 | } |
||
83 | |||
84 | 19 | return $priority; |
|
85 | } |
||
86 | |||
87 | protected function findHigherPriority($priority1, $priority2) |
||
102 | |||
103 | abstract protected function prioritySave(Job $job); |
||
104 | |||
105 | /** |
||
106 | * @param Job $job |
||
107 | * |
||
108 | * @throws PriorityException |
||
109 | */ |
||
110 | 69 | protected function retryableSave(RetryableJob $job) |
|
121 | } |
||
122 |