nur-sery/wip/app/cli/bg_launcher.php

48 lines
1.2 KiB
PHP
Raw Normal View History

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
static function _stop(Runfile $runfile): bool {
$data = $runfile->read();
$pid = $runfile->_getCid($data);
2024-09-26 19:35:24 +04:00
msg::action("stop $pid");
if ($runfile->wfKill($reason)) {
msg::asuccess();
return true;
} else {
msg::afailure($reason);
return false;
2024-09-26 10:54:43 +04:00
}
}
2024-09-26 09:07:44 +04:00
}