1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPDaemon\Clients\GearmanClient; |
4
|
|
|
|
5
|
|
|
use PHPDaemon\Network\ClientConnection; |
6
|
|
|
use PHPDaemon\Utils\Crypt; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Connections |
10
|
|
|
*/ |
11
|
|
|
class Connection extends ClientConnection |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Magic code for request |
15
|
|
|
*/ |
16
|
|
|
const MAGIC_REQUEST = "\0REQ"; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Magic code for response |
20
|
|
|
*/ |
21
|
|
|
const MAGIC_RESPONSE = "\0RES"; |
22
|
|
|
|
23
|
|
|
/* |
24
|
|
|
* Byte length of header |
25
|
|
|
*/ |
26
|
|
|
const HEADER_LENGTH = 12; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Header binary format |
30
|
|
|
*/ |
31
|
|
|
const HEADER_WRITE_FORMAT = "a4NN"; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Header read format |
35
|
|
|
*/ |
36
|
|
|
const HEADER_READ_FORMAT = "a4magic/Ntype/Nsize"; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Delimeter for function arguments |
40
|
|
|
*/ |
41
|
|
|
const ARGS_DELIMITER = "\0"; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Request codes |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected static $requestCommandList = [ |
50
|
|
|
'CAN_DO' => 1, |
51
|
|
|
'CANT_DO' => 2, |
52
|
|
|
'RESET_ABILITIES' => 3, |
53
|
|
|
'PRE_SLEEP' => 4, |
54
|
|
|
'SUBMIT_JOB' => 7, |
55
|
|
|
'GRAB_JOB' => 9, |
56
|
|
|
'WORK_STATUS' => 12, |
57
|
|
|
'WORK_COMPLETE' => 13, |
58
|
|
|
'WORK_FAIL' => 14, |
59
|
|
|
'GET_STATUS' => 15, |
60
|
|
|
'ECHO_REQ' => 16, |
61
|
|
|
'SUBMIT_JOB_BG' => 18, |
62
|
|
|
'SUBMIT_JOB_HIGH' => 21, |
63
|
|
|
'SET_CLIENT_ID' => 22, |
64
|
|
|
'CAN_DO_TIMEOUT' => 23, |
65
|
|
|
'ALL_YOURS' => 24, |
66
|
|
|
'WORK_EXCEPTION' => 25, |
67
|
|
|
'OPTION_REQ' => 26, |
68
|
|
|
'OPTION_RES' => 27, |
69
|
|
|
'WORK_DATA' => 28, |
70
|
|
|
'WORK_WARNING' => 29, |
71
|
|
|
'GRAB_JOB_UNIQ' => 30, |
72
|
|
|
'SUBMIT_JOB_HIGH_BG' => 32, |
73
|
|
|
'SUBMIT_JOB_LOW' => 33, |
74
|
|
|
'SUBMIT_JOB_LOW_BG' => 34, |
75
|
|
|
'SUBMIT_JOB_SCHED' => 35, |
76
|
|
|
'SUBMIT_JOB_EPOCH' => 36, |
77
|
|
|
]; |
78
|
|
|
protected static $requestCommandListFlipped; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Response codes |
82
|
|
|
* |
83
|
|
|
* @var array |
84
|
|
|
*/ |
85
|
|
|
protected static $responseCommandList = [ |
86
|
|
|
'NOOP' => 6, |
87
|
|
|
'JOB_CREATED' => 8, |
88
|
|
|
'NO_JOB' => 10, |
89
|
|
|
'JOB_ASSIGN' => 11, |
90
|
|
|
'WORK_STATUS' => 12, |
91
|
|
|
'WORK_COMPLETE' => 13, |
92
|
|
|
'WORK_FAIL' => 14, |
93
|
|
|
'ECHO_RES' => 17, |
94
|
|
|
'ERROR' => 19, |
95
|
|
|
'STATUS_RES' => 20, |
96
|
|
|
'WORK_EXCEPTION' => 25, |
97
|
|
|
'OPTION_RES' => 27, |
98
|
|
|
'WORK_WARNING' => 29, |
99
|
|
|
'JOB_ASSIGN_UNIQ' => 31, |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
protected static $responseCommandListFlipped; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var mixed |
106
|
|
|
*/ |
107
|
|
|
public $response; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var string |
111
|
|
|
*/ |
112
|
|
|
public $responseType; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var string |
116
|
|
|
*/ |
117
|
|
|
public $responseCommand; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Called when new data received |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function onRead() |
125
|
|
|
{ |
126
|
|
|
if (($head = $this->lookExact(static::HEADER_LENGTH)) === false) { |
127
|
|
|
return; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
list($magic, $typeInt, $size) = unpack(static::HEADER_READ_FORMAT, $head); |
131
|
|
|
|
132
|
|
|
if ($this->getInputLength() < static::HEADER_LENGTH + $size) { |
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->drain(static::HEADER_LENGTH); |
137
|
|
|
$pct = $this->read($size); |
138
|
|
|
|
139
|
|
|
if ($magic === static::MAGIC_RESPONSE) { |
140
|
|
|
$this->responseType = static::$responseCommandListFlipped[$typeInt]; |
141
|
|
|
$this->response = explode(static::ARGS_DELIMITER, $pct); |
142
|
|
|
$this->onResponse->executeOne($this); |
143
|
|
|
$this->responseType = null; |
144
|
|
|
$this->responseCommand = null; |
145
|
|
|
$this->responseType = null; |
146
|
|
|
$this->checkFree(); |
147
|
|
|
return; |
148
|
|
|
} else { |
149
|
|
|
$type = static::$requestCommandListFlipped[$typeInt]; |
|
|
|
|
150
|
|
|
// @TODO |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Called when the connection is handshaked (at low-level), and peer is ready to recv. data |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
|
public function onReady() |
159
|
|
|
{ |
160
|
|
|
if (static::$requestCommandListFlipped === null) { |
161
|
|
|
static::$requestCommandListFlipped = array_flip(static::$requestCommandList); |
162
|
|
|
} |
163
|
|
|
if (static::$responseCommandListFlipped === null) { |
164
|
|
|
static::$responseCommandListFlipped = array_flip(static::$responseCommandList); |
165
|
|
|
} |
166
|
|
|
parent::onReady(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Function send ECHO |
172
|
|
|
* |
173
|
|
|
* @param $payload |
174
|
|
|
* @param callable|null $cb |
175
|
|
|
*/ |
176
|
|
|
public function sendEcho($payload, $cb = null) |
177
|
|
|
{ |
178
|
|
|
$this->sendCommand('ECHO_REQ', $payload, $cb); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Send a command |
183
|
|
|
* |
184
|
|
|
* @param $commandName |
185
|
|
|
* @param $payload |
186
|
|
|
* @param callable $cb = null |
|
|
|
|
187
|
|
|
*/ |
188
|
|
|
public function sendCommand($commandName, $payload, $cb = null) |
189
|
|
|
{ |
190
|
|
|
|
191
|
|
|
$pct = implode( |
192
|
|
|
static::ARGS_DELIMITER, |
193
|
|
|
array_map(function ($item) { |
194
|
|
|
return !is_scalar($item) ? serialize($item) : $item; |
195
|
|
|
}, (array)$payload) |
196
|
|
|
); |
197
|
|
|
$this->onResponse->push($cb); |
|
|
|
|
198
|
|
|
$this->write( |
199
|
|
|
pack( |
200
|
|
|
static::HEADER_WRITE_FORMAT, |
201
|
|
|
static::MAGIC_REQUEST, |
202
|
|
|
$this->requestCommandList[$commandName], |
203
|
|
|
mb_orig_strlen($pct) |
204
|
|
|
) |
205
|
|
|
); |
206
|
|
|
$this->write($pct); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Function run task and wait result in callback |
211
|
|
|
* |
212
|
|
|
* @param $params |
213
|
|
|
* @param callable $cb = null |
|
|
|
|
214
|
|
|
* @param boolean $unique |
|
|
|
|
215
|
|
|
*/ |
216
|
|
|
public function submitJob($params, $cb = null) |
217
|
|
|
{ |
218
|
|
|
$closure = function () use (&$params, $cb) { |
219
|
|
|
$this->sendCommand( |
220
|
|
|
'SUBMIT_JOB' |
221
|
|
|
. (isset($params['pri']) ? '_ ' . strtoupper($params['pri']) : '') |
222
|
|
|
. (isset($params['bg']) && $params['bg'] ? '_BG' : ''), |
223
|
|
|
[$params['function'], $params['unique'], $params['payload']], |
224
|
|
|
$cb |
225
|
|
|
); |
226
|
|
|
}; |
227
|
|
|
if (isset($params['unique'])) { |
228
|
|
|
$closure(); |
229
|
|
|
} else { |
230
|
|
|
Crypt::randomString( |
231
|
|
|
10, |
232
|
|
|
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
233
|
|
|
function ($random) use ($closure) { |
234
|
|
|
$params['unique'] = $random; |
|
|
|
|
235
|
|
|
$closure(); |
236
|
|
|
} |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get job status |
243
|
|
|
* |
244
|
|
|
* @param mixed $jobHandle Job handle that was given in JOB_CREATED packet. |
245
|
|
|
* @param callable $cb = null |
|
|
|
|
246
|
|
|
* |
247
|
|
|
*/ |
248
|
|
|
public function getStatus($jobHandle, $cb = null) |
249
|
|
|
{ |
250
|
|
|
$this->sendCommand('GET_STATUS', [$jobHandle], $cb); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Function set settings for current connection |
255
|
|
|
* Available settings |
256
|
|
|
* 'exceptions' - Forward WORK_EXCEPTION packets to the client. |
257
|
|
|
* |
258
|
|
|
* @url http://gearman.org/protocol/ |
259
|
|
|
* |
260
|
|
|
* |
261
|
|
|
* @param int $optionName |
262
|
|
|
* @param callable $cb = null |
|
|
|
|
263
|
|
|
*/ |
264
|
|
|
public function setConnectionOption($optionName, $cb = null) |
265
|
|
|
{ |
266
|
|
|
$this->sendCommand('OPTION_RES', [$optionName], $cb); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.