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