-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver
301 lines (277 loc) · 9.45 KB
/
server
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
#!/usr/bin/env php
<?php
/**
* Created by PhpStorm.
* User: gongshaoyu
* Date: 2018/4/13
* Time: 下午3:18
*/
require_once 'Core/Core.php';
/** @var \Core\Core $server */
$server = \Core\Core::getInstance()->frameWorkInitialize();
function commandParser()
{
global $argv;
$command = '';
$options = [];
if (isset($argv[1])) {
$command = $argv[1];
}
foreach ($argv as $item) {
if (substr($item, 0, 2) === '--') {
$temp = trim($item, "--");
$temp = explode("-", $temp);
$key = array_shift($temp);
$options[$key] = array_shift($temp) ?: '';
}
}
return array(
"command" => $command,
"options" => $options
);
}
function commandHandler()
{
$command = commandParser();
switch ($command['command']) {
case "start":
startServer($command['options']);
break;
case 'stop':
stopServer($command['options']);
break;
case 'reload':
reloadServer($command['options']);
break;
case 'update':
echo "start download\n";
exec('curl -sS -o master.tar.gz https://codeload.github.com/easy-swoole/easyswoole/tar.gz/master');
echo "download success\n";
exec('tar -xzvf master.tar.gz');
if (isset($command['options']['ide'])) {
exec('rm -rf ./ide_helper');
exec('mv ./easyswoole-master/ide_helper ./');
} else {
exec('rm -rf ./Core');
exec('rm -rf ./server');
exec('mv ./easyswoole-master/src/Core ./');
exec('mv ./easyswoole-master/src/server ./');
}
exec('rm -rf ./easyswoole-master master.tar.gz');
echo "update success\n";
break;
case 'version':
echo \Core\Component\Di::getInstance()->get(\Core\Component\SysConst::VERSION);
echo "\n";
break;
case 'help':
default:
help($command['options']);
}
}
function startServer($options)
{
opCacheClear();
global $server;
/** @var \Conf\Config $conf */
$conf = \Conf\Config::getInstance();
echo 'server name ' . $conf->getConf('SERVER.SERVER_NAME') . "\n";
if (isset($options['ip'])) {
$conf->setConf("SERVER.LISTEN", $options['ip']);
}
echo 'listen address ' . $conf->getConf('SERVER.LISTEN') . "\n";
if (!empty($options['p'])) {
$conf->setConf("SERVER.PORT", $options['p']);
}
echo 'listen port ' . $conf->getConf('SERVER.PORT') . "\n";
if (!empty($options['pid'])) {
$pidFile = $options['pid'];
\Conf\Config::getInstance()->setConf("SERVER.CONFIG.pid_file", $pidFile);
}
if (isset($options['workerNum'])) {
$conf->setConf("SERVER.CONFIG.worker_num", $options['workerNum']);
}
echo 'worker num ' . $conf->getConf('SERVER.CONFIG.worker_num') . "\n";
if (isset($options['taskWorkerNum'])) {
$conf->setConf("SERVER.CONFIG.task_worker_num", $options['taskWorkerNum']);
}
echo 'task worker num ' . $conf->getConf('SERVER.CONFIG.task_worker_num') . "\n";
if (isset($options['user'])) {
$conf->setConf("SERVER.CONFIG.user", $options['user']);
}
echo 'user ' . $conf->getConf('SERVER.CONFIG.user') . "\n";
if (isset($options['group'])) {
$conf->setConf("SERVER.CONFIG.group", $options['group']);
}
echo 'user group ' . $conf->getConf('SERVER.CONFIG.group') . "\n";
if (isset($options['cpuAffinity'])) {
$conf->setConf("SERVER.CONFIG.open_cpu_affinity", true);
}
$label = 'false';
if (isset($options['d'])) {
$conf->setConf("SERVER.CONFIG.daemonize", true);
$label = 'true';
} else {
\Conf\Config::getInstance()->setConf("SERVER.CONFIG.pid_file", null);
}
echo "daemonize {$label} \n";
$label = 'false';
if ($conf->getConf('DEBUG.ENABLE')) {
$label = 'true';
}
echo 'debug enable ' . $label . "\n";
$label = 'false';
if ($conf->getConf('DEBUG.LOG')) {
$label = 'true';
}
echo 'debug log error ' . $label . "\n";
$label = 'false';
if ($conf->getConf('DEBUG.DISPLAY_ERROR')) {
$label = 'true';
}
echo 'debug display error ' . $label . "\n";
echo 'swoole version ' . phpversion('swoole') . "\n";
echo 'easyswoole version ' . \Core\Component\Di::getInstance()->get(Core\Component\SysConst::VERSION) . "\n";
$server->run();
}
function stopServer($options)
{
$pidFile = \Conf\Config::getInstance()->getConf("SERVER.CONFIG.pid_file");
if (!empty($options['pid'])) {
$pidFile = $options['pid'];
}
if (!file_exists($pidFile)) {
echo "pid file :{$pidFile} not exist \n";
return;
}
$pid = file_get_contents($pidFile);
if (!swoole_process::kill($pid, 0)) {
echo "pid :{$pid} not exist \n";
return;
}
if (isset($options['f'])) {
swoole_process::kill($pid, SIGKILL);
} else {
swoole_process::kill($pid);
}
//等待两秒
$time = time();
while (true) {
usleep(1000);
if (!swoole_process::kill($pid, 0)) {
echo "server stop at " . date("y-m-d h:i:s") . "\n";
if (is_file($pidFile)) {
unlink($pidFile);
}
break;
} else {
if (time() - $time > 2) {
echo "stop server fail.try --force again \n";
break;
}
}
}
}
function reloadServer($options)
{
$pidFile = \Conf\Config::getInstance()->getConf("SERVER.CONFIG.pid_file");
if (isset($options['pid'])) {
if (!empty($options['pid'])) {
$pidFile = $options['pid'];
}
}
if (isset($options['all']) && $options['all'] == false) {
$sig = SIGUSR2;
} else {
$sig = SIGUSR1;
}
if (!file_exists($pidFile)) {
echo "pid file :{$pidFile} not exist \n";
return;
}
$pid = file_get_contents($pidFile);
opCacheClear();
if (!swoole_process::kill($pid, 0)) {
echo "pid :{$pid} not exist \n";
return;
}
swoole_process::kill($pid, $sig);
echo "send server reload command at " . date("y-m-d h:i:s") . "\n";
}
function help($options)
{
$opName = '';
$args = array_keys($options);
if (isset($args[0])) {
$opName = $args[0];
}
switch ($opName) {
case 'start':
{
echo "------------easySwoole 启动命令------------\n";
echo "执行php server.php start 即可启动服务。启动可选参数为:\n";
echo "--d 是否以系统守护模式运行\n";
echo "--p-portNumber 指定服务监听端口\n";
echo "--pid-fileName 指定服务PID存储文件\n";
echo "--workerNum-num 设置worker进程数\n";
echo "--taskWorkerNum-num 设置Task进程数\n";
echo "--user-userName 指定以某个用户身份执行\n";
echo "--group-groupName 指定以某个用户组身份执行\n";
echo "--taskWorkerNum-num 设置Task进程数\n";
echo "--cpuAffinity-boolean 是否开启CPU亲和\n";
break;
}
case 'stop':
{
echo "------------easySwoole 停止命令------------\n";
echo "执行php server.php stop 即可停止服务。停止可选参数为:\n";
echo "--pid-fileName 指定服务PID存储文件\n";
echo "--f 强制停止服务\n";
break;
}
case 'reload':
{
echo "------------easySwoole 重启命令------------\n";
echo "执行php server.php reload 即可重启服务。重启可选参数为:\n";
echo "--pid-fileName 指定服务PID存储文件\n";
echo "--pid-all 是否重启所有进程,默认true\n";
break;
}
case 'update':
{
echo "------------easySwoole 更新命令------------\n";
echo "执行php server.php update 即可更新框架。更新可选参数为:\n";
echo "--ide-help 更新ide助手文件,不存在则下载\n";
break;
}
default:
{
echo "------------欢迎使用easySwoole------------\n";
echo "有关某个命令的详细信息,请使用 help 命令, 如help --start 可选参数为:\n";
echo "--start 启动easySwoole\n";
echo "--stop 停止easySwoole\n";
echo "--reload 重启easySwoole\n";
echo "--update 更新框架文件\n";
}
}
}
function evenCheck()
{
if (version_compare(phpversion(), '5.6', '<')) {
die("php version must >= 5.6");
}
if (version_compare(phpversion('swoole'), '1.9.5', '<')) {
die("swoole version must >= 1.9.5");
}
}
function opCacheClear()
{
if (function_exists('apc_clear_cache')) {
apc_clear_cache();
}
if (function_exists('opcache_reset')) {
opcache_reset();
}
}
evenCheck();
commandHandler();