Client.class.php
10.8 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php
class Util_Gearman_Client extends Util_Gearman_Abstract {
/**
* 临时状态
*
* @var Mixed
*/
private $_status = null;
/**
* 运行结果
*
* @var String
*/
private $_handle = '';
/**
* 构造函数
*/
public function __construct() {
parent::__construct('client');
}
/**
* 源 do
* 运行一项唯一任务并返回结果
*
*
* @param String $function_name
* @param String $workload
* @param String $unique
* @return String
*/
public function d($function_name, $workload, $unique = '') {
$this->_handle = $this->gearmanObj->do($function_name, $workload, $unique);
return $this->_handle;
}
/**
* 源 doBackground
*
* 后端运行一项唯一任务
*
*
* @param string $function_name
* @param string $workload
* @param string $unique
* @return string
*/
public function dB($function_name, $workload, $unique = '') {
$this->_handle = $this->gearmanObj->doBackground($function_name, $workload, $unique);
return $this->_handle;
}
/**
* 源 doHigh
*
* 运行一项优先级高的唯一任务
*
* @param String $function_name
* @param String $workload
* @param String $unique
* @return String
*/
public function dH($function_name, $workload, $unique = '') {
$this->_handle = $this->gearmanObj->doHigh($function_name, $workload, $unique);
return $this->_handle;
}
/**
* 源 doHighBackground
*
* 后端运行一项优先级高的唯一任务
*
* @param String $function_name
* @param String $workload
* @param String $unique
* @return String
*/
public function dHB($function_name, $workload, $unique = '') {
$this->_handle = $this->gearmanObj->doHighBackground($function_name, $workload, $unique);
return $this->_handle;
}
/**
* 源 doLow
*
* 运行一项优先级低的唯一任务
*
* @param String $function_name
* @param String $workload
* @param String $unique
* @return String
*/
public function dL($function_name, $workload, $unique = '') {
$this->_handle = $this->gearmanObj->doLow($function_name, $workload, $unique);
return $this->_handle;
}
/**
* 源 doLowBackground
*
* 后端运行一项优先级低的唯一任务
*
* @param String $function_name
* @param String $workload
* @param String $unique
* @return String
*/
public function dLB($function_name, $workload, $unique = '') {
$this->_handle = $this->gearmanObj->doLowBackground($function_name, $workload, $unique);
return $this->_handle;
}
/**
* 源 doJobHandle
*
* 返回Do执行任务信息
*
* @return Mixed
*/
public function dJH() {
$this->_handle = $this->gearmanObj->doJobHandle();
return $this->_handle;
}
/**
* 源 doStatus
*
* 返回连接状态
*
* @return Array
*/
public function dS() {
return $this->gearmanObj->doStatus();
}
/**
* 源 addTask
*
* 添加一个任务
*
* @param String $function_name
* @param String $workload
* @param Mixed $context 带入
* @param String $unique 唯一id
* @return Util_Gearman_Client
*/
public function aT($function_name, $workload, &$context = null, $unique = '') {
$this->_status = $this->gearmanObj->addTask($function_name, $workload, $context, $unique);
return $this;
}
/**
* 添加多个任务
*
* @param array $task
* @return Util_Gearman_Client
*/
public function aTES(array $task) {
foreach ($task as $k => $v) {
if (count($v) == 4) {
list($function_name, $workload, $context, $unique) = $v;
$this->gearmanObj->addTask($function_name, $workload, $context, $unique);
}
}
return $this;
}
/**
* 源 addTaskBackground
*
* 后端添加一个任务后端
*
* @param String $function_name
* @param String $workload
* @param String $context
* @param String $unique
* @return Util_Gearman_Client
*/
public function aTB($function_name, $workload, &$context = null, $unique = '') {
$this->_status = $this->gearmanObj->addTaskBackground($function_name, $workload, $context, $unique);
return $this;
}
/**
* 源 addTaskHigh
*
* 添加一个优先级高任务
*
* @param String $function_name
* @param String $workload
* @param String $context
* @param String $unique
* @return Util_Gearman_Client
*/
public function aTH($function_name, $workload, &$context = null, $unique = '') {
$this->_status = $this->gearmanObj->addTaskHigh($function_name, $workload, $context, $unique);
return $this;
}
/**
* 源 addTaskHighBackground
*
* 后端添加一个优先级高任务
*
* @param String $function_name
* @param String $workload
* @param String $context
* @param String $unique
* @return Util_Gearman_Client
*/
public function aTHB($function_name, $workload, &$context = null, $unique = '') {
$this->_status = $this->gearmanObj->addTaskHighBackground($function_name, $workload, $context, $unique);
return $this;
}
/**
* 源 addTaskLow
*
* 添加一个优先级低任务
*
* @param String $function_name
* @param String $workload
* @param String $context
* @param String $unique
* @return Util_Gearman_Client
*/
public function aTL($function_name, $workload, &$context = null, $unique = '') {
$this->_status = $this->gearmanObj->addTaskLow($function_name, $workload, $context, $unique);
return $this;
}
/**
* 源 addTaskLowBackground
*
* 后端添加一个优先级低任务
*
* @param String $function_name
* @param String $workload
* @param String $context
* @param String $unique
* @return Util_Gearman_Client
*/
public function aTLB($function_name, $workload, &$context = null, $unique = '') {
$this->_status = $this->gearmanObj->addTaskLowBackground($function_name, $workload, $context, $unique);
return $this;
}
/**
* 源 addTaskStatus
*
* 添加一个任务的状态
*
* @param String $job_handle
* @param String $context
* @return Util_Gearman_Client
*/
public function aTS($job_handle, &$context) {
$this->_status = $this->gearmanObj->addTaskStatus($job_handle, $context);
return $this;
}
/**
* 源 clearCallbacks
*
* 关闭所有回调函数
*
* @return bool
*/
public function cC() {
return $this->gearmanObj->clearCallbacks();
}
/**
* 源 setCompleteCallback
*
* 设置回调
*
* @param Util_Gearman_Client
*/
public function sCC($callback) {
if (empty($callback)) {
throw new Util_Gearman_Exception(__CLASS__ . ' callback null ');
}
$this->_status = $this->gearmanObj->setCompleteCallback($callback);
return $this;
}
/**
* 源 setStatusCallback
*
* 设置状态回调
*
* @param Util_Gearman_Client
*/
public function sSC($callback) {
if (empty($callback)) {
throw new Util_Gearman_Exception(__CLASS__ . ' callback null ');
}
$this->_status = $this->gearmanObj->setStatusCallback($callback);
return $this;
}
/**
* 源 runTasks
*
* 运行任务列表(使用在所有add*)
*
* @return bool
*/
public function rT() {
return $this->gearmanObj->runTasks();
}
/**
* 返回临时状态
*
* @return Mixed
*/
public function s() {
return $this->_status;
}
/**
* 获取环境参数
*
* @return Mixed
*/
public function context() {
return $this->gearmanObj->context();
}
/**
* 获取运行状态
*
* @param gearmanObj $job_handle
* @return Array
*/
public function jobStatus($job_handle) {
if (empty($job_handle)) {
$job_handle = $this->_handle;
}
$done = false;
$running_data = array();
do {
sleep(3);
$stat = $this->gearmanObj->jobStatus($job_handle);
if (!$stat[0]) // the job is known so it is not done
$done = true;
$running_data[] = "Running: " . ($stat[1] ? "true" : "false") . ", numerator: " . $stat[2] . ", denomintor: " . $stat[3] . "\n";
} while (!$done);
return $running_data;
}
/**
* 移除选项
*
* @param Integer $options
* @return Util_Gearman_Client
*/
public function removeOptions($options) {
$this->_status = $this->gearmanObj->removeOptions($options);
return $this;
}
/**
* 注销 & 重置
*
*/
private function _resetStatus() {
unset($this->_status);
unset($this->_handle);
}
/**
* 设置一个客户端 认为错误时调用的函数
*
* @param Strign $callback
* @return Util_Gearman_Client
*/
public function setClientCallback($callback) {
if (empty($callback)) {
throw new Util_Gearman_Exception(__CLASS__ . ' callback null ');
}
$this->_status = $this->gearmanObj->setClientCallback($callback);
return $this;
}
/**
* 设置一个客户端认为排队时回调函数
*
* @param Strign $callback
* @return Util_Gearman_Client
*/
public function setCreatedCallback($callback) {
if (empty($callback)) {
throw new Util_Gearman_Exception(__CLASS__ . ' callback null ');
}
$this->_status = $this->gearmanObj->setCreatedCallback($callback);
return $this;
}
/**
* 设置数据
*
* @param String $data
* @return Util_Gearman_Client
*/
public function setData($data) {
if (empty($data)) {
return $this;
}
$this->_status = $this->gearmanObj->setData($data);
return $this;
}
/**
* 设置当前任务数据库回调函数
*
* @param String $callback
* @return Util_Gearman_Client
*/
public function setDataCallback($callback) {
if (empty($callback)) {
throw new Util_Gearman_Exception(__CLASS__ . ' callback null ');
}
$this->_status = $this->gearmanObj->setDataCallback($callback);
return $this;
}
/**
* 源 setExceptionCallback
*
* 设置异常回调函数
*
* @param String $callback
* @return Util_Gearman_Client
*/
public function sEC($callback) {
if (empty($callback)) {
throw new Util_Gearman_Exception(__CLASS__ . ' callback null ');
}
$this->_status = $this->gearmanObj->setExceptionCallback($callback);
return $this;
}
/**
* 源 setFailCallback
*
* 设置服务器故障回调函数
*
* @param String $callback
* @return Util_Gearman_Client
*/
public function sFC($callback) {
if (empty($callback)) {
throw new Util_Gearman_Exception(__CLASS__ . ' callback null ');
}
$this->_status = $this->gearmanObj->setFailCallback($callback);
return $this;
}
/**
* 源 setWarningCallback
*
* 设置任务警告回调函数
*
* @param String $callback
* @return Util_Gearman_Client
*/
public function setWarningCallback($callback) {
if (empty($callback)) {
throw new Util_Gearman_Exception(__CLASS__ . ' callback null ');
}
$this->_status = $this->gearmanObj->setWarningCallback($callback);
return $this;
}
/**
* 源 setWorkloadCallback
*
* 设置任务完成后返回值时回调函数
*
* @param String $callback
* @return Util_Gearman_Client
*/
public function setWorkloadCallback($callback) {
if (empty($callback)) {
throw new Util_Gearman_Exception(__CLASS__ . ' callback null ');
}
$this->_status = $this->gearmanObj->setWorkloadCallback($callback);
return $this;
}
/**
* 析构函数
*/
public function __destruct() {
$this->cC();
}
}