Gearman.class.php
1.3 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
<?php
/**
* 任务调度使用类
*
* @example
* <pre>
* $c = Util_Gearman::client();
* $w = Util_Gearman::worker();
* </pre>
*
* @name Util_Gearman
* @package Util
* @copyright yoho.inc
* @version 1.1 (2012-07-31 10:39:21)
* @author fei.hong <hf@yoho.cn>
* @since 1.0
*/
class Util_Gearman {
/**
* @var array (Util_Gearman_Client | Util_Gearman_Worker )
*/
private static $gearman = array ();
/**
* 工厂: 生成任务调度的操作对象
*
* @param String $router ("Client" | "Worker")
* @throws Util_Exception
* @return Util_Gearman_Client | Util_Gearman_Worker
*/
public static function factory($router = 'Client') {
$gearClass = 'Util_Gearman_' . ucfirst ( $router );
if (false === class_exists ( $gearClass )) {
throw new Util_Gearman_Exception('没有' . $gearClass . '类文件.' . __CLASS__);
}
if (! isset ( self::$gearman[$gearClass] )) {
self::$gearman [$gearClass] = new $gearClass ();
}
return self::$gearman [$gearClass];
}
/**
* 客户端使用类
*
* @return Util_Gearman_Client
*/
public static function client() {
return Util_Gearman::factory ( 'Client' );
}
/**
* 工作者使用类
*
* @return Util_Gearman_Worker
*/
public static function worker() {
return Util_Gearman::factory ( 'Worker' );
}
}