DTux
/
dtux__moha
Archived
1
0
Fork 0

ajout rechargement auto des fichiers de conf modifié.

This commit is contained in:
Daniel Tartavel 2022-09-02 17:58:13 +02:00
parent af26fd4653
commit 0f9f3d5a98
2 changed files with 82 additions and 2 deletions

50
configWatch.php Normal file
View File

@ -0,0 +1,50 @@
<?php
$cwfd = inotify_init();
if ($cwfd !== false)
{
$watch_descriptor = inotify_add_watch($cwfd, $configDir, IN_CLOSE_WRITE);
logger(ERROR, _("configWatchInit : Inotify retourne 'false'"), __FILE__ . ":" . __LINE__);
}
function configWatchInit()
{
global $cwfd;
logger(DEBUG, _("configWatchInit : Initialise la surveillance du dossier des configurations"), __FILE__ . ":" . __LINE__);
if ($cwfd !== false)
{
$read = array($cwfd);
$write = null;
$except = null;
stream_select($read, $write, $except,0);
stream_set_blocking($cwfd, 0);
}
}
function configWach() :void
{
global $cwfd, $configDir;
if ($cwfd !== false)
{
$input = inotify_read($cwfd); // Does no block, and return false if no events are pending
if ($input !== false)
{
foreach ($input as $event)
{
logger(DEBUG, _("configWatch : inclus ") . $configDir . "/" . $event["name"], __FILE__ . ":" . __LINE__);
if (pathinfo($event["name"], PATHINFO_EXTENSION) == "php")
{
include $configDir . "/" . $event["name"];
}else
{
logger(DEBUG, _("configWatch : mauvaise extension, n'inclus pas ") . $configDir . "/" . $event["name"], __FILE__ . ":" . __LINE__);
}
}
}
}
}
?>

View File

@ -62,6 +62,8 @@ if ($testMode)
require "/etc/moha/liste_telephones.php";
if (!init()) exit(1);
// (re)démarre le démon de présence
system("systemctl restart presenceD");
// gettext
bindtextdomain("moha", "./locale");
@ -150,7 +152,7 @@ function listHooks($dir, &$hookList)
function endMoha()
{
global $testMode, $devices, $topics, $events, $nSubscribed ,$client, $logFh, $connected, $dataPath;
global $testMode, $devices, $topics, $events, $nSubscribed ,$client, $logFh, $connected, $dataPath, $cwfd, $watch_descriptor;
$x = 0;
logger(WARNING, _("moha is ending"), __FILE__ . ":" . __LINE__);
@ -167,6 +169,10 @@ function endMoha()
logger(ERROR, _("Can not store events db" ), __FILE__ . ":" . __LINE__);
}
if($testMode) file_put_contents($dataPath . "moha.devices", var_export($devices, true));
// stop la surveillance
inotify_rm_watch($cwfd, $watch_descriptor);
// Destruction de l'instance inotify
fclose($cwfd);
if ($connected)
{
$mid = $client->unsubscribe("#");
@ -217,7 +223,7 @@ require "db_functions.php";
require "config/liste_telephones.php";
require "presence.php";
require "apiserver/apiserver.php";
require "configWatch.php";
logger(DEBUG, _("Loading stored events datas from ") . $dataPath . "events.db", __FILE__ . ":" . __LINE__);
if (file_exists($dataPath . "events.db"))
@ -331,6 +337,9 @@ logger(INFO, _("Starting loop"), __FILE__ . ":" . __LINE__, false);
$oneshot = false;
logger(ALERT, _("Moha started"), __FILE__ . ":" . __LINE__);
// démarre la surveillance du dossier de configuration : /etc/moha
configWatchInit();
while (true)
{
$client->loop(); // mqtt server loop()
@ -372,6 +381,27 @@ while (true)
checkEvents();
checkTopicsAvailability();
if ($apiServerIsActive) apiServer($read);
// surveillance du dossier de configuration
if ($cwfd !== false)
{
$input = inotify_read($cwfd); // Does no block, and return false if no events are pending
if ($input !== false)
{
foreach ($input as $event)
{
logger(DEBUG, _("configWatch : inclus ") . $configDir . "/" . $event["name"], __FILE__ . ":" . __LINE__);
if (pathinfo($event["name"], PATHINFO_EXTENSION) == "php")
{
include $configDir . "/" . $event["name"];
}else
{
logger(DEBUG, _("configWatch : mauvaise extension, n'inclus pas ") . $configDir . "/" . $event["name"], __FILE__ . ":" . __LINE__);
}
}
}
}
//presence();
}