1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Manager; |
4
|
|
|
|
5
|
|
|
use Dtc\QueueBundle\Exception\PriorityException; |
6
|
|
|
use Dtc\QueueBundle\Model\RetryableJob; |
7
|
|
|
use Dtc\QueueBundle\Model\Job; |
8
|
|
|
|
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() |
21
|
|
|
{ |
22
|
38 |
|
return $this->maxPriority; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param mixed $maxPriority |
27
|
|
|
*/ |
28
|
20 |
|
public function setMaxPriority($maxPriority) |
29
|
|
|
{ |
30
|
20 |
|
$this->maxPriority = $maxPriority; |
31
|
20 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
19 |
|
public function getPriorityDirection() |
37
|
|
|
{ |
38
|
19 |
|
return $this->priorityDirection; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param mixed $priorityDirection |
43
|
|
|
*/ |
44
|
24 |
|
public function setPriorityDirection($priorityDirection) |
45
|
|
|
{ |
46
|
24 |
|
$this->priorityDirection = $priorityDirection; |
47
|
24 |
|
} |
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
|
|
|
abstract protected function prioritySave(Job $job); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Job $job |
91
|
|
|
* |
92
|
|
|
* @throws PriorityException |
93
|
|
|
*/ |
94
|
69 |
|
protected function retryableSave(RetryableJob $job) |
95
|
|
|
{ |
96
|
69 |
|
$this->validatePriority($job->getPriority()); |
97
|
69 |
|
if (!$job->getId()) { // An unsaved job needs it's priority potentially adjusted |
98
|
69 |
|
$job->setPriority($this->calculatePriority($job->getPriority())); |
99
|
|
|
} |
100
|
|
|
|
101
|
69 |
|
$result = $this->prioritySave($job); |
102
|
|
|
|
103
|
69 |
|
return $result; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|