Browse Source

Merge branch 'dev'

master
Dnomd343 2 years ago
parent
commit
a49a7455f0
  1. 40
      kms.php
  2. 22
      src/Daemon.php

40
kms.php

@ -1,7 +1,7 @@
#!/usr/bin/env php8
<?php
$VERSION = 'v1.2.2';
$VERSION = 'v1.2.3';
require_once './src/Daemon.php';
require_once './src/Logger.php';
@ -9,6 +9,7 @@ require_once './src/Process.php';
$KMS_PORT = 1688; // kms expose port
$HTTP_PORT = 1689; // http server port
$ENABLE_HTTP = true; // http interface
$NGINX = array( // nginx process
'name' => 'nginx',
@ -78,6 +79,12 @@ function load_params(): void {
logging::$logLevel = logging::DEBUG;
}
global $ENABLE_HTTP;
if (in_array('--disable-http', $argv)) { // disable http service
logging::warning('Disable http service');
$ENABLE_HTTP = false;
}
global $KMS_PORT;
$KMS_PORT = intval(get_param('--kms-port', strval($KMS_PORT)));
if ($KMS_PORT < 1 || $KMS_PORT > 65535) { // 1 ~ 65535
@ -104,28 +111,49 @@ function load_params(): void {
}
function start_process(): void { // start sub processes
global $ENABLE_HTTP;
global $NGINX, $PHP_FPM, $VLMCSD;
if ($ENABLE_HTTP) { // http server process
new Process($NGINX['command']);
logging::info('Start nginx server...OK');
new Process($PHP_FPM['command']);
logging::info('Start php-fpm server...OK');
}
new Process($VLMCSD['command']);
logging::info('Start vlmcsd server...OK');
}
function exit_process(): void { // kill sub processes
global $ENABLE_HTTP;
global $NGINX, $PHP_FPM, $VLMCSD;
if ($ENABLE_HTTP) {
subExit($NGINX, $PHP_FPM, $VLMCSD); // with http service
} else {
subExit($VLMCSD);
}
}
declare(ticks = 1);
pcntl_signal(SIGCHLD, function() { // receive SIGCHLD signal
pcntl_wait($status, WNOHANG); // avoid zombie process
});
pcntl_signal(SIGTERM, function() { // receive SIGTERM signal
global $NGINX, $PHP_FPM, $VLMCSD;
logging::info('Get SIGTERM -> exit');
subExit($NGINX['pidFile'], $PHP_FPM['pidFile'], $VLMCSD['pidFile']);
exit_process();
exit;
});
pcntl_signal(SIGINT, function() { // receive SIGINT signal
global $NGINX, $PHP_FPM, $VLMCSD;
logging::info('Get SIGINT -> exit');
subExit($NGINX['pidFile'], $PHP_FPM['pidFile'], $VLMCSD['pidFile']);
exit_process();
exit;
});
pcntl_signal(SIGQUIT, function() { // receive SIGQUIT signal
logging::info('Get SIGQUIT -> exit');
exit_process();
exit;
});
logging::info('Loading kms-server (' . $VERSION . ')');
@ -139,7 +167,9 @@ while (true) { // start daemon
for ($i = 0; $i < 500; $i++) { // sleep 5s
msDelay(10); // return main loop every 10ms
}
if ($ENABLE_HTTP) { // with http service
daemon($NGINX);
daemon($PHP_FPM);
}
daemon($VLMCSD);
}

22
src/Daemon.php

@ -1,7 +1,5 @@
<?php
use JetBrains\PhpStorm\NoReturn;
require_once 'Logger.php';
require_once 'Process.php';
@ -48,18 +46,14 @@ function daemon(array $info): void {
}
}
#[NoReturn] function subExit(string $nginxPid, string $phpFpmPid, string $vlmcsdPid): void {
$nginxPid = getPid($nginxPid);
logging::info("Sending kill signal to nginx ($nginxPid)");
posix_kill($nginxPid, SIGTERM);
$phpFpmPid = getPid($phpFpmPid);
logging::info("Sending kill signal to php-fpm ($phpFpmPid)");
posix_kill($phpFpmPid, SIGTERM);
$vlmcsdPid = getPid($vlmcsdPid);
logging::info("Sending kill signal to vlmcsd ($vlmcsdPid)");
posix_kill($vlmcsdPid, SIGTERM);
logging::info('Waiting sub process exit...');
function subExit(array ...$subList): void {
foreach ($subList as $sub) {
$subName = $sub['name'];
$subPid = getPid($sub['pidFile']);
logging::info("Sending kill signal to $subName (PID = $subPid)");
posix_kill($subPid, SIGTERM);
}
logging::info('Waiting sub-process exit...');
pcntl_wait($status); // wait all process exit
logging::info('All process exit, Goodbye!');
exit;
}

Loading…
Cancel
Save