1
|
|
|
<?php |
2
|
|
|
namespace Workana\AsyncJobs; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use Bernard\Message; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Abstract job |
9
|
|
|
* |
10
|
|
|
* @author Carlos Frutos <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
abstract class Job implements Message |
13
|
|
|
{ |
14
|
|
|
const DEFAULT_MAX_RETRIES = 10; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
protected $delay = 0; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $retries = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $maxRetries = self::DEFAULT_MAX_RETRIES; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string? |
33
|
|
|
*/ |
34
|
|
|
protected $preferredQueueName; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public final function getName() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$class = get_class($this); |
42
|
|
|
|
43
|
|
|
return current(array_reverse(explode('\\', $class))); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get delay |
48
|
|
|
* |
49
|
|
|
* @return int |
50
|
|
|
*/ |
51
|
|
|
public function getDelay() |
52
|
|
|
{ |
53
|
|
|
return $this->delay; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* With delay (in seconds) |
58
|
|
|
* |
59
|
|
|
* @param int $delay Delay |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function withDelay($delay = 0) |
64
|
|
|
{ |
65
|
|
|
$this->delay = (int) $delay; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Increment retries, step one |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
public function incrementRetries() |
74
|
|
|
{ |
75
|
|
|
$nextRetry = $this->retries + 1; |
76
|
|
|
|
77
|
|
|
if ($nextRetry > $this->maxRetries) { |
78
|
|
|
throw new InvalidArgumentException('Max retries exceeded'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->retries = $nextRetry; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Total retries |
86
|
|
|
* |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function getRetries() |
90
|
|
|
{ |
91
|
|
|
return $this->retries; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Are retries exhausted? |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function areRetriesExhausted() |
100
|
|
|
{ |
101
|
|
|
return ($this->retries === $this->maxRetries); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Max retries |
106
|
|
|
* |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
|
|
public function getMaxRetries() |
110
|
|
|
{ |
111
|
|
|
return $this->maxRetries; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param int $maxRetries |
116
|
|
|
* |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
|
|
public function withMaxRetries($maxRetries) |
120
|
|
|
{ |
121
|
|
|
$this->maxRetries = (int) $maxRetries; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getPreferredQueueName() |
125
|
|
|
{ |
126
|
|
|
return $this->preferredQueueName; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function hasPreferredQueue() |
130
|
|
|
{ |
131
|
|
|
return !empty($this->preferredQueueName); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function withPreferredQueueName($preferredQueueName) |
135
|
|
|
{ |
136
|
|
|
$this->preferredQueueName = $preferredQueueName ?(string) $preferredQueueName : null; |
137
|
|
|
} |
138
|
|
|
} |