Browse Source

Merge branch 'dev'

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

52
kms.php

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

22
src/Daemon.php

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

Loading…
Cancel
Save