From d4a15d26ed9d4a586b8318b740c2067fb99b40d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Wed, 17 Nov 2021 16:43:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.php | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/function.php b/function.php index 3e003fe5..0e05d499 100644 --- a/function.php +++ b/function.php @@ -18,6 +18,7 @@ use Kiri\Exception\NotFindClassException; use Kiri\Kiri; use Psr\Log\LoggerInterface; use Server\ServerManager; +use Swoole\Process; use Swoole\WebSocket\Server; if (!function_exists('make')) { @@ -52,6 +53,38 @@ if (!function_exists('make')) { } +if (!function_exists('checkPortIsAlready')) { + + + /** + * @param $port + * @return bool|string + * @throws Exception + */ + function checkPortIsAlready($port): bool|string + { + if (!Kiri::getPlatform()->isLinux()) { + exec("lsof -i :" . $port . " | grep -i 'LISTEN' | awk '{print $2}'", $output); + if (empty($output)) return false; + $output = explode(PHP_EOL, $output[0]); + return $output[0]; + } + + $serverPid = file_get_contents(storage('.swoole.pid')); + if (!empty($serverPid) && shell_exec('ps -ef | grep ' . $serverPid . ' | grep -v grep')) { + Process::kill($serverPid, SIGTERM); + } + + exec('netstat -lnp | grep ' . $port . ' | grep "LISTEN" | awk \'{print $7}\'', $output); + if (empty($output)) { + return false; + } + return explode('/', $output[0])[0]; + } + +} + + if (!function_exists('done')) { /** @@ -59,12 +92,26 @@ if (!function_exists('done')) { */ function done() { - ServerManager::setEnv('state', 'exit'); + set_env('state', 'exit'); } } +if (!function_exists('set_env')) { + + + /** + * @param $key + * @param $value + */ + function set_env($key, $value) + { + putenv(sprintf('%s=%s', $key, $value)); + } + +} + if (!function_exists('enable_file_modification_listening')) {