modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-09-30 10:45:54 +04:00
parent a099261b58
commit fe5ed584b4
6 changed files with 96 additions and 50 deletions

View File

@ -8,7 +8,7 @@
<sourceFolder url="file://$MODULE_DIR$/nur_src" isTestSource="false" packagePrefix="nur\" /> <sourceFolder url="file://$MODULE_DIR$/nur_src" isTestSource="false" packagePrefix="nur\" />
<sourceFolder url="file://$MODULE_DIR$/nur_tests" isTestSource="true" packagePrefix="nur\" /> <sourceFolder url="file://$MODULE_DIR$/nur_tests" isTestSource="true" packagePrefix="nur\" />
<sourceFolder url="file://$MODULE_DIR$/wip" isTestSource="false" packagePrefix="nur\sery\wip\" /> <sourceFolder url="file://$MODULE_DIR$/wip" isTestSource="false" packagePrefix="nur\sery\wip\" />
<sourceFolder url="file://$MODULE_DIR$/wip_app" isTestSource="false" packagePrefix="nur\sery\app\" /> <sourceFolder url="file://$MODULE_DIR$/wip_app" isTestSource="false" packagePrefix="nur\sery\" />
<excludeFolder url="file://$MODULE_DIR$/vendor" /> <excludeFolder url="file://$MODULE_DIR$/vendor" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />

View File

