1
|
|
|
<?php |
2
|
|
|
namespace Disque; |
3
|
|
|
|
4
|
|
|
use Disque\Command; |
5
|
|
|
use Disque\Command\CommandInterface; |
6
|
|
|
use Disque\Command\InvalidCommandException; |
7
|
|
|
use Disque\Connection\Manager; |
8
|
|
|
use Disque\Connection\ManagerInterface; |
9
|
|
|
use Disque\Queue\Queue; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @method int ackJob(string... $ids) |
13
|
|
|
* @method string addJob(string $queue, string $payload, array $options = []) |
14
|
|
|
* @method int delJob(string... $ids) |
15
|
|
|
* @method int dequeue(string... $ids) |
16
|
|
|
* @method int enqueue(string... $ids) |
17
|
|
|
* @method int fastAck(string... $ids) |
18
|
|
|
* @method array getJob(string... $queues, array $options = []) |
19
|
|
|
* @method array hello() |
20
|
|
|
* @method string info() |
21
|
|
|
* @method array jscan(array $options = []) |
22
|
|
|
* @method int nack(string... $ids) |
23
|
|
|
* @method int qlen(string $queue) |
24
|
|
|
* @method array qpeek(string $queue, int $count) |
25
|
|
|
* @method array qscan(array $options = []) |
26
|
|
|
* @method array show(string $id) |
27
|
|
|
* @method int working(string $id) |
28
|
|
|
*/ |
29
|
|
|
class Client |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Connection manager |
33
|
|
|
* |
34
|
|
|
* @var ManagerInterface |
35
|
|
|
*/ |
36
|
|
|
protected $connectionManager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Command handlers |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $commandHandlers = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* List of built queues |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $queues; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* A list of credentials to Disque servers |
54
|
|
|
* |
55
|
|
|
* @var Disque\Connection\Credentials[] |
56
|
|
|
*/ |
57
|
|
|
private $servers; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create a new Client |
61
|
|
|
* |
62
|
|
|
* @param Disque\Connection\Credentials[] $servers |
63
|
|
|
*/ |
64
|
|
|
public function __construct(array $servers = []) |
65
|
|
|
{ |
66
|
|
|
foreach ([ |
67
|
|
|
new Command\AckJob(), |
68
|
|
|
new Command\AddJob(), |
69
|
|
|
new Command\DelJob(), |
70
|
|
|
new Command\Dequeue(), |
71
|
|
|
new Command\Enqueue(), |
72
|
|
|
new Command\FastAck(), |
73
|
|
|
new Command\GetJob(), |
74
|
|
|
new Command\Hello(), |
75
|
|
|
new Command\Info(), |
76
|
|
|
new Command\JScan(), |
77
|
|
|
new Command\Nack(), |
78
|
|
|
new Command\Pause(), |
79
|
|
|
new Command\QLen(), |
80
|
|
|
new Command\QPeek(), |
81
|
|
|
new Command\QScan(), |
82
|
|
|
new Command\QStat(), |
83
|
|
|
new Command\Show(), |
84
|
|
|
new Command\Working() |
85
|
|
|
] as $command) { |
86
|
|
|
$this->registerCommand($command); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->servers = $servers; |
90
|
|
|
|
91
|
|
|
$connectionManager = new Manager(); |
92
|
|
|
$this->setConnectionManager($connectionManager); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set a connection manager |
97
|
|
|
* |
98
|
|
|
* @param ManagerInterface $manager |
99
|
|
|
*/ |
100
|
|
|
public function setConnectionManager(ManagerInterface $manager) |
101
|
|
|
{ |
102
|
|
|
$this->connectionManager = $manager; |
103
|
|
|
foreach ($this->servers as $server) { |
104
|
|
|
$this->connectionManager->addServer($server); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the connection manager |
110
|
|
|
* |
111
|
|
|
* @return ManagerInterface Connection manager |
112
|
|
|
*/ |
113
|
|
|
public function getConnectionManager() |
114
|
|
|
{ |
115
|
|
|
return $this->connectionManager; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Tells if connection is established |
120
|
|
|
* |
121
|
|
|
* @return bool Success |
122
|
|
|
*/ |
123
|
|
|
public function isConnected() |
124
|
|
|
{ |
125
|
|
|
return $this->connectionManager->isConnected(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Connect to Disque |
130
|
|
|
* |
131
|
|
|
* @return Disque\Connection\Node\Node Connected node information |
132
|
|
|
* @throws Disque\Connection\ConnectionException |
133
|
|
|
*/ |
134
|
|
|
public function connect() |
135
|
|
|
{ |
136
|
|
|
return $this->connectionManager->connect(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @throws InvalidCommandException |
141
|
|
|
*/ |
142
|
|
|
public function __call($command, array $arguments) |
143
|
|
|
{ |
144
|
|
|
$command = strtoupper($command); |
145
|
|
|
if (!isset($this->commandHandlers[$command])) { |
146
|
|
|
throw new InvalidCommandException($command); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$command = $this->commandHandlers[$command]; |
150
|
|
|
$command->setArguments($arguments); |
151
|
|
|
$result = $this->connectionManager->execute($command); |
152
|
|
|
return $command->parse($result); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Register a command handler |
157
|
|
|
* |
158
|
|
|
* @param CommandInterface $commandHandler Command |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
public function registerCommand(CommandInterface $commandHandler) |
162
|
|
|
{ |
163
|
|
|
$command = strtoupper($commandHandler->getCommand()); |
164
|
|
|
$this->commandHandlers[$command] = $commandHandler; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get a queue |
169
|
|
|
* |
170
|
|
|
* @param string $name Queue name |
171
|
|
|
* @return Queue Queue |
172
|
|
|
*/ |
173
|
|
|
public function queue($name) |
174
|
|
|
{ |
175
|
|
|
if (!isset($this->queues[$name])) { |
176
|
|
|
$this->queues[$name] = new Queue($this, $name); |
177
|
|
|
} |
178
|
|
|
return $this->queues[$name]; |
179
|
|
|
} |
180
|
|
|
} |