modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-06-13 17:19:58 +04:00
parent 16376a473a
commit a09f3a0a2b
7 changed files with 218 additions and 111 deletions

View File

@ -1,13 +1,26 @@
#!/usr/bin/php
<?php
require $_composer_autoload_path?? __DIR__.'/../vendor/autoload.php';
$internalUse = $argv[1] ?? null;
if ($internalUse !== "--internal-use") exit("Wrong args");
$paramsfile = $argv[2] ?? null;
if (!file_exists($paramsfile)) exit("bad params file");
$argc -= 2;
$argv = array_merge(
array_slice($argv, 0, 1),
array_slice($argv, 3),
);
$app_params = unserialize(file_get_contents($paramsfile));
require $app_params["vendor"]["autoload"];
use nur\cli\Application;
use nur\sery\wip\app\app;
use nur\sery\app\launcher;
use nur\yaml;
Application::run(new class extends Application {
class _LaunchApp extends Application {
const NAME = "_launch";
const ACTION_INFOS = 0, ACTION_START = 1, ACTION_STOP = 2;
const ARGS = [
@ -44,8 +57,7 @@ Application::run(new class extends Application {
self::die("Cette application ne supporte pas l'usage de runfile");
}
$app = app::with($appClass);
$runfile = $app->getRunfile();
$runfile = app::with($appClass, self::$internal_use_app_params)->getRunfile();
switch ($this->action) {
case self::ACTION_START:
launcher::_start($args, $runfile);
@ -58,4 +70,6 @@ Application::run(new class extends Application {
break;
}
}
});
}
_LaunchApp::internal_use_set_app_params($app_params);
_LaunchApp::run();

View File

@ -19,9 +19,20 @@ use nur\sery\output\std\StdMessenger;
* Class Application: application de base
*/
abstract class Application {
protected static ?array $internal_use_app_params = null;
static function internal_use_set_app_params($params) {
self::$internal_use_app_params = $params;
}
/** @var string répertoire du projet (celui qui contient composer.json */
const PROJDIR = null;
/**
* @var array répertoires vendor exprimés relativement à PROJDIR
*/
const VENDOR = null;
/**
* @var string code du projet, utilisé pour dériver le noms de certains des
* paramètres extraits de l'environnement, e.g XXX_DATADIR si le projet a pour
@ -85,7 +96,7 @@ abstract class Application {
if ($projdir == "/") break;
}
app::init(static::class);
app::init(static::class, self::$internal_use_app_params);
nmsg::set_messenger(new StdMessenger([
"min_level" => nmsg::DEBUG,
]));
@ -124,9 +135,8 @@ abstract class Application {
$unlock = false;
$stop = false;
register_shutdown_function(function () use (&$unlock, &$stop) {
$runfile = app::get()->getRunfile();
if ($unlock) $runfile->release();
if ($stop) $runfile->wfStop();
if ($unlock) app::get()->getRunfile()->release();
if ($stop) app::get()->getRunfile()->wfStop();
});
try {
static::_app_init();

View File

@ -175,14 +175,14 @@ class RunFile {
if ($data["date_stop"] !== null) return false;
if (!posix_kill($data["pid"], 0)) {
switch (posix_get_last_error()) {
case PCNTL_ESRCH:
# process inexistant
return false;
case PCNTL_EPERM:
case 1: #PCNTL_EPERM:
# process auquel on n'a pas accès?! est-ce un autre process qui a
# réutilisé le PID?
return false;
case PCNTL_EINVAL:
case 3: #PCNTL_ESRCH:
# process inexistant
return false;
case 22: #PCNTL_EINVAL:
# ne devrait pas se produire
return false;
}
@ -281,14 +281,14 @@ class RunFile {
$pid ??= $data["pid"];
if (!posix_kill($pid, 0)) {
switch (posix_get_last_error()) {
case PCNTL_ESRCH:
# process inexistant
return true;
case PCNTL_EPERM:
case 1: #PCNTL_EPERM:
# process auquel on n'a pas accès?! est-ce un autre process qui a
# réutilisé le PID?
return false;
case PCNTL_EINVAL:
case 3: #PCNTL_ESRCH:
# process inexistant
return true;
case 22: #PCNTL_EINVAL:
# ne devrait pas se produire
return false;
}

View File

@ -1,21 +1,32 @@
<?php
namespace nur\sery\app;
use nur\sery\file\TmpfileWriter;
use nur\sery\os\proc\Cmd;
use nur\sery\output\msg;
use nur\sery\StateException;
use nur\sery\wip\app\app;
class launcher {
static function launch(string $appClass, ...$args): int {
global $_composer_bin_dir;
if (isset($_composer_bin_dir)) {
$launch_php = "$_composer_bin_dir/_launch.php";
} else {
$app = app::get();
$vendorBindir = $app->getVendorbindir();
$launch_php = "$vendorBindir/_launch.php";
if (!file_exists($launch_php)) {
$launch_php = __DIR__."/../../lib/_launch.php";
}
$cmd = new Cmd([$launch_php, $appClass, ...$args]);
//$cmd->addRedir("null");
$tmpfile = new TmpfileWriter();
$tmpfile->serialize($app->getParams());
$cmd = new Cmd([
$launch_php,
"--internal-use", $tmpfile->getFile(),
$appClass, ...$args,
]);
$cmd->addRedir("null");
$cmd->passthru($exitcode);
$tmpfile->close();
return $exitcode;
}
@ -37,7 +48,6 @@ class launcher {
# puis lancer la commande
$cmd = new Cmd($args);
#XXX fichier de log?
#XXX charger /g/init.env
$cmd->addSource("/g/init.env");
$cmd->addRedir("null");
$cmd->fork_exec($exitcode);

View File

@ -79,7 +79,7 @@ abstract class AbstractCmd implements ICmd {
function addSource(?string $source, bool $onlyIfExists=true): void {
if ($source === null) return;
if (!$onlyIfExists || file_exists($source)) {
$source = implode(" ", ["source", sh::quote($source)]);
$source = implode(" ", [".", sh::quote($source)]);
$this->sources[] = $source;
}
}
@ -200,7 +200,9 @@ abstract class AbstractCmd implements ICmd {
* composition de plusieurs commandes
*/
protected function useExec(): bool {
return $this->vars === null && count($this->cmds) == 1;
return $this->sources === null
&& $this->vars === null
&& count($this->cmds) == 1;
}
function fork_exec(int &$retcode=null): bool {

View File

@ -2,6 +2,7 @@
namespace nur\sery\os;
use nur\sery\cl;
use nur\sery\StateException;
use RuntimeException;
class sh {
@ -144,19 +145,19 @@ class sh {
$pid = pcntl_fork();
if ($pid == -1) {
// parent, impossible de forker
throw new RuntimeException("unable to fork");
throw new StateException("unable to fork");
} elseif ($pid) {
// parent, fork ok
pcntl_waitpid($pid, $status);
if (pcntl_wifexited($status)) {
$retcode = pcntl_wexitstatus($status);
} else {
$retcode = 127;
}
if (pcntl_wifexited($status)) $retcode = pcntl_wexitstatus($status);
else $retcode = 127;
return $retcode == 0;
}
// child, fork ok
pcntl_exec("/bin/sh", ["-c", $cmd]);
$shell = "/bin/bash";
if (!file_exists($shell)) $shell = "/bin/sh";
$shell = "/bin/sh";
pcntl_exec($shell, ["-c", $cmd]);
return false;
}

View File

@ -5,67 +5,109 @@ namespace nur\sery\wip\app;
use nur\cli\Application;
use nur\sery\app\LockFile;
use nur\sery\app\RunFile;
use nur\sery\cl;
use nur\sery\os\path;
use nur\sery\os\sh;
use nur\sery\str;
use nur\sery\ValueException;
class app {
/**
* @var array répertoires vendor exprimés relativement à PROJDIR
*/
const DEFAULT_VENDOR = [
"bindir" => "vendor/bin",
"autoload" => "vendor/autoload.php",
];
private static function isa_Application($app): bool {
if (!is_string($app)) return false;
return $app === Application::class || is_subclass_of($app, Application::class);
}
private static function verifix_name(string &$name): void {
# si $name est une classe, enlever le package et normaliser
$name = preg_replace('/.*\\\\/', "", $name);
$name = str::without_suffix("-app", str::camel2us($name, false, "-"));
}
/** @param Application|string */
static function with($app): self {
static function with($app, ?array $internal_use_params=null): self {
if ($app instanceof Application) {
$projdir = $app::PROJDIR;
$appcode = $app::APPCODE;
$name = $app::NAME;
$title = $app::TITLE;
$datadir = $app::DATADIR;
$etcdir = $app::ETCDIR;
$vardir = $app::VARDIR;
$logdir = $app::LOGDIR;
$params = [
"projdir" => $app::PROJDIR,
"vendor" => $app::VENDOR,
"appcode" => $app::APPCODE,
"apptype" => "cli",
"name" => $app::NAME,
"title" => $app::TITLE,
"datadir" => $app::DATADIR,
"etcdir" => $app::ETCDIR,
"vardir" => $app::VARDIR,
"logdir" => $app::LOGDIR,
];
} elseif (self::isa_Application($app)) {
$projdir = constant("$app::PROJDIR");
$appcode = constant("$app::APPCODE");
$name = constant("$app::NAME");
$title = constant("$app::TITLE");
$datadir = constant("$app::DATADIR");
$etcdir = constant("$app::ETCDIR");
$vardir = constant("$app::VARDIR");
$logdir = constant("$app::LOGDIR");
$params = [
"projdir" => constant("$app::PROJDIR"),
"vendor" => constant("$app::VENDOR"),
"appcode" => constant("$app::APPCODE"),
"apptype" => "cli",
"name" => constant("$app::NAME"),
"title" => constant("$app::TITLE"),
"datadir" => constant("$app::DATADIR"),
"etcdir" => constant("$app::ETCDIR"),
"vardir" => constant("$app::VARDIR"),
"logdir" => constant("$app::LOGDIR"),
];
} elseif (is_array($app)) {
return new static($app);
$params = $app;
} else {
throw ValueException::invalid_type($app, Application::class);
}
return new static([
"projdir" => $projdir,
"appcode" => $appcode,
if ($internal_use_params !== null) {
$params = array_merge($internal_use_params, cl::selectm($params, [
"name",
"title",
], [
"apptype" => "cli",
"name" => $name,
"title" => $title,
"datadir" => $datadir,
"etcdir" => $etcdir,
"vardir" => $vardir,
"logdir" => $logdir,
]);
]));
self::verifix_name($params["name"]);
}
return new static($params, $internal_use_params !== null);
}
protected static ?app $app = null;
static function init($app): void {
self::$app = static::with($app);
static function init($app, ?array $internal_use_params=null): void {
self::$app = static::with($app, $internal_use_params);
}
static function get(): self {
return self::$app ??= new self(null);
}
function __construct(?array $params) {
function __construct(?array $params, bool $internalUse_asis=false) {
if ($internalUse_asis) {
[
"projdir" => $this->projdir,
"vendor" => $this->vendor,
"appcode" => $this->appcode,
"apptype" => $this->apptype,
"name" => $this->name,
"title" => $this->title,
"profile" => $this->profile,
"cwd" => $this->cwd,
"datadir" => $this->datadir,
"etcdir" => $this->etcdir,
"vardir" => $this->vardir,
"logdir" => $this->logdir,
] = $params;
} else {
$this->projdir = $projdir = path::abspath($params["projdir"] ?? ".");
$vendor = $params["vendor"] ?? self::DEFAULT_VENDOR;
$vendor["bindir"] = path::reljoin($projdir, $vendor["bindir"]);
$vendor["autoload"] = path::reljoin($projdir, $vendor["autoload"]);
$this->vendor = $vendor;
$this->appcode = $appcode = $params["appcode"] ?? "app";
$this->apptype = $apptype = $params["apptype"] ?? "cli";
$name = $params["name"] ?? null;
@ -73,7 +115,6 @@ class app {
$name = $appcode;
} else {
# si $name est une classe, enlever le package et normaliser
$name = $params["name"] ?? $appcode;
$name = preg_replace('/.*\\\\/', "", $name);
$name = str::without_suffix("-app", str::camel2us($name, false, "-"));
}
@ -112,6 +153,25 @@ class app {
if ($logdir === null) $logdir = "log";
$this->logdir = $logdir = path::reljoin($datadir, $logdir);
}
}
/** recréer le tableau des paramètres */
function getParams(): array {
return [
"projdir" => $this->projdir,
"vendor" => $this->vendor,
"appcode" => $this->appcode,
"apptype" => $this->apptype,
"name" => $this->name,
"title" => $this->title,
"profile" => $this->profile,
"cwd" => $this->cwd,
"datadir" => $this->datadir,
"etcdir" => $this->etcdir,
"vardir" => $this->vardir,
"logdir" => $this->logdir,
];
}
protected string $projdir;
@ -119,6 +179,16 @@ class app {
return $this->projdir;
}
protected array $vendor;
function getVendorBindir(): string {
return $this->vendor["bindir"];
}
function getVendorAutoload(): string {
return $this->vendor["autoload"];
}
protected string $appcode;
function getAppcode(): string {