-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun
More file actions
315 lines (304 loc) · 8.77 KB
/
run
File metadata and controls
315 lines (304 loc) · 8.77 KB
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
307
308
309
310
311
312
313
314
315
#!/bin/env php
<?php
/**
* 默认时区定义
*/
date_default_timezone_set('Asia/Shanghai');
/**
* 设置错误报告模式
*/
error_reporting(E_ALL);
/**
* 设置默认区域
*/
setlocale(LC_ALL, "zh_CN.utf-8");
/**
* 检查exec 函数是否启用
*/
if (!function_exists('exec')) {
exit('exec function is disabled' . PHP_EOL);
}
/**
* 定义项目根目录&gm-agent pid
*/
define('SWOOLE_PATH', __DIR__);
define('SWOOLE_TASK_PID_PATH', SWOOLE_PATH . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'gm-agent.pid');
define('SWOOLE_TASK_NAME_PRE', 'gm-agent');
/**
* 加载 swoole http server
*/
require_once "vendor/autoload.php";
use \Hprose\Future;
use \Hprose\Swoole\Client;
function servStart($host, $port, $daemon, $name)
{
echo "正在启动 gm-agent 服务" . PHP_EOL;
if (!is_writable(dirname(SWOOLE_TASK_PID_PATH))) {
exit("gm-agent-pid文件需要目录的写入权限:" . dirname(SWOOLE_TASK_PID_PATH) . PHP_EOL);
}
if (file_exists(SWOOLE_TASK_PID_PATH)) {
$pid = explode("\n", file_get_contents(SWOOLE_TASK_PID_PATH));
$cmd = "ps ax | awk '{ print $1 }' | grep -e \"^{$pid[0]}$\"";
exec($cmd, $out);
if (!empty($out)) {
exit("gm-agent pid文件 " . SWOOLE_TASK_PID_PATH . " 存在,gm-agent 服务器已经启动,进程pid为:{$pid[0]}" . PHP_EOL);
} else {
echo "警告:gm-agent pid文件 " . SWOOLE_TASK_PID_PATH . " 存在,可能gm-agent服务上次异常退出(非守护模式ctrl+c终止造成是最大可能)" . PHP_EOL;
unlink(SWOOLE_TASK_PID_PATH);
}
}
if ($daemon) {
\Swoole\Process::daemon(true, true);
$ppid = getmypid();
file_put_contents(SWOOLE_TASK_PID_PATH, $ppid);
}
if (function_exists('swoole_set_process_name') && PHP_OS != 'Darwin') {
swoole_set_process_name(SWOOLE_TASK_NAME_PRE."//{$host}:{$port}");
}
$cli = new Client("tcp://{$host}:{$port}");
$cli->fullDuplex = false;
\swoole_timer_tick(1000, function($id,$cli){
$cli->getTask()
->then(function($r) use ($cli) {
foreach ($r as $k) {
$k['run_code'] = 300;
$cli->addTaskLog($k);
}
return $r;
})->then(function($r) use ($cli) {
foreach ($r as $k) {
$k['run_code'] = 400;
$cli->addTaskLog($k);
}
return $r;
})->then(function($r) use ($cli) {
foreach ($r as $k) {
runTask($k,$cli);
}
return $r;
});
},$cli);
// $server = new HttpServer();
// $server->run($host, $port, $daemon, $name);
// 确保服务器启动后gm-agent-pid文件必须生成
if (!file_exists(SWOOLE_TASK_PID_PATH)) {
exit("gm-agent pid文件生成失败( " . SWOOLE_TASK_PID_PATH . ") ,请手动关闭当前启动的gm-agent服务检查原因" . PHP_EOL);
}
exit("启动 gm-agent 成功" . PHP_EOL);
}
function runTask($task = '',$cli = '')
{
$process = new \Swoole\Process(function ($worker) use ($task,$cli) {
try {
$exec = $task["execute"];
@$worker->name($exec . "#" . $task["id"]);
$exec = explode(" ", trim($exec));
$execfile = array_shift($exec);
$retmsg = $worker->exec($execfile,$exec);
// $process->write('Hello');
} catch (Exception $e) {
$task['run_code'] = -300;
$task['msg'] = $e->getMessage();
$cli->addTaskLog($task);
}
},true);
$pid = $process->start();
$task['run_code'] = 500;
$r = $cli->addTaskLog($task)->then(function($r){
// print_r($r);
});
}
function servStop($host, $port, $isRestart = false)
{
echo "正在停止 gm-agent 服务" . PHP_EOL;
if (!file_exists(SWOOLE_TASK_PID_PATH)) {
exit('gm-agent-pid文件:' . SWOOLE_TASK_PID_PATH . '不存在' . PHP_EOL);
}
$pid = explode("\n", file_get_contents(SWOOLE_TASK_PID_PATH));
$cmd = "kill {$pid[0]}";
exec($cmd);
do {
$out = [];
$c = "ps ax | awk '{ print $1 }' | grep -e \"^{$pid[0]}$\"";
exec($c, $out);
if (empty($out)) {
break;
}
} while (true);
//确保停止服务后gm-agent-pid文件被删除
if (file_exists(SWOOLE_TASK_PID_PATH)) {
unlink(SWOOLE_TASK_PID_PATH);
}
$msg = "执行命令 {$cmd} 成功,端口 {$host}:{$port} 进程结束" . PHP_EOL;
if ($isRestart) {
echo $msg;
} else {
exit($msg);
}
}
function servStatus($host, $port)
{
echo "gm-agent {$host}:{$port} 运行状态" . PHP_EOL;
$cmd = "curl -s '{$host}:{$port}?cmd=status'";
exec($cmd, $out);
if (empty($out)) {
exit("{$host}:{$port} gm-agent服务不存在或者已经停止" . PHP_EOL);
}
foreach ($out as $v) {
$a = json_decode($v);
foreach ($a as $k1 => $v1) {
echo "$k1:\t$v1" . PHP_EOL;
}
}
exit();
}
function servList()
{
echo "本机运行的gm-agent服务进程" . PHP_EOL;
$cmd = "ps aux|grep " . SWOOLE_TASK_NAME_PRE . "|grep -v grep|awk '{print $1, $2, $6, $8, $9, $11}'";
exec($cmd, $out);
if (empty($out)) {
exit("没有发现正在运行的gm-agent服务" . PHP_EOL);
}
echo "USER PID RSS(kb) STAT START COMMAND" . PHP_EOL;
foreach ($out as $v) {
echo $v . PHP_EOL;
}
exit();
}
//可执行命令
$cmds = [
'start',
'stop',
'restart',
'status',
'list',
];
$shortopts = "dDh:p:n:";
$longopts = [
'help',
'daemon',
'nondaemon',
'host:',
'port:',
'name:',
];
$opts = getopt($shortopts, $longopts);
if (isset($opts['help']) || $argc < 2) {
echo <<<HELP
用法:php run 选项 ... 命令[start|stop|restart|status|list]
管理gm-agent服务,确保系统 lsof 命令有效
如果不指定监听host或者port,使用配置参数
参数说明
--help 显示本帮助说明
-d, --daemon 指定此参数,以守护进程模式运行,不指定则读取配置文件值
-D, --nondaemon 指定此参数,以非守护进程模式运行,不指定则读取配置文件值
-h, --host 指定监听ip,例如 php run -h 127.0.0.1
-p, --port 指定监听端口port, 例如 php run -h 127.0.0.1 -p 9520
-n, --name 指定服务进程名称,例如 php run -n test start, 则进程名称为SWOOLE_TASK_NAME_PRE-name
启动gm-agent 如果不指定 host和port,默认为 127.0.0.1 端口为 9911
关闭gm-agent 必须指定port,没有指定host,关闭的监听端口是 *:port,指定了host,关闭 host:port端口
重启gm-agent 必须指定端口\n
HELP;
exit;
}
//参数检查
foreach ($opts as $k => $v) {
if (($k == 'h' || $k == 'host')) {
if (empty($v)) {
exit("参数 -h --host 必须指定值\n");
}
}
if (($k == 'p' || $k == 'port')) {
if (empty($v)) {
exit("参数 -p --port 必须指定值\n");
}
}
if (($k == 'n' || $k == 'name')) {
if (empty($v)) {
exit("参数 -n --name 必须指定值\n");
}
}
}
//命令检查
$cmd = $argv[$argc - 1];
if (!in_array($cmd, $cmds)) {
exit("输入命令有误 : {$cmd}, 请查看帮助文档\n");
}
//监听ip,空读取配置文件
$host = '';
if (!empty($opts['h'])) {
$host = $opts['h'];
if (!filter_var($host, FILTER_VALIDATE_IP)) {
exit("输入host有误:{$host}");
}
}
if (!empty($opts['host'])) {
$host = $opts['host'];
if (!filter_var($host, FILTER_VALIDATE_IP)) {
exit("输入host有误:{$host}");
}
}
//监听端口,0 读取配置文件
$port = 0;
if (!empty($opts['p'])) {
$port = (int)$opts['p'];
if ($port <= 0) {
exit("输入port有误:{$port}");
}
}
if (!empty($opts['port'])) {
$port = (int)$opts['port'];
if ($port <= 0) {
exit("输入port有误:{$port}");
}
}
//进程名称 没有默认为 SWOOLE_TASK_NAME_PRE;
$name = '';
if (!empty($opts['n'])) {
$name = $opts['n'];
}
if (!empty($opts['name'])) {
$name = $opts['n'];
}
//是否守护进程 -1 读取配置文件
$isdaemon = -1;
if (isset($opts['D']) || isset($opts['nondaemon'])) {
$isdaemon = 0;
}
if (isset($opts['d']) || isset($opts['daemon'])) {
$isdaemon = 1;
}
if ($cmd == 'start') {
if (empty($host)) {
exit("启动gm-agent服务必须指定host" . PHP_EOL);
}
if (empty($port)) $port = 9911;
servStart($host, $port, $isdaemon, $name);
}
if ($cmd == 'stop') {
if (empty($host)) {
exit("停止gm-agent服务必须指定host" . PHP_EOL);
}
servStop($host, $port);
}
if ($cmd == 'restart') {
if (empty($host)) {
exit("重启gm-agent服务必须指定host" . PHP_EOL);
}
echo "重启gm-agent服务" . PHP_EOL;
servStop($host, $port, true);
servStart($host, $port, $isdaemon, $name);
}
if ($cmd == 'status') {
if (empty($host)) {
$host = '127.0.0.1';
}
if (empty($port)) {
exit("查看gm-agent服务必须指定port(host不指定默认使用127.0.0.1)" . PHP_EOL);
}
servStatus($host, $port);
}
if ($cmd == 'list') {
servList();
}