-
Notifications
You must be signed in to change notification settings - Fork 2
/
swoolefy
306 lines (275 loc) · 10.1 KB
/
swoolefy
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
299
300
301
302
303
304
305
306
<?php
// score目录,根据模式自动适应,composer安装模式
if(is_dir(__DIR__.'/vendor/bingcool/swoolefy')) {
$SCORE_DIR = __DIR__.'/vendor/bingcool/swoolefy';
}else {
// 直接下载使用模式
$SCORE_DIR = __DIR__;
}
// 定义一个全局常量
defined('SCORE_DIR_ROOT') or define('SCORE_DIR_ROOT', $SCORE_DIR);
// 启动文件目录
defined('START_DIR_ROOT') or define('START_DIR_ROOT', __DIR__);
// include composer的自动加载类完成命名空间的注册
include_once START_DIR_ROOT.'/vendor/autoload.php';
// include App应用层的自定义的自动加载类命名空间
include_once START_DIR_ROOT.'/autoloader.php';
function initCheck(){
if(version_compare(phpversion(),'7.0.0','<')) {
die("php version must >= 7.0.0");
}
if(version_compare(swoole_version(),'1.9.15','<')) {
die("swoole version must >= 1.9.15");
}
}
function opCacheClear(){
if(function_exists('apc_clear_cache')){
apc_clear_cache();
}
if(function_exists('opcache_reset')){
opcache_reset();
}
}
function commandParser() {
global $argv;
$command = isset($argv[1]) ? $argv[1] : null;
$server = isset($argv[2]) ? $argv[2] : null;
return ['command'=>$command, 'server'=>$server];
}
function startServer($server) {
opCacheClear();
global $argv;
switch(strtolower($server)) {
case 'http':{
$path = START_DIR_ROOT.'/protocol/http';
if(!is_dir($path)) {
@mkdir($path, 0777, true);
}
$config_file = $path.'/config.php';
if(!file_exists($config_file)) {
copy(SCORE_DIR_ROOT.'/score/Http/config.php', $config_file);
}
$event_server_file = $path.'/HttpServer.php';
if(!file_exists($event_server_file)) {
copy(SCORE_DIR_ROOT.'/score/EventServer/HttpServer.php', $event_server_file);
}
$config = include $config_file;
if(isset($argv[3]) && ($argv[3] == '-d' || $argv[3] == '-D')) {
$config['setting']['daemonize'] = true;
}
$http = new \protocol\http\HttpServer($config);
$http->start();
break;
}
case 'websocket':{
$path = START_DIR_ROOT.'/protocol/websocket';
if(!is_dir($path)) {
@mkdir($path, 0777, true);
}
$config_file = $path.'/config.php';
if(!file_exists($config_file)) {
copy(SCORE_DIR_ROOT.'/score/Websocket/config.php', $config_file);
}
$event_server_file = $path.'/WebsocketEventServer.php';
if(!file_exists($event_server_file)) {
copy(SCORE_DIR_ROOT.'/score/EventServer/WebsocketEventServer.php', $event_server_file);
}
$config = include $config_file;
if(isset($argv[3]) && ($argv[3] == '-d' || $argv[3] == '-D')) {
$config['setting']['daemonize'] = true;
}
$websocket = new \protocol\websocket\WebsocketEventServer($config);
$websocket->start();
break;
}
case 'rpc': {
$path = START_DIR_ROOT.'/protocol/rpc';
if(!is_dir($path)) {
@mkdir($path, 0777, true);
}
$config_file = $path.'/config.php';
if(!file_exists($config_file)) {
copy(SCORE_DIR_ROOT.'/score/Rpc/config.php', $config_file);
}
$event_server_file = $path.'/RpcServer.php';
if(!file_exists($event_server_file)) {
copy(SCORE_DIR_ROOT.'/score/EventServer/RpcServer.php', $event_server_file);
}
$config = include $config_file;
if(isset($argv[3]) && ($argv[3] == '-d' || $argv[3] == '-D')) {
$config['setting']['daemonize'] = true;
}
$rpc = new \protocol\rpc\RpcServer($config);
$rpc->start();
break;
}
case 'udp': {
$path = START_DIR_ROOT.'/protocol/udp';
if(!is_dir($path)) {
@mkdir($path, 0777, true);
}
$config_file = $path.'/config.php';
if(!file_exists($config_file)) {
copy(SCORE_DIR_ROOT.'/score/Udp/config.php', $config_file);
}
$event_server_file = $path.'/UdpEventServer.php';
if(!file_exists($event_server_file)) {
copy(SCORE_DIR_ROOT.'/score/EventServer/UdpEventServer.php', $event_server_file);
}
$config = include $config_file;
if(isset($argv[3]) && ($argv[3] == '-d' || $argv[3] == '-D')) {
$config['setting']['daemonize'] = true;
}
$rpc = new \protocol\udp\UdpEventServer($config);
$rpc->start();
break;
}
case 'monitor' :{
global $argv;
$path = START_DIR_ROOT.'/protocol/monitor';
if(!is_dir($path)) {
@mkdir($path, 0777, true);
}
$config_file = $path.'/config.php';
if(!file_exists($config_file)) {
copy(SCORE_DIR_ROOT.'/score/AutoReload/config.php', $config_file);
}
if(isset($argv[3]) && ($argv[3] == '-d' || $argv[3] == '-D')) {
swoole_process::daemon(true,false);
}
$pid = posix_getpid();
$monitor_pid_file = $path.'/monitor.pid';
@file_put_contents($monitor_pid_file, $pid);
// 设置当前进程的名称
cli_set_process_title("php-autoreload-swoole-server");
$config = include $config_file;
// 创建进程服务实例
$daemon = new \Swoolefy\AutoReload\daemon($config);
// 启动
$daemon->run();
break;
}
default:{
help($command='help');
}
}
return ;
}
function stopServer($server) {
switch(strtolower($server)) {
case 'http': {
$path = START_DIR_ROOT.'/protocol/http';
$pid_file = $path.'/server.pid';
break;
}
case 'websocket': {
$path = START_DIR_ROOT.'/protocol/websocket';
$pid_file = $path.'/server.pid';
break;
}
case 'rpc': {
$path = START_DIR_ROOT.'/protocol/rpc';
$pid_file = $path.'/server.pid';
break;
}
case 'udp': {
$path = START_DIR_ROOT.'/protocol/udp';
$pid_file = $path.'/server.pid';
break;
}
case 'monitor': {
$path = START_DIR_ROOT.'/protocol/monitor';
$pid_file = $path.'/monitor.pid';
break;
}
default:{
help($command='help');
}
}
if(!is_file($pid_file)) {
echo "warning: pid file {$pid_file} is not exist! \n";
return;
}
$pid = intval(file_get_contents($pid_file));
if(!swoole_process::kill($pid,0)){
echo "warning: pid={$pid} not exist \n";
return;
}
// 发送信号,终止进程
swoole_process::kill($pid,SIGTERM);
// 回收master创建的子进程(manager,worker,taskworker)
swoole_process::wait();
//等待2秒
$nowtime = time();
while(true){
usleep(1000);
if(!swoole_process::kill($pid,0)){
echo "------------stop info------------\n";
echo "successful: server stop at ".date("Y-m-d H:i:s")."\n";
echo "\n";
@unlink($pid_file);
break;
}else {
if(time() - $nowtime > 2){
echo "-----------stop info------------\n";
echo "warnning: stop server failed. please try again \n";
echo "\n";
break;
}
}
}
}
function help($command) {
switch(strtolower($command.'-'.'help')) {
case 'start-help':{
echo "------------swoolefy启动服务命令------------\n";
echo "1、执行php swoolefy start http 即可启动http server服务\n\n";
echo "2、执行php swoolefy start websocket 即可启动websocket server服务\n\n";
echo "3、执行php swoolefy start rpc 即可启动rpc server服务\n\n";
echo "4、执行php swoolefy start udp 即可启动udp server服务\n\n";
echo "5、执行php swoolefy start monitor 即在当前终端启动monitor 监控文件自动重启worker服务\n\n";
echo "6、执行php swoolefy start monitor -d 即以守护进程启动monitor 监控文件自动重启worker服务\n\n";
echo "\n";
break;
}
case 'stop-help':{
echo "------------swoolefy终止服务命令------------\n";
echo "1、执行php swoolefy stop http 即可终止http server服务\n\n";
echo "2、执行php swoolefy stop websocket 即可终止websocket server服务\n\n";
echo "3、执行php swoolefy stop rpc 即可终止rpc server服务\n\n";
echo "4、执行php swoolefy stop udp 即可终止rpc server服务\n\n";
echo "5、执行php swoolefy stop monitor 即可终止monitor 监控文件自动重启worker服务\n\n";
echo "\n";
break;
}
default:{
echo "------------欢迎使用swoolefy------------\n";
echo "有关某个命令的详细信息,请键入 help 命令:\n\n";
echo "1、php swoolefy start help 查看详细信息!\n\n";
echo "2、php swoolefy stop help 查看详细信息!\n\n";
}
}
}
function commandHandler(){
$command = commandParser();
if(isset($command['server']) && $command['server'] != 'help') {
switch($command['command']){
case "start":{
startServer($command['server']);
break;
}
case 'stop':{
stopServer($command['server']);
break;
}
case 'help':
default:{
help($command['command']);
}
}
}else {
help($command['command']);
}
}
initCheck();
commandHandler();