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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Tarantool\Queue; |
15
|
|
|
|
16
|
|
|
use Tarantool\Client\Client; |
17
|
|
|
|
18
|
|
|
class Queue |
19
|
|
|
{ |
20
|
|
|
private $client; |
21
|
|
|
private $name; |
22
|
|
|
|
23
|
344 |
|
public function __construct(Client $client, string $name) |
24
|
|
|
{ |
25
|
344 |
|
$this->client = $client; |
26
|
344 |
|
$this->name = $name; |
27
|
344 |
|
} |
28
|
|
|
|
29
|
2 |
|
public function getClient() : Client |
30
|
|
|
{ |
31
|
2 |
|
return $this->client; |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
public function getName() : string |
35
|
|
|
{ |
36
|
2 |
|
return $this->name; |
37
|
|
|
} |
38
|
|
|
|
39
|
64 |
|
public function put($data, array $options = []) : Task |
40
|
|
|
{ |
41
|
64 |
|
return Task::fromTuple( |
42
|
64 |
|
$this->client->call("queue.tube.$this->name:put", $data, $options)[0] |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
48 |
|
public function take(float $timeout = null) : ?Task |
47
|
|
|
{ |
48
|
48 |
|
$result = $this->client->call("queue.tube.$this->name:take", $timeout); |
49
|
|
|
|
50
|
48 |
|
return empty($result[0]) ? null : Task::fromTuple($result[0]); |
51
|
|
|
} |
52
|
|
|
|
53
|
12 |
|
public function touch(int $taskId, float $increment) : ?Task |
54
|
|
|
{ |
55
|
12 |
|
$result = $this->client->call("queue.tube.$this->name:touch", $taskId, $increment); |
56
|
|
|
|
57
|
12 |
|
return empty($result[0]) ? null : Task::fromTuple($result[0]); |
58
|
|
|
} |
59
|
|
|
|
60
|
16 |
|
public function ack(int $taskId) : Task |
61
|
|
|
{ |
62
|
16 |
|
return Task::fromTuple( |
63
|
16 |
|
$this->client->call("queue.tube.$this->name:ack", $taskId)[0] |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
24 |
|
public function release(int $taskId, array $options = []) : Task |
68
|
|
|
{ |
69
|
24 |
|
return Task::fromTuple( |
70
|
24 |
|
$this->client->call("queue.tube.$this->name:release", $taskId, $options)[0] |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
24 |
|
public function peek(int $taskId) : Task |
75
|
|
|
{ |
76
|
24 |
|
return Task::fromTuple( |
77
|
24 |
|
$this->client->call("queue.tube.$this->name:peek", $taskId)[0] |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
16 |
|
public function bury(int $taskId) : Task |
82
|
|
|
{ |
83
|
16 |
|
return Task::fromTuple( |
84
|
16 |
|
$this->client->call("queue.tube.$this->name:bury", $taskId)[0] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
16 |
|
public function kick(int $count) : int |
89
|
|
|
{ |
90
|
16 |
|
return $this->client->call("queue.tube.$this->name:kick", $count)[0]; |
91
|
|
|
} |
92
|
|
|
|
93
|
20 |
|
public function delete(int $taskId) : Task |
94
|
|
|
{ |
95
|
20 |
|
return Task::fromTuple( |
96
|
20 |
|
$this->client->call("queue.tube.$this->name:delete", $taskId)[0] |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
16 |
|
public function truncate() : void |
101
|
|
|
{ |
102
|
16 |
|
$this->client->call("queue.tube.$this->name:truncate"); |
103
|
16 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @throws \InvalidArgumentException |
107
|
|
|
* |
108
|
|
|
* @return array|int |
109
|
|
|
*/ |
110
|
116 |
|
public function stats(?string $path = null) |
111
|
|
|
{ |
112
|
116 |
|
[$stats] = $this->client->call('queue.stats', $this->name); |
113
|
|
|
|
114
|
116 |
|
if (null === $path) { |
115
|
16 |
|
return $stats; |
116
|
|
|
} |
117
|
|
|
|
118
|
100 |
|
foreach (\explode('.', $path) as $key) { |
119
|
100 |
|
if (!isset($stats[$key])) { |
120
|
72 |
|
throw new \InvalidArgumentException(\sprintf('Invalid path "%s"', $path)); |
121
|
|
|
} |
122
|
60 |
|
$stats = $stats[$key]; |
123
|
|
|
} |
124
|
|
|
|
125
|
28 |
|
return $stats; |
126
|
|
|
} |
127
|
|
|
|
128
|
8 |
|
public function call(string $methodName, ...$args) : array |
129
|
|
|
{ |
130
|
8 |
|
return $this->client->call("queue.tube.$this->name:$methodName", ...$args); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|