Browse Source

feat: accept `SIGINT` and `SIGTERM`

master v1.2.0-rc2
dnomd343 2 years ago
parent
commit
4aa67d2c37
  1. 2
      Dockerfile
  2. 31
      main.php
  3. 20
      src/Daemon.php

2
Dockerfile

@ -30,7 +30,7 @@ COPY . /asset/kms-server/
RUN mkdir -p /asset/etc/ && mv /asset/kms-server/nginx/ /asset/etc/
FROM alpine:3.16
RUN apk add --no-cache nginx php8 php8-fpm php8-iconv php8-pcntl
RUN apk add --no-cache nginx php8 php8-fpm php8-iconv php8-pcntl php8-posix
COPY --from=asset /asset/ /
EXPOSE 1688/tcp 1689/tcp
WORKDIR /kms-server/

31
main.php

@ -1,15 +1,11 @@
<?php
$version = 'dev';
$version = 'v1.2.0-rc2';
require_once './src/Daemon.php';
require_once './src/Logger.php';
require_once './src/Process.php';
if (in_array('--debug', $argv)) { // debug mode
logging::$logLevel = logging::DEBUG;
}
$nginx = array(
'name' => 'nginx',
'command' => ['/usr/sbin/nginx'],
@ -28,13 +24,26 @@ $vlmcsd = array(
'pidFile' => '/run/vlmcsd.pid',
);
logging::info('Loading kms-server (' . $version . ')');
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, $phpFpm, $vlmcsd;
logging::info('Get SIGTERM -> exit');
subExit($nginx['pidFile'], $phpFpm['pidFile'], $vlmcsd['pidFile']);
});
pcntl_signal(SIGINT, function() { // receive SIGINT signal
global $nginx, $phpFpm, $vlmcsd;
logging::info('Get SIGINT -> exit');
subExit($nginx['pidFile'], $phpFpm['pidFile'], $vlmcsd['pidFile']);
});
if (in_array('--debug', $argv)) { // enter debug mode
logging::$logLevel = logging::DEBUG;
}
logging::info('Loading kms-server (' . $version . ')');
new Process($nginx['command']);
logging::info('Start nginx server...OK');
new Process($phpFpm['command']);
@ -42,9 +51,11 @@ logging::info('Start php-fpm server...OK');
new Process($vlmcsd['command']);
logging::info('Start vlmcsd server...OK');
logging::info('Enter the daemon process');
while (true) {
msSleep(5000); // sleep 5s
logging::info('Enter daemon process');
while (true) { // start daemon
for ($i = 0; $i < 500; $i++) { // sleep 5s
msDelay(10); // return main loop every 10ms
}
daemon($nginx);
daemon($phpFpm);
daemon($vlmcsd);

20
src/Daemon.php

@ -1,9 +1,11 @@
<?php
use JetBrains\PhpStorm\NoReturn;
require_once 'Logger.php';
require_once 'Process.php';
function msSleep(int $ms): void { // sleep for xxx ms
function msDelay(int $ms): void { // delay for xxx ms
for ($i = 0; $i < $ms; $i++) {
usleep(1000); // split multiple times (avoid SIGCHLD signal)
}
@ -45,3 +47,19 @@ function daemon(array $info): void {
logging::info('Restart ' . $info['name'] . ' success');
}
}
#[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...');
pcntl_wait($status); // wait all process exit
logging::info('All process exit, Goodbye!');
exit;
}

Loading…
Cancel
Save