@ -50,8 +50,7 @@
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"nur\\sery\\wip\\": "wip", "nur\\sery\\wip\\": "wip",
"nur\\sery\\app\\": "wip_app", "nur\\sery\\": ["wip_app", "src"],
"nur\\sery\\": "src",
"nur\\": "nur_src" "nur\\": "nur_src"
}, },
"files": [ "files": [

View File

@ -9,25 +9,18 @@ use nur\config\ArrayConfig;
use nur\msg; use nur\msg;
use nur\os; use nur\os;
use nur\path; use nur\path;
use nur\sery\app\launcher;
use nur\sery\app\RunFile;
use nur\sery\cl;
use nur\sery\app; use nur\sery\app;
use nur\sery\app\RunFile;
use nur\sery\output\console as nconsole;
use nur\sery\output\log as nlog; use nur\sery\output\log as nlog;
use nur\sery\output\msg as nmsg; use nur\sery\output\msg as nmsg;
use nur\sery\output\console as nconsole;
use nur\sery\output\std\StdMessenger; use nur\sery\output\std\StdMessenger;
use nur\yaml;
/** /**
* Class Application: application de base * Class Application: application de base
*/ */
abstract class Application { 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 */ /** @var string répertoire du projet (celui qui contient composer.json */
const PROJDIR = null; const PROJDIR = null;
@ -46,6 +39,13 @@ abstract class Application {
*/ */
const APPCODE = null; const APPCODE = null;
/**
* @var string|null identifiant d'un groupe auquel l'application appartient.
* les applications du même groupe enregistrent leur fichiers de controle au
* même endroit $VARDIR/$APPGROUP
*/
const APPGROUP = null;
/** /**
* @var string code de l'application, utilisé pour inférer le nom de certains * @var string code de l'application, utilisé pour inférer le nom de certains
* fichiers spécifiques à l'application. * fichiers spécifiques à l'application.
@ -78,7 +78,74 @@ abstract class Application {
const USE_RUNLOCK = false; const USE_RUNLOCK = false;
/** @var bool faut-il installer le gestionnaire de signaux? */ /** @var bool faut-il installer le gestionnaire de signaux? */
const USE_SIGNAL_HANDLER = false; const INSTALL_SIGNAL_HANDLER = false;
private static function _info(string $message, int $ec=0): int {
fwrite(STDERR, "INFO: $message\n");
return $ec;
}
private static function _error(string $message, int $ec=1): int {
fwrite(STDERR, "ERROR: $message\n");
return $ec;
}
static function _manage_runfile(int &$argc, array &$argv, RunFile $runfile): void {
if ($argc <= 1 || $argv[1] !== "//") return;
array_splice($argv, 1, 1); $argc--;
$ec = 0;
switch ($argv[1] ?? "infos") {
case "help":
self::_info(<<<EOT
Valid commands:
infos
dump
reset
release
start
kill
EOT);
break;
case "infos":
case "i":
$desc = $runfile->getDesc();
if ($runfile->isRunning()) {
$actionDesc = $runfile->getActionDesc();
if ($actionDesc !== null) $actionDesc = "\n$actionDesc";
echo "$desc$actionDesc\n";
} else {
echo "$desc\n";
$ec = 1;
}
break;
case "dump":
case "d":
yaml::dump($runfile->read());
break;
case "reset":
case "z":
if (!$runfile->isRunning()) $runfile->reset();
else $ec = self::_error("cannot reset while running");
break;
case "release":
case "rl":
$runfile->release();
break;
case "start":
case "s":
array_splice($argv, 1, 1); $argc--;
return;
case "kill":
case "k":
if ($runfile->isRunning()) $runfile->wfKill();
else $ec = self::_error("not running");
break;
default:
$ec = self::_error("$argv[1]: unexpected command", app::EC_BAD_COMMAND);
}
exit($ec);
}
protected static function _app_init(): void { protected static function _app_init(): void {
config::set_fact(config::FACT_CLI_APP); config::set_fact(config::FACT_CLI_APP);
@ -108,7 +175,7 @@ abstract class Application {
if ($projdir == "/") break; if ($projdir == "/") break;
} }
app::init(static::class, self::$internal_use_app_params); app::init(static::class);
nmsg::set_messenger(new StdMessenger([ nmsg::set_messenger(new StdMessenger([
"min_level" => nmsg::DEBUG, "min_level" => nmsg::DEBUG,
])); ]));
@ -146,7 +213,7 @@ abstract class Application {
static function run(?Application $app=null): void { static function run(?Application $app=null): void {
$unlock = false; $unlock = false;
$stop = false; $stop = false;
$shutdownHandler = function () use (&$unlock, &$stop) { $shutdown = function () use (&$unlock, &$stop) {
if ($unlock) { if ($unlock) {
app::get()->getRunfile()->release(); app::get()->getRunfile()->release();
$unlock = false; $unlock = false;
@ -156,29 +223,19 @@ abstract class Application {
$stop = false; $stop = false;
} }
}; };
register_shutdown_function($shutdownHandler); register_shutdown_function($shutdown);
if (static::USE_SIGNAL_HANDLER) { app::install_signal_handler(static::INSTALL_SIGNAL_HANDLER);
$signalHandler = function(int $signo, $siginfo) {
self::exit(255);
};
pcntl_signal(SIGHUP, $signalHandler);
pcntl_signal(SIGINT, $signalHandler);
pcntl_signal(SIGQUIT, $signalHandler);
pcntl_signal(SIGTERM, $signalHandler);
}
try { try {
static::_app_init(); static::_app_init();
if (static::USE_RUNFILE) { $useRunfile = static::USE_RUNFILE;
$runfile = app::get()->getRunfile();
global $argc, $argv;
if ($argc === 2 && ($argv[1] === "--Application-release-runlock" || $argv[1] === "--ARL")) {
$runfile->release();
exit(0);
}
$useRunlock = static::USE_RUNLOCK; $useRunlock = static::USE_RUNLOCK;
if ($useRunlock && $runfile->warnIfLocked()) { if ($useRunfile) {
exit(1); $runfile = app::get()->getRunfile();
}
global $argc, $argv;
self::_manage_runfile($argc, $argv, $runfile);
if ($useRunlock && $runfile->warnIfLocked()) exit(app::EC_LOCKED);
$runfile->wfStart(); $runfile->wfStart();
$stop = true; $stop = true;
if ($useRunlock) { if ($useRunlock) {
@ -393,15 +450,4 @@ abstract class Application {
} }
abstract function main(); abstract function main();
const BGLAUNCH_SCRIPT = null;
static function runfile(): RunFile {
$callerParams = app::get()->getParams();
return app::with(static::class, $callerParams)->getRunfile();
}
static function bglaunch(?array $args=null) {
launcher::launch(static::class, cl::merge([
static::BGLAUNCH_SCRIPT,
], $args));
}
} }

View File

@ -1,6 +1,7 @@
<?php <?php
namespace nur\sery; namespace nur\sery;
use nur\cli\Application as nur_Application;
use nur\sery\app\cli\Application; use nur\sery\app\cli\Application;
use nur\sery\app\LockFile; use nur\sery\app\LockFile;
use nur\sery\app\RunFile; use nur\sery\app\RunFile;
@ -11,7 +12,8 @@ use nur\sery\php\func;
class app { class app {
private static function isa_Application($app): bool { private static function isa_Application($app): bool {
if (!is_string($app)) return false; if (!is_string($app)) return false;
return $app === Application::class || is_subclass_of($app, Application::class); return $app === Application::class || is_subclass_of($app, Application::class)
|| $app === nur_Application::class || is_subclass_of($app, nur_Application::class);
} }
private static function get_params($app): array { private static function get_params($app): array {
@ -525,9 +527,8 @@ class app {
* afficher une erreur et quitter l'application * afficher une erreur et quitter l'application
*/ */
static function check_bgapplication_enabled(bool $forceEnabled=false): void { static function check_bgapplication_enabled(bool $forceEnabled=false): void {
if (self::$bgapplication_enabled === null) return; if (self::$bgapplication_enabled === null || $forceEnabled) return;
$enabled = boolval(self::$bgapplication_enabled->invoke()); if (!self::$bgapplication_enabled->invoke()) {
if (!$forceEnabled && !$enabled) {
throw new ExitError(self::EC_DISABLED, "Planifications désactivées. La tâche n'a pas été lancée"); throw new ExitError(self::EC_DISABLED, "Planifications désactivées. La tâche n'a pas été lancée");
} }
} }