modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-09-28 15:47:02 +04:00
parent 1cc2fff84e
commit 18295868d6
4 changed files with 51 additions and 51 deletions

View File

@ -1,11 +1,13 @@
<?php
namespace nur\sery\tools;
use nur\sery\ExitError;
use nur\sery\os\path;
use nur\sery\os\proc\Cmd;
use nur\sery\os\sh;
use nur\sery\output\msg;
use nur\sery\wip\app\app2;
use nur\sery\wip\app\cli\Application;
use nur\sery\wip\app\cli\bg_launcher;
use nur\sery\wip\app\RunFile;
use nur\yaml;
@ -66,10 +68,10 @@ class BgLauncherApp extends Application {
path::abspath(NULIB_APP_app_launcher),
]);
app2::params_putenv();
bg_launcher::_start($args, $runfile);
self::_start($args, $runfile);
break;
case self::ACTION_STOP:
bg_launcher::_stop($runfile);
self::_stop($runfile);
self::show_infos($runfile, -1);
break;
case self::ACTION_INFOS:
@ -77,4 +79,45 @@ class BgLauncherApp extends Application {
break;
}
}
public static function _start(array $args, Runfile $runfile): void {
$pid = pcntl_fork();
if ($pid == -1) {
# parent, impossible de forker
throw new ExitError(app2::EC_FORK_PARENT, "Unable to fork");
} elseif (!$pid) {
# child, fork ok
$runfile->wfPrepare($pid);
$logfile = $runfile->getLogfile() ?? "/tmp/NULIB_APP_app_console.log";
$exitcode = app2::EC_FORK_CHILD;
try {
# rediriger STDOUT et STDERR
fclose(STDOUT);
$out = fopen($logfile, "a+b");
fclose(STDERR);
$err = fopen($logfile, "a+b");
# puis lancer la commande
$cmd = new Cmd($args);
$cmd->addSource("/g/init.env");
$cmd->addRedir("both", $logfile, true);
$cmd->fork_exec($exitcode, false);
sh::_waitpid(-$pid, $exitcode);
} finally {
$runfile->wfReaped($exitcode);
}
}
}
public static function _stop(Runfile $runfile): bool {
$data = $runfile->read();
$pid = $runfile->_getCid($data);
msg::action("stop $pid");
if ($runfile->wfKill($reason)) {
msg::asuccess();
return true;
} else {
msg::afailure($reason);
return false;
}
}
}

View File

@ -2,6 +2,8 @@
namespace nur\sery\tools;
use nur\sery\output\msg;
use nur\sery\php\time\DateTime;
use nur\sery\text\words;
use nur\sery\wip\app\app2;
use nur\sery\wip\app\cli\Application;
@ -33,11 +35,13 @@ EOT,
function main() {
if ($this->installSignalHandler) app2::install_signal_handler();
$count = intval($this->count);
msg::info("Starting train for ".words::q($count, "step#s"));
app2::action("Running train...", $count);
for ($i = 1; $i <= $count; $i++) {
msg::print("Tchou-tchou! x $i");
app2::step();
sleep(1);
}
msg::info("Stopping train at ".new DateTime());
}
}

View File

@ -463,7 +463,7 @@ class app2 {
$name = $this->name;
$runfile = $this->getWorkfile($name);
# ne pas spécifier $name pour avoir le fichier par défaut sans profil
$logfile = $this->getLogfile();
$logfile = $this->getLogfile("$name.console.log", false);
return $this->runfile ??= new RunFile($name, $runfile, $logfile);
}

View File

@ -1,47 +0,0 @@
<?php
namespace nur\sery\wip\app\cli;
use nur\sery\ExitError;
use nur\sery\os\proc\Cmd;
use nur\sery\os\sh;
use nur\sery\output\msg;
use nur\sery\wip\app\app2;
use nur\sery\wip\app\RunFile;
class bg_launcher {
static function _start(array $args, Runfile $runfile): void {
$pid = pcntl_fork();
if ($pid == -1) {
# parent, impossible de forker
throw new ExitError(app2::EC_FORK_PARENT, "Unable to fork");
} elseif (!$pid) {
# child, fork ok
$runfile->wfPrepare($pid);
$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);
$cmd->fork_exec($exitcode, false);
sh::_waitpid(-$pid, $exitcode);
} finally {
$runfile->wfReaped($exitcode);
}
}
}
static function _stop(Runfile $runfile): bool {
$data = $runfile->read();
$pid = $runfile->_getCid($data);
msg::action("stop $pid");
if ($runfile->wfKill($reason)) {
msg::asuccess();
return true;
} else {
msg::afailure($reason);
return false;
}
}
}