-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap
134 lines (121 loc) · 3.28 KB
/
bootstrap
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
#!/usr/bin/env php
<?php
// 启动文件目录
defined('ROOT') or define('ROOT', __DIR__);
//日志目录
defined('LOG') or define('LOG', ROOT . DIRECTORY_SEPARATOR . 'log');
$loaderFile = ROOT . '/vendor/autoload.php';
if(file_exists($loaderFile)){
$loader = include_once ROOT . '/vendor/autoload.php';
$loader->add('Thumb\\', ROOT . '/src');//这里是因为src目录是自定义的目录,不是vendor下面的目录.所以要这样加载
}else{
die("include composer autoload.php fail\n");
}
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 commandParser()
{
global $argv;
$command = $argv[1] ?? null;
$server = $argv[2] ?? null;
return ['command' => $command, 'server' => $server];
}
function startServer($server)
{
global $argv;
switch (strtolower($server)) {
case 'http':
{
include ROOT.DIRECTORY_SEPARATOR.'http/HttpServer.php';
$http = new HttpServer();
$http->start();
break;
}
default:
{
help($command = 'help');
}
}
return;
}
function stopServer($server)
{
global $argv;
switch (strtolower($server)) {
case 'http':
{
$pid_file = LOG . '/server.pid';
break;
}
default:
{
help($command = 'help');
}
}
if (!is_file($pid_file)) {
echo "error: pid file {$pid_file} is not exist! \n";
return;
}
$pid = intval(file_get_contents($pid_file));
if (!swoole_process::kill($pid, 0)) {
echo "error: 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 "error: stop server failed. please try again \n";
echo "\n";
break;
}
}
}
}
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();