-
Notifications
You must be signed in to change notification settings - Fork 32
/
Manager.php
executable file
·320 lines (289 loc) · 8.83 KB
/
Manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* Interface for Danga's Gearman job scheduling system
*
* PHP version 5.1.0+
*
* LICENSE: This source file is subject to the New BSD license that is
* available through the world-wide-web at the following URI:
* http://www.opensource.org/licenses/bsd-license.php. If you did not receive
* a copy of the New BSD License and are unable to obtain it through the web,
* please send a note to license@php.net so we can mail you a copy immediately.
*
* @category Net
* @package Net_Gearman
* @author Joe Stump <joe@joestump.net>
* @copyright 2007-2008 Digg.com, Inc.
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id$
* @link http://pear.php.net/package/Net_Gearman
* @link http://www.danga.com/gearman/
*/
require_once 'Net/Gearman/Connection.php';
/**
* A client for managing Gearmand servers
*
* This class implements the administrative text protocol used by Gearman to do
* a number of administrative tasks such as collecting stats on workers, the
* queue, shutting down the server, version, etc.
*
* @category Net
* @package Net_Gearman
* @author Joe Stump <joe@joestump.net>
* @copyright 2007-2008 Digg.com, Inc.
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @link http://www.danga.com/gearman/
*/
class Net_Gearman_Manager
{
/**
* Connection resource
*
* @var resource $conn Connection to Gearman server
* @see Net_Gearman_Manager::sendCommand()
* @see Net_Gearman_Manager::recvCommand()
*/
protected $conn = null;
/**
* The server is shutdown
*
* We obviously can't send more commands to a server after it's been shut
* down. This is set to true in Net_Gearman_Manager::shutdown() and then
* checked in Net_Gearman_Manager::sendCommand().
*
* @var boolean $shutdown
*/
protected $shutdown = false;
/**
* Constructor
*
* @param string $server Host and port (e.g. 'localhost:7003')
* @param integer $timeout Connection timeout
*
* @throws Net_Gearman_Exception
* @see Net_Gearman_Manager::$conn
*/
public function __construct($server, $timeout = 5)
{
if (strpos($server, ':')) {
list($host, $port) = explode(':', $server);
} else {
$host = $server;
$port = 7003;
}
$errCode = 0;
$errMsg = '';
$this->conn = @fsockopen($host, $port, $errCode, $errMsg, $timeout);
if ($this->conn === false) {
throw new Net_Gearman_Exception(
'Could not connect to ' . $host . ':' . $port
);
}
}
/**
* Get the version of Gearman running
*
* @return string
* @see Net_Gearman_Manager::sendCommand()
* @see Net_Gearman_Manager::checkForError()
*/
public function version()
{
$this->sendCommand('version');
$res = fgets($this->conn, 4096);
$this->checkForError($res);
return trim($res);
}
/**
* Shut down Gearman
*
* @param boolean $graceful Whether it should be a graceful shutdown
*
* @return boolean
* @see Net_Gearman_Manager::sendCommand()
* @see Net_Gearman_Manager::checkForError()
* @see Net_Gearman_Manager::$shutdown
*/
public function shutdown($graceful = false)
{
$cmd = ($graceful) ? 'shutdown graceful' : 'shutdown';
$this->sendCommand($cmd);
$res = fgets($this->conn, 4096);
$this->checkForError($res);
$this->shutdown = (trim($res) == 'OK');
return $this->shutdown;
}
/**
* Get worker status and info
*
* Returns the file descriptor, IP address, client ID and the abilities
* that the worker has announced.
*
* @return array A list of workers connected to the server
* @throws Net_Gearman_Exception
*/
public function workers()
{
$this->sendCommand('workers');
$res = $this->recvCommand();
$workers = array();
$tmp = explode("\n", $res);
foreach ($tmp as $t) {
if (!Net_Gearman_Connection::stringLength($t)) {
continue;
}
list($info, $abilities) = explode(" : ", $t);
list($fd, $ip, $id) = explode(' ', $info);
$abilities = trim($abilities);
$workers[] = array(
'fd' => $fd,
'ip' => $ip,
'id' => $id,
'abilities' => empty($abilities) ? array() : explode(' ', $abilities)
);
}
return $workers;
}
/**
* Set maximum queue size for a function
*
* For a given function of job, the maximum queue size is adjusted to be
* max_queue_size jobs long. A negative value indicates unlimited queue
* size.
*
* If the max_queue_size value is not supplied then it is unset (and the
* default maximum queue size will apply to this function).
*
* @param string $function Name of function to set queue size for
* @param integer $size New size of queue
*
* @return boolean
* @throws Net_Gearman_Exception
*/
public function setMaxQueueSize($function, $size)
{
if (!is_numeric($size)) {
throw new Net_Gearman_Exception('Queue size must be numeric');
}
if (preg_match('/[^a-z0-9_]/i', $function)) {
throw new Net_Gearman_Exception('Invalid function name');
}
$this->sendCommand('maxqueue ' . $function . ' ' . $size);
$res = fgets($this->conn, 4096);
$this->checkForError($res);
return (trim($res) == 'OK');
}
/**
* Get queue/worker status by function
*
* This function queries for queue status. The array returned is keyed by
* the function (job) name and has how many jobs are in the queue, how
* many jobs are running and how many workers are capable of performing
* that job.
*
* @return array An array keyed by function name
* @throws Net_Gearman_Exception
*/
public function status()
{
$this->sendCommand('status');
$res = $this->recvCommand();
$status = array();
$tmp = explode("\n", $res);
foreach ($tmp as $t) {
if (!Net_Gearman_Connection::stringLength($t)) {
continue;
}
list($func, $inQueue, $jobsRunning, $capable) = explode("\t", $t);
$status[$func] = array(
'in_queue' => $inQueue,
'jobs_running' => $jobsRunning,
'capable_workers' => $capable
);
}
return $status;
}
/**
* Send a command
*
* @param string $cmd The command to send
*
* @return void
* @throws Net_Gearman_Exception
*/
protected function sendCommand($cmd)
{
if ($this->shutdown) {
throw new Net_Gearman_Exception('This server has been shut down');
}
fwrite($this->conn,
$cmd . "\r\n",
Net_Gearman_Connection::stringLength($cmd . "\r\n"));
}
/**
* Receive a response
*
* For most commands Gearman returns a bunch of lines and ends the
* transmission of data with a single line of ".\n". This command reads
* in everything until ".\n". If the command being sent is NOT ended with
* ".\n" DO NOT use this command.
*
* @throws Net_Gearman_Exception
* @return string
*/
protected function recvCommand()
{
$ret = '';
while (true) {
$data = fgets($this->conn, 4096);
$this->checkForError($data);
if ($data == ".\n") {
break;
}
$ret .= $data;
}
return $ret;
}
/**
* Check for an error
*
* Gearman returns errors in the format of 'ERR code_here Message+here'.
* This method checks returned values from the server for this error format
* and will throw the appropriate exception.
*
* @param string $data The returned data to check for an error
*
* @return void
* @throws Net_Gearman_Exception
*/
protected function checkForError($data)
{
$data = trim($data);
if (preg_match('/^ERR/', $data)) {
list(, $code, $msg) = explode(' ', $data);
throw new Net_Gearman_Exception($msg, urldecode($code));
}
}
/**
* Disconnect from server
*
* @return void
* @see Net_Gearman_Manager::$conn
*/
public function disconnect()
{
if (is_resource($this->conn)) {
fclose($this->conn);
}
}
/**
* Destructor
*
* @return void
*/
public function __destruct()
{
$this->disconnect();
}
}
?>