1
|
|
|
<?php |
2
|
|
|
namespace Phloppy\Client; |
3
|
|
|
|
4
|
|
|
use Iterator; |
5
|
|
|
use Phloppy\Client\Node\JScanIterator; |
6
|
|
|
use Phloppy\Exception\CommandException; |
7
|
|
|
use Phloppy\NodeInfo; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Disque commands for local nodes. |
11
|
|
|
*/ |
12
|
|
|
class Node extends AbstractClient { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Authenticate against a Disque node. |
16
|
|
|
* |
17
|
|
|
* @param $password |
18
|
|
|
* @return boolean true if authenticated. False otherwise. |
19
|
|
|
* |
20
|
|
|
* @throws CommandException |
21
|
|
|
*/ |
22
|
2 |
|
public function auth($password) |
|
|
|
|
23
|
|
|
{ |
24
|
2 |
|
$rsp = $this->send(['AUTH', $password]); |
25
|
1 |
|
return $rsp === 'OK'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Send a ping command to the connected server. |
31
|
|
|
* |
32
|
|
|
* @return boolean True if the ping is acknowleged. False otherwise. |
33
|
|
|
*/ |
34
|
2 |
|
public function ping() |
|
|
|
|
35
|
|
|
{ |
36
|
2 |
|
return $this->send(['PING']) === 'PONG'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
1 |
|
public function info() |
44
|
|
|
{ |
45
|
1 |
|
$rsp = $this->send(['INFO']); |
46
|
1 |
|
$info = []; |
47
|
1 |
|
$sections = preg_split('/^#/m', $rsp); |
48
|
|
|
|
49
|
1 |
|
foreach ($sections as $section) { |
50
|
1 |
|
$lines = explode("\r\n", trim($section)); |
51
|
1 |
|
$header = trim($lines[0]); |
52
|
|
|
|
53
|
1 |
|
array_shift($lines); |
54
|
|
|
|
55
|
1 |
|
$lines = array_reduce($lines, function($c, $e) { |
56
|
1 |
|
list($k, $v) = explode(':', $e); |
57
|
1 |
|
$c[$k] = $v; |
58
|
|
|
|
59
|
1 |
|
return $c; |
60
|
1 |
|
}, []); |
61
|
|
|
|
62
|
1 |
|
$info[$header] = $lines; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
return $info; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return NodeInfo[] |
71
|
|
|
*/ |
72
|
2 |
|
public function hello() |
73
|
|
|
{ |
74
|
2 |
|
$nodes = []; |
75
|
2 |
|
$response = $this->send(['HELLO']); |
76
|
2 |
|
$version = array_shift($response); |
77
|
|
|
|
78
|
|
|
switch ($version) { |
79
|
2 |
|
case 1: |
80
|
|
|
// active node id |
81
|
2 |
|
array_shift($response); |
82
|
|
|
|
83
|
|
|
// in single node mode the server may not have |
84
|
|
|
// determinded its own IP address, so we just use the |
85
|
|
|
// node url that we used to connect. |
86
|
2 |
|
if (count($response === 1) && $response[0][1] === '') { |
87
|
2 |
|
$host = parse_url($this->stream->getNodeUrl(), PHP_URL_HOST); |
88
|
2 |
|
$response[0][1] = $host; |
89
|
2 |
|
} |
90
|
|
|
|
91
|
2 |
|
foreach ($response as $node) { |
92
|
2 |
|
$server = 'tcp://'.$node[1].':'.$node[2]; |
93
|
2 |
|
$nodes[] = new NodeInfo($node[0], $server, $node[3]); |
94
|
2 |
|
} |
95
|
|
|
|
96
|
2 |
|
break; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
return $nodes; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* DELETE jobs from the connected node. |
105
|
|
|
* |
106
|
|
|
* @param string[] $jobs Job IDs to delete. |
107
|
|
|
* |
108
|
|
|
* @return int Number of jobs deleted |
109
|
|
|
*/ |
110
|
3 |
|
public function del(array $jobs) |
111
|
|
|
{ |
112
|
|
|
try { |
113
|
3 |
|
return (int) $this->send(array_merge(['DELJOB'], $jobs)); |
114
|
1 |
|
} catch (CommandException $e) { |
115
|
1 |
|
return 0; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Retrieve an Iterator over available jobs on the connected Disque node. |
123
|
|
|
* |
124
|
|
|
* @param int $count |
125
|
|
|
* @param array $queues |
126
|
|
|
* @param array $states |
127
|
|
|
* @param $format |
128
|
|
|
* |
129
|
|
|
* @return Iterator |
|
|
|
|
130
|
|
|
* @see https://github.com/antirez/disque#jscan-cursor-count-count-busyloop-queue-queue-state-state1-state-state2--state-staten-reply-allid |
|
|
|
|
131
|
|
|
*/ |
132
|
1 |
|
public function jscan($count = 50, array $queues = [], array $states = [], $format = JScanIterator::FORMAT_ID) |
133
|
|
|
{ |
134
|
1 |
|
$iterator = new JScanIterator($this->stream, $this->log); |
135
|
1 |
|
$iterator->setCount($count); |
136
|
1 |
|
$iterator->setQueues($queues); |
137
|
1 |
|
$iterator->setStates($states); |
138
|
1 |
|
$iterator->setFormat($format); |
139
|
|
|
|
140
|
1 |
|
return $iterator; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.