-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver
130 lines (111 loc) · 2.89 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
#!/user/bin/env php
<?php
use App\Process\ServerManage;
use App\Config;
define('ROOT_PATH', realpath(getcwd()));
require 'vendor/autoload.php';
error_reporting(E_ALL);
ini_set('error_log', ROOT_PATH . '/Logs/error.log');
function commandHandle()
{
list($command, $options) = commandParse();
switch ( $command ) {
case 'start':
ServerManage::getInstance($options)->start();
break;
case 'stop':
serverStop();
break;
case 'reload':
serverReload();
break;
case '--help':
case '-h':
case 'help':
case 'h':
default:
showHelp();
break;
}
}
/**
* 关闭服务器
* @return void
*/
function serverStop()
{
$conf = Config::getInstance()->getConf('MAIN_SERVER');
$pid_file = $conf['SETTING']['pid_file'];
if ( is_file($pid_file) ) {
$pid = file_get_contents($pid_file);
}
if ( !isset($pid) || !swoole_process::kill($pid, 0) ) {
echo "\e[31m 进程:{$pid}不存在.\e[0m\n";
return;
}
swoole_process::kill($pid);
$time = time();
while ( true ) {
usleep(1000);
if ( !swoole_process::kill($pid, 0) ) {
echo "\e[31mserver stop at " . date('Y-m-d H:i:s') . ".\e[0m\n";
if ( is_file($pid_file) ) {
@unlink($pid_file);
}
break;
} else {
if ( time() - $time > 5 ) {
echo "\e[31mstop server fail,please try again\e[0m\n";
break;
}
}
}
}
function serverReload()
{
$conf = Config::getInstance()->getConf('MAIN_SERVER');
$pid_file = $conf['SETTING']['pid_file'];
if ( is_file($pid_file) ) {
$pid = file_get_contents($pid_file);
}
if ( !isset($pid) || !swoole_process::kill($pid, 0) ) {
echo "\e[31m 进程:{$pid}不存在.\e[0m\n";
return;
}
swoole_process::kill($pid, SIGUSR2);
echo "\e[31mserver reload at" . date('Y-m-d H:i:s') . ".\e[0m\n";
}
/**
* 解析参数
* @return array
*/
function commandParse()
{
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 [ $command, $options ];
}
function showHelp()
{
echo <<<HELP
\e[33m用法:
\e[31m php server (start|stop|reload) (--d)\e[0m
\e[33m参数说明:\e[0m
\e[37m \e[36mstart\e[0m 启动进程 接入--d表示以守护进程方式运行\e[0m
\e[37m \e[36mstop\e[0m 结束进程\e[0m
\e[37m \e[36mreload\e[0m 重启进程\e[0m\n
HELP;
}
commandHandle();