1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tarantool Queue package. |
5
|
|
|
* |
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tarantool\Queue; |
13
|
|
|
|
14
|
|
|
class Queue |
15
|
|
|
{ |
16
|
|
|
private $client; |
17
|
|
|
private $tubeName; |
18
|
|
|
private $prefix; |
19
|
|
|
|
20
|
157 |
|
public function __construct(\Tarantool $client, $tubeName) |
21
|
|
|
{ |
22
|
157 |
|
$this->client = $client; |
23
|
157 |
|
$this->tubeName = $tubeName; |
24
|
157 |
|
$this->prefix = "queue.tube.$tubeName:"; |
25
|
157 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param mixed $data |
29
|
|
|
* @param array|null $options |
30
|
|
|
* |
31
|
|
|
* @return Task |
32
|
|
|
*/ |
33
|
34 |
View Code Duplication |
public function put($data, array $options = null) |
|
|
|
|
34
|
|
|
{ |
35
|
34 |
|
$args = $options ? [$data, $options] : [$data]; |
36
|
34 |
|
$result = $this->client->call($this->prefix.'put', $args); |
37
|
|
|
|
38
|
34 |
|
return Task::createFromTuple($result[0]); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param int|float|null $timeout |
43
|
|
|
* |
44
|
|
|
* @return Task|null |
45
|
|
|
*/ |
46
|
20 |
|
public function take($timeout = null) |
47
|
|
|
{ |
48
|
20 |
|
$args = null === $timeout ? [] : [$timeout]; |
49
|
20 |
|
$result = $this->client->call($this->prefix.'take', $args); |
50
|
|
|
|
51
|
20 |
|
return empty($result[0]) ? null : Task::createFromTuple($result[0]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param int $taskId |
56
|
|
|
* |
57
|
|
|
* @return Task |
58
|
|
|
*/ |
59
|
9 |
|
public function ack($taskId) |
60
|
|
|
{ |
61
|
9 |
|
$result = $this->client->call($this->prefix.'ack', [$taskId]); |
62
|
|
|
|
63
|
5 |
|
return Task::createFromTuple($result[0]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param int $taskId |
68
|
|
|
* @param array|null $options |
69
|
|
|
* |
70
|
|
|
* @return Task |
71
|
|
|
*/ |
72
|
12 |
View Code Duplication |
public function release($taskId, array $options = null) |
|
|
|
|
73
|
|
|
{ |
74
|
12 |
|
$args = $options ? [$taskId, $options] : [$taskId]; |
75
|
12 |
|
$result = $this->client->call($this->prefix.'release', $args); |
76
|
|
|
|
77
|
8 |
|
return Task::createFromTuple($result[0]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param int $taskId |
82
|
|
|
* |
83
|
|
|
* @return Task |
84
|
|
|
*/ |
85
|
13 |
|
public function peek($taskId) |
86
|
|
|
{ |
87
|
13 |
|
$result = $this->client->call($this->prefix.'peek', [$taskId]); |
88
|
|
|
|
89
|
9 |
|
return Task::createFromTuple($result[0]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param int $taskId |
94
|
|
|
* |
95
|
|
|
* @return Task |
96
|
|
|
*/ |
97
|
9 |
|
public function bury($taskId) |
98
|
|
|
{ |
99
|
9 |
|
$result = $this->client->call($this->prefix.'bury', [$taskId]); |
100
|
|
|
|
101
|
5 |
|
return Task::createFromTuple($result[0]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param int $count |
106
|
|
|
* |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
13 |
|
public function kick($count) |
110
|
|
|
{ |
111
|
13 |
|
$result = $this->client->call($this->prefix.'kick', [$count]); |
112
|
|
|
|
113
|
9 |
|
return $result[0][0]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param int $taskId |
118
|
|
|
* |
119
|
|
|
* @return Task |
120
|
|
|
*/ |
121
|
11 |
|
public function delete($taskId) |
122
|
|
|
{ |
123
|
11 |
|
$result = $this->client->call($this->prefix.'delete', [$taskId]); |
124
|
|
|
|
125
|
7 |
|
return Task::createFromTuple($result[0]); |
126
|
|
|
} |
127
|
|
|
|
128
|
9 |
|
public function truncate() |
129
|
|
|
{ |
130
|
9 |
|
$this->client->call($this->prefix.'truncate'); |
131
|
9 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string|null $path |
135
|
|
|
* |
136
|
|
|
* @return array|int |
137
|
|
|
* |
138
|
|
|
* @throws \InvalidArgumentException |
139
|
|
|
*/ |
140
|
43 |
|
public function statistics($path = null) |
141
|
|
|
{ |
142
|
43 |
|
$result = $this->client->call('queue.statistics', [$this->tubeName]); |
143
|
|
|
|
144
|
43 |
|
if (null === $path) { |
145
|
9 |
|
return $result[0][0]; |
146
|
|
|
} |
147
|
|
|
|
148
|
34 |
|
$result = $result[0][0]; |
149
|
34 |
|
foreach (explode('.', $path) as $key) { |
150
|
34 |
|
if (!isset($result[$key])) { |
151
|
9 |
|
throw new \InvalidArgumentException(sprintf('Invalid path "%s".', $path)); |
152
|
|
|
} |
153
|
29 |
|
$result = $result[$key]; |
154
|
29 |
|
} |
155
|
|
|
|
156
|
25 |
|
return $result; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.