1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\QueuedJobs\Jobs; |
4
|
|
|
|
5
|
|
|
use AsyncPHP\Doorman\Cancellable; |
6
|
|
|
use AsyncPHP\Doorman\Expires; |
7
|
|
|
use AsyncPHP\Doorman\Process; |
8
|
|
|
use AsyncPHP\Doorman\Task; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; |
11
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJob; |
12
|
|
|
|
13
|
|
|
class DoormanQueuedJobTask implements Task, Expires, Process, Cancellable |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
protected $id; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var QueuedJobDescriptor |
22
|
|
|
*/ |
23
|
|
|
protected $descriptor; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Reload descriptor from DB |
27
|
|
|
*/ |
28
|
|
|
protected function refreshDescriptor() |
29
|
|
|
{ |
30
|
|
|
if ($this->descriptor) { |
31
|
|
|
$this->descriptor = QueuedJobDescriptor::get()->byID($this->descriptor->ID); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
* |
38
|
|
|
* @return null|int |
39
|
|
|
*/ |
40
|
|
|
public function getId() |
41
|
|
|
{ |
42
|
|
|
return $this->id; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
* |
48
|
|
|
* @param int $id |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function setId($id) |
53
|
|
|
{ |
54
|
|
|
$this->id = $id; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return QueuedJobDescriptor |
61
|
|
|
*/ |
62
|
|
|
public function getDescriptor() |
63
|
|
|
{ |
64
|
|
|
return $this->descriptor; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param QueuedJobDescriptor $descriptor |
69
|
|
|
*/ |
70
|
|
|
public function __construct(QueuedJobDescriptor $descriptor) |
71
|
|
|
{ |
72
|
|
|
$this->descriptor = $descriptor; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function serialize() |
81
|
|
|
{ |
82
|
|
|
return serialize(array( |
83
|
|
|
'descriptor' => $this->descriptor->ID, |
84
|
|
|
)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritdoc |
89
|
|
|
* |
90
|
|
|
* @throws InvalidArgumentException |
91
|
|
|
* @param string |
92
|
|
|
*/ |
93
|
|
|
public function unserialize($serialized) |
94
|
|
|
{ |
95
|
|
|
$data = unserialize($serialized); |
96
|
|
|
|
97
|
|
|
if (!isset($data['descriptor'])) { |
98
|
|
|
throw new InvalidArgumentException('Malformed data'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$descriptor = QueuedJobDescriptor::get() |
102
|
|
|
->filter('ID', $data['descriptor']) |
103
|
|
|
->first(); |
104
|
|
|
|
105
|
|
|
if (!$descriptor) { |
106
|
|
|
throw new InvalidArgumentException('Descriptor not found'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->descriptor = $descriptor; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getHandler() |
116
|
|
|
{ |
117
|
|
|
return 'DoormanQueuedJobHandler'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function getData() |
124
|
|
|
{ |
125
|
|
|
return array( |
126
|
|
|
'descriptor' => $this->descriptor, |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function ignoresRules() |
134
|
|
|
{ |
135
|
|
|
if ($this->descriptor->hasMethod('ignoreRules')) { |
136
|
|
|
return $this->descriptor->ignoreRules(); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function stopsSiblings() |
146
|
|
|
{ |
147
|
|
|
if ($this->descriptor->hasMethod('stopsSiblings')) { |
148
|
|
|
return $this->descriptor->stopsSiblings(); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritdoc |
156
|
|
|
* |
157
|
|
|
* @return int |
158
|
|
|
*/ |
159
|
|
|
public function getExpiresIn() |
160
|
|
|
{ |
161
|
|
|
if ($this->descriptor->hasMethod('getExpiresIn')) { |
162
|
|
|
return $this->descriptor->getExpiresIn(); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return -1; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @inheritdoc |
170
|
|
|
* |
171
|
|
|
* @param int $startedAt |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
public function shouldExpire($startedAt) |
175
|
|
|
{ |
176
|
|
|
if ($this->descriptor->hasMethod('shouldExpire')) { |
177
|
|
|
return $this->descriptor->shouldExpire($startedAt); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return true; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @inheritdoc |
185
|
|
|
* |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
View Code Duplication |
public function canRunTask() |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
$this->refreshDescriptor(); |
191
|
|
|
return in_array( |
192
|
|
|
$this->descriptor->JobStatus, |
193
|
|
|
array( |
194
|
|
|
QueuedJob::STATUS_NEW, |
195
|
|
|
QueuedJob::STATUS_INIT, |
196
|
|
|
QueuedJob::STATUS_WAIT |
197
|
|
|
) |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @inheritdoc |
203
|
|
|
* |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
View Code Duplication |
public function isCancelled() |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
$this->refreshDescriptor(); |
209
|
|
|
|
210
|
|
|
// Treat completed jobs as cancelled when it comes to how Doorman handles picking up jobs to run |
211
|
|
|
$cancelledStates = [ |
212
|
|
|
QueuedJob::STATUS_CANCELLED, |
213
|
|
|
QueuedJob::STATUS_COMPLETE, |
214
|
|
|
]; |
215
|
|
|
|
216
|
|
|
return in_array($this->descriptor->JobStatus, $cancelledStates, true); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.