-
Notifications
You must be signed in to change notification settings - Fork 17
/
helpers.php
166 lines (144 loc) · 2.91 KB
/
helpers.php
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
<?php
/**
* @author jan huang <bboyjanhuang@gmail.com>
* @copyright 2016
*
* @link https://www.github.com/janhuang
* @link http://www.fast-d.cn/
*/
/**
* @param $name
*/
function process_rename ($name)
{
set_error_handler(function () {
});
if (function_exists('cli_set_process_title')) {
cli_set_process_title($name);
} else if (function_exists('swoole_set_process_name')) {
swoole_set_process_name($name);
}
restore_error_handler();
}
/**
* Kill somebody
*
* @param $pid
* @param int $signo
* @return int
*/
function process_kill($pid, $signo = SIGTERM)
{
return swoole_process::kill($pid, $signo);
}
/**
* @param bool $blocking
* @return array
*/
function process_wait($blocking = true)
{
return swoole_process::wait($blocking);
}
/**
* @param bool $nochdir
* @param bool $noclose
* @return mixed
*/
function process_daemon($nochdir = true, $noclose = true)
{
return swoole_process::daemon($nochdir, $noclose);
}
/**
* @param $signo
* @param callable $callback
* @return mixed
*/
function process_signal($signo, callable $callback)
{
return swoole_process::signal($signo, $callback);
}
/**
* @param $interval
* @param $type
* @return bool
*/
function process_alarm($interval, $type = ITIMER_REAL)
{
return swoole_process::alarm($interval, $type);
}
/**
* @param array $cpus
* @return mixed
*/
function process_affinity(array $cpus)
{
return swoole_process::setaffinity($cpus);
}
/**
* @param $interval
* @param callable $callback
* @return mixed
*/
function timer_tick($interval, $callback, array $params = [])
{
return swoole_timer_tick($interval, $callback, $params);
}
/**
* @param $interval
* @param callable $callback
* @param array $params
* @return mixed
*/
function timer_after($interval, $callback, array $params = [])
{
return swoole_timer_after($interval, $callback, $params);
}
/**
* @param $timerId
* @return mixed
*/
function timer_clear($timerId)
{
return swoole_timer_clear($timerId);
}
/**
* @return string
*/
function get_local_ip()
{
$serverIps = swoole_get_local_ip();
$patternArray = [
'10\.',
'172\.1[6-9]\.',
'172\.2[0-9]\.',
'172\.31\.',
'192\.168\.'
];
foreach ($serverIps as $serverIp) {
if (preg_match('#^' . implode('|', $patternArray) . '#', $serverIp)) {
return $serverIp;
}
}
return gethostbyname(gethostname());
}
/**
* @param $keyword
* @return bool
*/
function process_is_running($keyword)
{
$scriptName = pathinfo($_SERVER['SCRIPT_FILENAME'], PATHINFO_BASENAME);
$command = "ps axu | grep '{$keyword}' | grep -v grep | grep -v {$scriptName}";
exec($command, $output);
return !empty($output);
}
/**
* @param $port
* @return bool
*/
function port_is_running($port)
{
$command = "lsof -i:{$port} | grep LISTEN";
exec($command, $output);
return !empty($output);
}