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

48 lines
1.2 KiB
PHP

<?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;
}
}
}