/
swooleserver.php
275 lines (254 loc) · 9.64 KB
/
swooleserver.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
<?php
/**
* Created by PhpStorm.
* User: xiemin
* Date: 2015/11/10
* Time: 11:45
* Description: 框架的核心封装类
*/
class SwooleServer
{
private $_serv;
private $_swoole_cfg = array();
public function __construct($cfg)
{
$this->_swoole_cfg = $cfg;
}
public function __destruct()
{
}
private function _getSwooleCoreCfg()
{
if (!$this->_swoole_cfg) {
throw new Exception('_swoole_cfg is invaild');
}
$core_cfg = array();
/* 默认会以cpu数为准 */
if (isset($this->_swoole_cfg['swoole']['worker_num'])) {
$core_cfg['worker_num'] = $this->_swoole_cfg['swoole']['worker_num'];
}
/* 下面强制设置 */
$core_cfg['task_worker_num'] = isset($this->_swoole_cfg['swoole']['task_worker_num']) ? $this->_swoole_cfg['swoole']['task_worker_num'] : 8;
$core_cfg['max_request'] = isset($this->_swoole_cfg['swoole']['max_request']) ? $this->_swoole_cfg['swoole']['max_request'] : 100000;
/* log_file和debug绑定设置 */
$core_cfg['daemonize'] = isset($this->_swoole_cfg['swoole']['debug']) ? !$this->_swoole_cfg['swoole']['debug'] : false;
if ($core_cfg['daemonize']) {
if (isset($this->_swoole_cfg['swoole']['log_file'])) {
if (!file_exists($this->_swoole_cfg['swoole']['log_file'])) {
throw new Exception('log_file not exsits');
}
$core_cfg['log_file'] = $this->_swoole_cfg['swoole']['log_file'];
} else {
$core_cfg['log_file'] = "/dev/null";
}
}
return $core_cfg;
}
public function onInit()
{
$host = isset($this->_swoole_cfg['swoole']['swoole_host']) ? $this->_swoole_cfg['swoole']['swoole_host'] : '0.0.0.0';
$port = isset($this->_swoole_cfg['swoole']['swoole_port']) ? $this->_swoole_cfg['swoole']['swoole_port'] : 80;
$serv = new swoole_http_server ($host, $port);
/**
* 这里对swoole进程进行设置
*/
$serv->set($this->_getSwooleCoreCfg());
$serv->on('WorkerStart', array(
$this,
'onWorkerStart'
));
$serv->on('WorkerStop', array(
$this,
'onWorkerStop'
));
$serv->on('Request', array(
$this,
'onRequest'
));
$serv->on('Task', array(
$this,
'onTask'
));
$serv->on('Finish', array(
$this,
'onFinish'
));
$this->_serv = $serv;
}
/**
* @throws Exception
*/
public function onStart()
{
if ($this->_serv) {
$this->_serv->start();
} else {
throw new Exception('swoole instance is null');
}
}
public function onExit()
{
// TODO
}
/**
* 启动进程的时候调用此函数
* @param unknown $server
* @param unknown $worker_id
*/
public function onWorkerStart($server, $worker_id)
{
global $argv;
if ($worker_id >= $server->setting['worker_num']) {
swoole_set_process_name("php {$argv[0]} {$this->_swoole_cfg['swoole']['progname']} task worker");
} else {
swoole_set_process_name("php {$argv[0]} {$this->_swoole_cfg['swoole']['progname']} event worker");
}
}
/**
* @param unknown $server
* @param unknown $worker_id
*/
public function onWorkerStop($server, $worker_id)
{
// TODO
}
/**
* @param $serv
* @param $task_id
* @param $from_id
* @param $data
*/
public function onTask($serv, $task_id, $from_id, $data)
{
try {
Logger::logInfo("pid: [" . posix_getpid() . "] task_id[" . $task_id . "] from_id: [" . $from_id . "] start");
if ($data && is_array($data)) {
if (isset($data['c_arr']) && isset($data['args'])) {
$c_arr = $data['c_arr'];
$args = $data['args'];
$c = $this->_loadClass($c_arr);
$c->workTaskProcess($args);
}
}
} catch (Exception $e) {
Logger::logWarn("throw error message: [" . $e->getMessage() . "] error code : [" . $e->getCode() . "]");
}
$this->_serv->finish("finished");
}
public function onFinish($serv, $task_id, $data)
{
Logger::logInfo("tasking_num: [" . $serv->stats()['tasking_num'] . "] pid: [" . posix_getpid() . "] task_id: [" . $task_id . "] message: [" . $data . "]");
}
/**
* @param $request
* @param $response
* @throws Exception
*/
public function onRequest($request, $response)
{
/* 下面操作性能出现了比较大的下降 */
try {
$msg = "";
$code = 0;
$query = array();
if (!isset($request->server['path_info'])) {
throw new Exception('path_info of request->server is invaild');
}
$c_arr = $this->getCname($request->server['path_info']);
if (!isset($request->server['query_string'])) {
$query = array();
} else {
parse_str($request->server['query_string'], $query);
}
if ($this->_hasRegister($c_arr['cname'], 'http')) {
$c = $this->_loadClass($c_arr);
$ret = $c->httpTaskProcess($request);
if ($this->_swoole_cfg['swoole']['gzip'] === true) {
$response->gzip(1);
$response->header('Content-Encoding', 'gzip');
}
/* default */
$response->header('Content-Type', 'application/json');
$response->header('Content-Type', 'text/html; charset=utf-8');
/* 这里msg的类型发生了变化, 不好的编程风格 */
is_string($ret) ? $msg .= $ret : $msg = $ret;
$code = 1;
} elseif ($this->_hasRegister($c_arr['cname'], 'process')) {
$this->_serv->task(array('c_arr' => $c_arr, 'args' => array('query' => $query)));
$msg .= " pid: [" . posix_getpid() . "] finished";
$code = 1;
} else {
$msg = $c_arr['cname'] . " not be register";
$code = 0;
}
$response->end(json_encode(array('code' => $code, 'msg' => $msg), JSON_UNESCAPED_UNICODE));
} catch (Exception $e) {
$msg = "throw error message: [" . $e->getMessage() . "] error code : [" . $e->getCode() . "]\n";
Logger::logWarn($msg . "" . $e->getTraceAsString());
$response->end(json_encode(array('code' => 0, 'msg' => $msg)));
}
}
/**
* @param $class
* @return mixed
* @throws Exception
*/
private function _loadClass($c_arr)
{
//加载类文件
if ($c_arr && is_array($c_arr) && isset($c_arr['path_info']) && isset($c_arr['cname'])) {
$cname = $c_arr['cname'];
$path_info = $c_arr['path_info'];
if (!class_exists($cname, false)) {
require_once($this->_swoole_cfg['workpath']['LOGIC'] . 'logic.interface.php');
if (file_exists($this->_swoole_cfg['workpath']['LOGIC'] . strtolower($cname) . '.class.php')) {
require_once($this->_swoole_cfg['workpath']['LOGIC'] . strtolower($cname) . '.class.php');
} else if ($this->_swoole_cfg['workpath']['LOGIC'] . $path_info . strtolower($cname) . '.class.php') {
//这里支持单个业务放入文件夹中
require_once($this->_swoole_cfg['workpath']['LOGIC'] . $path_info . strtolower($cname) . '.class.php');
} else {
$info = strtolower($cname) . ' class not exsits';
throw new Exception($info);
}
}
} else {
throw new Exception('cname or path_info is not set');
}
//加载配置文件,目前只支持单一配置文件
if (!file_exists($this->_swoole_cfg['workpath']['CONFIG'] . "" . strtolower($cname) . '.config.php')) {
$info = $this->_swoole_cfg['workpath']['CONFIG'] . strtolower($cname) . '.config.php not exsit';
throw new Exception($info);
}
$bussness_cfg = require($this->_swoole_cfg['workpath']['CONFIG'] . strtolower($cname) . '.config.php');
return new $cname($bussness_cfg);
}
/**
* @param $class
* @param $type
*/
private function _hasRegister($class, $type)
{
$flag = false;
if ($this->_swoole_cfg['business'] && is_array($this->_swoole_cfg['business'])) {
foreach ($this->_swoole_cfg['business'] as $name => $prop) {
if ($name == $class && is_array($prop) && in_array($type, $prop['type']) && $prop['online'] == true) {
$flag = true;
break;
}
}
}
return $flag;
}
/**
* @param $path_info
* @return array
*/
public function getCname($path_info)
{
preg_match('/(.*\/)(.*)/', $path_info, $match);
if (is_array($match) && count($match) == 3) {
return array('path_info' => $match[1], 'cname' => $match[2]);
}
throw new Exception('pathinfo is invaild');
}
}