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