2024-09-26 09:07:44 +04:00
|
|
|
<?php
|
|
|
|
namespace nur\sery\wip\app\cli;
|
|
|
|
|
2024-09-26 10:54:43 +04:00
|
|
|
use nur\sery\ExitError;
|
|
|
|
use nur\sery\os\proc\Cmd;
|
2024-09-26 17:31:36 +04:00
|
|
|
use nur\sery\os\sh;
|
2024-09-26 10:54:43 +04:00
|
|
|
use nur\sery\output\msg;
|
|
|
|
use nur\sery\wip\app\app2;
|
|
|
|
use nur\sery\wip\app\RunFile;
|
|
|
|
|
2024-09-26 09:07:44 +04:00
|
|
|
class bg_launcher {
|
2024-09-26 17:31:36 +04:00
|
|
|
static function _start(array $args, Runfile $runfile): void {
|
2024-09-26 10:54:43 +04:00
|
|
|
$pid = pcntl_fork();
|
|
|
|
if ($pid == -1) {
|
|
|
|
# parent, impossible de forker
|
|
|
|
throw new ExitError(app2::EC_FORK_PARENT, "Unable to fork");
|
2024-09-26 17:31:36 +04:00
|
|
|
} elseif (!$pid) {
|
|
|
|
# child, fork ok
|
|
|
|
$runfile->wfPrepare($pid);
|
2024-09-26 10:54:43 +04:00
|
|
|
$logfile = $runfile->getLogfile() ?? "/tmp/NULIB_APP_app_start.log";
|
|
|
|
$exitcode = app2::EC_FORK_CHILD;
|
|
|
|
try {
|
|
|
|
# puis lancer la commande
|
|
|
|
$cmd = new Cmd($args);
|
|
|
|
$cmd->addSource("/g/init.env");
|
|
|
|
$cmd->addRedir("both", $logfile, true);
|
2024-09-26 17:31:36 +04:00
|
|
|
$cmd->fork_exec($exitcode, false);
|
|
|
|
sh::_waitpid(-$pid, $exitcode);
|
2024-09-26 10:54:43 +04:00
|
|
|
} finally {
|
2024-09-26 17:31:36 +04:00
|
|
|
$runfile->wfReaped($exitcode);
|
2024-09-26 10:54:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-26 17:31:36 +04:00
|
|
|
private static function kill(int $pid, int $signal): bool {
|
|
|
|
if (!posix_kill($pid, $signal)) {
|
2024-09-26 10:54:43 +04:00
|
|
|
switch (posix_get_last_error()) {
|
|
|
|
case PCNTL_ESRCH:
|
|
|
|
msg::afailure("process inexistant");
|
|
|
|
break;
|
|
|
|
case PCNTL_EPERM:
|
|
|
|
msg::afailure("process non accessible");
|
|
|
|
break;
|
|
|
|
case PCNTL_EINVAL:
|
|
|
|
msg::afailure("signal invalide");
|
|
|
|
break;
|
|
|
|
}
|
2024-09-26 17:31:36 +04:00
|
|
|
return false;
|
2024-09-26 10:54:43 +04:00
|
|
|
}
|
2024-09-26 17:31:36 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function _stop(Runfile $runfile): bool {
|
|
|
|
$data = $runfile->read();
|
|
|
|
$pid = $runfile->_getCid($data);
|
|
|
|
$stopped = false;
|
|
|
|
msg::action("term $pid");
|
2024-09-26 10:54:43 +04:00
|
|
|
$timeout = 10;
|
2024-09-26 17:31:36 +04:00
|
|
|
$delay = 300000;
|
|
|
|
while (--$timeout >= 0) {
|
|
|
|
if (!self::kill($pid, SIGTERM)) {
|
|
|
|
msg::afailure();
|
|
|
|
return false;
|
2024-09-26 10:54:43 +04:00
|
|
|
}
|
2024-09-26 17:31:36 +04:00
|
|
|
usleep($delay);
|
|
|
|
$delay = 1000000; // attendre 1 seconde à partir de la deuxième fois
|
|
|
|
if (!$runfile->_isRunning($data)) {
|
|
|
|
msg::asuccess();
|
|
|
|
$stopped = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$stopped) {
|
|
|
|
msg::action("kill $pid");
|
|
|
|
$timeout = 3;
|
|
|
|
$delay = 300000;
|
|
|
|
while (--$timeout >= 0) {
|
|
|
|
if (!self::kill($pid, SIGKILL)) {
|
|
|
|
msg::afailure();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
usleep($delay);
|
|
|
|
$delay = 1000000; // attendre 1 seconde à partir de la deuxième fois
|
|
|
|
if (!$runfile->_isRunning($data)) {
|
|
|
|
msg::asuccess();
|
|
|
|
$stopped = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($stopped) {
|
|
|
|
sh::_waitpid($pid, $exitcode);
|
|
|
|
$runfile->wfReaped($exitcode);
|
2024-09-26 10:54:43 +04:00
|
|
|
}
|
2024-09-26 17:31:36 +04:00
|
|
|
return $stopped;
|
2024-09-26 10:54:43 +04:00
|
|
|
}
|
2024-09-26 09:07:44 +04:00
|
|
|
}
|