/
Queue.php
329 lines (298 loc) · 8.32 KB
/
Queue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
namespace yii\queue;
use Yii;
use yii\base\Component;
use yii\base\InvalidArgumentException;
use yii\base\InvalidConfigException;
use yii\di\Instance;
use yii\helpers\VarDumper;
use yii\queue\serializers\PhpSerializer;
use yii\queue\serializers\SerializerInterface;
/**
* Base Queue.
*
* @author Roman Zhuravlev <zhuravljov@gmail.com>
*/
abstract class Queue extends Component
{
/**
* @event PushEvent
*/
const EVENT_BEFORE_PUSH = 'beforePush';
/**
* @event PushEvent
*/
const EVENT_AFTER_PUSH = 'afterPush';
/**
* @event ExecEvent
*/
const EVENT_BEFORE_EXEC = 'beforeExec';
/**
* @event ExecEvent
*/
const EVENT_AFTER_EXEC = 'afterExec';
/**
* @event ExecEvent
*/
const EVENT_AFTER_ERROR = 'afterError';
/**
* @see Queue::isWaiting()
*/
const STATUS_WAITING = 1;
/**
* @see Queue::isReserved()
*/
const STATUS_RESERVED = 2;
/**
* @see Queue::isDone()
*/
const STATUS_DONE = 3;
/**
* @var bool whether to enable strict job type control.
* Note that in order to enable type control, a pushing job must be [[JobInterface]] instance.
* @since 2.0.1
*/
public $strictJobType = true;
/**
* @var SerializerInterface|array
*/
public $serializer = PhpSerializer::class;
/**
* @var int default time to reserve a job
*/
public $ttr = 300;
/**
* @var int default attempt count
*/
public $attempts = 1;
private $pushTtr;
private $pushDelay;
private $pushPriority;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->serializer = Instance::ensure($this->serializer, SerializerInterface::class);
if (!is_numeric($this->ttr)) {
throw new InvalidConfigException('Default TTR must be integer.');
}
$this->ttr = (int) $this->ttr;
if ($this->ttr <= 0) {
throw new InvalidConfigException('Default TTR must be greater that zero.');
}
if (!is_numeric($this->attempts)) {
throw new InvalidConfigException('Default attempts count must be integer.');
}
$this->attempts = (int) $this->attempts;
if ($this->attempts <= 0) {
throw new InvalidConfigException('Default attempts count must be greater that zero.');
}
}
/**
* Sets TTR for job execute.
*
* @param int|mixed $value
* @return $this
*/
public function ttr($value)
{
$this->pushTtr = $value;
return $this;
}
/**
* Sets delay for later execute.
*
* @param int|mixed $value
* @return $this
*/
public function delay($value)
{
$this->pushDelay = $value;
return $this;
}
/**
* Sets job priority.
*
* @param mixed $value
* @return $this
*/
public function priority($value)
{
$this->pushPriority = $value;
return $this;
}
/**
* Pushes job into queue.
*
* @param JobInterface|mixed $job
* @return string|null id of a job message
*/
public function push($job)
{
$event = new PushEvent([
'job' => $job,
'ttr' => $this->pushTtr ?: (
$job instanceof RetryableJobInterface
? $job->getTtr()
: $this->ttr
),
'delay' => $this->pushDelay ?: 0,
'priority' => $this->pushPriority,
]);
$this->pushTtr = null;
$this->pushDelay = null;
$this->pushPriority = null;
$this->trigger(self::EVENT_BEFORE_PUSH, $event);
if ($event->handled) {
return null;
}
if ($this->strictJobType && !($event->job instanceof JobInterface)) {
throw new InvalidArgumentException('Job must be instance of JobInterface.');
}
if (!is_numeric($event->ttr)) {
throw new InvalidArgumentException('Job TTR must be integer.');
}
$event->ttr = (int) $event->ttr;
if ($event->ttr <= 0) {
throw new InvalidArgumentException('Job TTR must be greater that zero.');
}
if (!is_numeric($event->delay)) {
throw new InvalidArgumentException('Job delay must be integer.');
}
$event->delay = (int) $event->delay;
if ($event->delay < 0) {
throw new InvalidArgumentException('Job delay must be positive.');
}
$message = $this->serializer->serialize($event->job);
$event->id = $this->pushMessage($message, $event->ttr, $event->delay, $event->priority);
$this->trigger(self::EVENT_AFTER_PUSH, $event);
return $event->id;
}
/**
* @param string $message
* @param int $ttr time to reserve in seconds
* @param int $delay
* @param mixed $priority
* @return string id of a job message
*/
abstract protected function pushMessage($message, $ttr, $delay, $priority);
/**
* Uses for CLI drivers and gets process ID of a worker.
*
* @since 2.0.2
*/
public function getWorkerPid()
{
return null;
}
/**
* @param string $id of a job message
* @param string $message
* @param int $ttr time to reserve
* @param int $attempt number
* @return bool
*/
protected function handleMessage($id, $message, $ttr, $attempt)
{
list($job, $error) = $this->unserializeMessage($message);
$event = new ExecEvent([
'id' => $id,
'job' => $job,
'ttr' => $ttr,
'attempt' => $attempt,
'error' => $error,
]);
$this->trigger(self::EVENT_BEFORE_EXEC, $event);
if ($event->handled) {
return true;
}
if ($event->error) {
return $this->handleError($event);
}
try {
$event->result = $event->job->execute($this);
} catch (\Exception $error) {
$event->error = $error;
return $this->handleError($event);
} catch (\Throwable $error) {
$event->error = $error;
return $this->handleError($event);
}
$this->trigger(self::EVENT_AFTER_EXEC, $event);
return true;
}
/**
* Unserializes.
*
* @param string $id of the job
* @param string $serialized message
* @return array pair of a job and error that
*/
public function unserializeMessage($serialized)
{
try {
$job = $this->serializer->unserialize($serialized);
} catch (\Exception $e) {
return [null, new InvalidJobException($serialized, $e->getMessage(), 0, $e)];
}
if ($job instanceof JobInterface) {
return [$job, null];
}
return [null, new InvalidJobException($serialized, sprintf(
'Job must be a JobInterface instance instead of %s.',
VarDumper::dumpAsString($job)
))];
}
/**
* @param ExecEvent $event
* @return bool
* @internal
*/
public function handleError(ExecEvent $event)
{
$event->retry = $event->attempt < $this->attempts;
if ($event->error instanceof InvalidJobException) {
$event->retry = false;
} elseif ($event->job instanceof RetryableJobInterface) {
$event->retry = $event->job->canRetry($event->attempt, $event->error);
}
$this->trigger(self::EVENT_AFTER_ERROR, $event);
return !$event->retry;
}
/**
* @param string $id of a job message
* @return bool
*/
public function isWaiting($id)
{
return $this->status($id) === self::STATUS_WAITING;
}
/**
* @param string $id of a job message
* @return bool
*/
public function isReserved($id)
{
return $this->status($id) === self::STATUS_RESERVED;
}
/**
* @param string $id of a job message
* @return bool
*/
public function isDone($id)
{
return $this->status($id) === self::STATUS_DONE;
}
/**
* @param string $id of a job message
* @return int status code
*/
abstract public function status($id);
}