Abstract.class.php
3.05 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
<?php
abstract class Util_Gearman_Abstract {
/**
* 超时时间
* @var Integer
*/
protected $timeout = 0;
/**
* 服务器
*
* @var Array
*/
protected $dsn = array();
/**
* gearman 对象
*
* @var Object
*/
protected $gearmanObj;
/**
* 初始化
*
* @param String $driver
*/
protected function __construct($driver = 'Client') {
$Gearmandriver = 'Gearman' . ucfirst($driver);
$this->gearmanObj = new $Gearmandriver();
if (!isset($this->dsn) || empty($this->dsn)) {
$this->dsn = Util_Gearman_Dsn::getServers();
}
$this->addServers($this->dsn);
}
/**
* 添加多个服务器
*
* @param array $servers
* @return Util_Gearman_Abstract
*/
public function addServers(array $servers) {
$serversStr = implode(',', $servers);
$status = $this->gearmanObj->addServers($serversStr);
if ($status == false) {
throw new Util_Gearman_Exception('init server error : ' . $this->gearmanObj->error() . ' server List: ' . $serversStr);
}
return $this;
}
/**
* 添加一个服务器
*
* @param String $host
* @param Integer $port
* @return Util_Gearman_Abstract
*/
public function addServer($host, $port = 4730) {
$returns = $this->gearmanObj->addServer($host, $port);
if ($returns == false) {
throw new Util_Gearman_Exception(' add server error : ' . $host . ' error mes:' . $this->gearmanObj->error());
}
return $this;
}
/**
* 获取gearman 对象
*
* @return Util_Gearman_Abstract
*/
public function gearman() {
return $this->gearmanObj;
}
/**
* 设置超时时间
*
* @param Integer $time
* @return Util_Gearman_Abstract
*/
public function setTimeout($time) {
$time = intval($time);
if ($time > 0) {
$this->timeout = $time;
$this->gearmanObj->setTimeout($this->timeout);
}
return $this;
}
/**
* 获取超时时间
*
* @return Integer
*/
public function getTimeout() {
return $this->gearmanObj->timeout();
}
/**
* 设置更多参数
* @param Integer $options
* @return Util_Gearman_Abstract
*/
public function setOptions($options) {
if (!$options) {
$this->gearmanObj->setOptions($options);
}
return $this;
}
/**
* 添加更多参数
*
* @param Integer $options
* @return Util_Gearman_Abstract
*/
public function addOptions($options) {
if (!$options) {
$this->gearmanObj->addOptions($options);
}
return $this;
}
/**
* 复制 Gearman 对象
*
* @return Object
*/
public function clones() {
return $this->gearmanObj->clone();
}
/**
* 获取返回的错误信息
*
* @return String
*/
public function error() {
return $this->gearmanObj->error();
}
/**
* 获得errno
*
* @return Mixed
*/
public function getErrno() {
return $this->gearmanObj->getErrno();
}
/**
* 测试服务器相应
*
* @param String $workload 参数|数据
* @return boll
*/
public function test($workload) {
return $this->gearmanObj->echo($workload);
}
/**
* 返回最后执行码
*
* @return Mixed
*/
public function returnCode() {
return $this->gearmanObj->returnCode();
}
}