1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Queue\Driver; |
4
|
|
|
|
5
|
|
|
use Zenstruck\Queue\Driver; |
6
|
|
|
use Zenstruck\Queue\Job; |
7
|
|
|
use Zenstruck\Queue\Payload; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Kevin Bond <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
abstract class BaseRedisDriver implements Driver |
13
|
|
|
{ |
14
|
|
|
private $queueName; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $queueName |
18
|
|
|
*/ |
19
|
2 |
|
public function __construct($queueName) |
20
|
|
|
{ |
21
|
2 |
|
$this->queueName = $queueName; |
22
|
2 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function push(Payload $payload) |
28
|
|
|
{ |
29
|
|
|
$this->rPush($this->queueName, $this->encodePayload($payload)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
2 |
|
public function pop() |
36
|
|
|
{ |
37
|
|
|
$rawPayload = $this->lPop($this->queueName); |
38
|
|
|
|
39
|
|
|
if (null === $rawPayload) { |
40
|
|
|
// empty queue |
41
|
2 |
|
return null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (null === $job = $this->decodePayload($rawPayload)) { |
45
|
|
|
// can't handle, requeue |
46
|
|
|
$this->rPush($this->queueName, $rawPayload); |
47
|
|
|
|
48
|
|
|
return null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $job; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function release(Job $job) |
58
|
|
|
{ |
59
|
|
|
$this->rPush($this->queueName, $this->encodePayload($job->payload(), $job->attempts())); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function delete(Job $job) |
66
|
|
|
{ |
67
|
|
|
// noop - job is deleted during pop |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Payload $payload |
72
|
|
|
* @param int $attempts |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
private function encodePayload(Payload $payload, $attempts = 0) |
77
|
|
|
{ |
78
|
|
|
return json_encode([ |
79
|
|
|
'payload' => $payload, |
80
|
|
|
'attempts' => $attempts, |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $rawPayload |
86
|
|
|
* |
87
|
|
|
* @return Job|null |
88
|
|
|
*/ |
89
|
|
|
private function decodePayload($rawPayload) |
90
|
|
|
{ |
91
|
|
|
$decodedPayload = json_decode($rawPayload, true); |
92
|
|
|
|
93
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (!is_array($decodedPayload)) { |
98
|
|
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!isset($decodedPayload['payload']) || !isset($decodedPayload['attempts'])) { |
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (!is_array($decodedPayload['payload'])) { |
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (null === $payload = Payload::fromArray($decodedPayload['payload'])) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return new Job($payload, $decodedPayload['attempts'] + 1); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $queueName |
118
|
|
|
* @param string $payload |
119
|
|
|
*/ |
120
|
|
|
abstract protected function rPush($queueName, $payload); |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $queueName |
124
|
|
|
* |
125
|
|
|
* @return string|null |
126
|
|
|
*/ |
127
|
|
|
abstract protected function lPop($queueName); |
128
|
|
|
} |
129
|
|
|
|