103 lines
5.2 KiB
PHP
103 lines
5.2 KiB
PHP
<?php
|
|
// ============================================================================
|
|
// traiter_demandes_archivage.php - Traitement des demandes d'archive déposées par le portail (synthèse §50, étape 3d).
|
|
// Exécuté NATIVEMENT sur le VPS par ship2net-demandes.service, lui-même déclenché par ship2net-demandes.path dès
|
|
// qu'un fichier apparaît dans /var/www/demandes/a_traiter (le portail, dans son conteneur, ne peut pas lancer le
|
|
// traitement lui-même : il le demande en y déposant un fichier nommé d'après le numéro du bateau).
|
|
//
|
|
// Pour chaque demande (fichier consommé AVANT traitement, pour ne jamais boucler) :
|
|
// 1. demande au CM5 du bateau de produire une archive IMMÉDIATEMENT (POST /archives/demande sur son API, via
|
|
// NetBird) - l'Appli met TB_Systeme.Demande_sauvegarde_bdd à profit et génère l'archive dans la seconde ;
|
|
// 2. attend que cette nouvelle archive apparaisse dans la liste de l'API (au plus DELAI_ATTENTE_ARCHIVE secondes) ;
|
|
// 3. lance le traitement des archives de ce seul bateau (cron_archivage.php --bateau=N).
|
|
// La page du portail, qui interroge bateau_etat.php toutes les 3 s après sa demande, se met à jour dès le chargement.
|
|
// ============================================================================
|
|
|
|
chdir("/var/www/html");
|
|
require_once __DIR__ . "/config/config.php";
|
|
require_once __DIR__ . "/config/fonctions_bdd.php";
|
|
|
|
const REPERTOIRE_DEMANDES_A_TRAITER = "/var/www/demandes/a_traiter";
|
|
const PORT_API_CM5_DEMANDE = 8080;
|
|
const DELAI_ATTENTE_ARCHIVE = 30; // secondes
|
|
const DELAI_FIN_ECRITURE_ARCHIVE = 2; // secondes laissées au CM5 pour finir d'écrire l'archive une fois listée
|
|
|
|
function Appel_cm5($adresse_netbird, $methode, $chemin)
|
|
{
|
|
$ch = curl_init("http://".$adresse_netbird.":".PORT_API_CM5_DEMANDE.$chemin);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $methode);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
$reponse = curl_exec($ch);
|
|
$code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
|
curl_close($ch);
|
|
if ($reponse === false || $code !== 200) return null;
|
|
$donnees = json_decode($reponse, true);
|
|
return (is_array($donnees) && ($donnees['status'] ?? '') === 'ok') ? $donnees : null;
|
|
}
|
|
|
|
function Noms_archives($adresse_netbird)
|
|
{
|
|
$donnees = Appel_cm5($adresse_netbird, 'GET', '/archives');
|
|
if ($donnees === null) return null;
|
|
return array_map(fn($a) => $a['nom'], $donnees['archives'] ?? array());
|
|
}
|
|
|
|
function Traiter_demande($id_bateau)
|
|
{
|
|
Ouverture_bdd("127.0.0.1:3307", BDD_USER, BDD_PASSWORD, BDD_DATABASE);
|
|
$stmt = $_SESSION['db']->prepare("SELECT C.Adresse_netbird FROM TB_Affectations_bateaux A JOIN TB_Cartes C ON C.Adresse_mac = A.Adresse_mac
|
|
WHERE A.ID_Bateau=? AND A.Date_fin IS NULL");
|
|
$stmt->bind_param('i', $id_bateau);
|
|
$stmt->execute();
|
|
$ligne = $stmt->get_result()->fetch_row();
|
|
$stmt->close();
|
|
Fermeture_bdd();
|
|
|
|
$adresse = $ligne ? trim((string)$ligne[0]) : '';
|
|
if (filter_var($adresse, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false)
|
|
{
|
|
echo "Bateau {$id_bateau} : aucun module joignable affecté - demande ignorée.\n";
|
|
return;
|
|
}
|
|
|
|
// 1. Archive immédiate (si le CM5 ne connaît pas encore cette route - ancienne version de l'API -, on se contente de
|
|
// rapatrier ce qui est déjà en attente) :
|
|
$avant = Noms_archives($adresse);
|
|
if ($avant === null) { echo "Bateau {$id_bateau} : CM5 injoignable ({$adresse}) - demande abandonnée.\n"; return; }
|
|
if (Appel_cm5($adresse, 'POST', '/archives/demande') === null)
|
|
{
|
|
echo "Bateau {$id_bateau} : demande d'archive immédiate refusée ou non prise en charge - rapatriement de l'existant.\n";
|
|
}
|
|
else
|
|
{
|
|
// 2. Attente de la nouvelle archive :
|
|
$nouvelle = false;
|
|
for ($attente = 0; $attente < DELAI_ATTENTE_ARCHIVE && !$nouvelle; $attente += 2)
|
|
{
|
|
sleep(2);
|
|
$apres = Noms_archives($adresse);
|
|
if ($apres !== null && count(array_diff($apres, $avant)) > 0) $nouvelle = true;
|
|
}
|
|
echo "Bateau {$id_bateau} : ".($nouvelle ? "nouvelle archive produite." : "aucune nouvelle archive après ".DELAI_ATTENTE_ARCHIVE." s - rapatriement de l'existant.")."\n";
|
|
if ($nouvelle) sleep(DELAI_FIN_ECRITURE_ARCHIVE);
|
|
}
|
|
|
|
// 3. Traitement des archives de ce seul bateau :
|
|
passthru("/usr/bin/php ".escapeshellarg(__DIR__."/cron_archivage.php")." --bateau=".(int)$id_bateau);
|
|
}
|
|
|
|
if (!is_dir(REPERTOIRE_DEMANDES_A_TRAITER)) { echo "Répertoire ".REPERTOIRE_DEMANDES_A_TRAITER." absent.\n"; exit(1); }
|
|
|
|
// Toutes les demandes présentes (plusieurs bateaux possibles), chacune consommée AVANT son traitement :
|
|
foreach (scandir(REPERTOIRE_DEMANDES_A_TRAITER) as $nom)
|
|
{
|
|
if ($nom === '.' || $nom === '..') continue;
|
|
$chemin = REPERTOIRE_DEMANDES_A_TRAITER."/".$nom;
|
|
@unlink($chemin);
|
|
if (preg_match('/^[1-9][0-9]{0,9}$/', $nom) !== 1) { echo "Demande '{$nom}' invalide - supprimée.\n"; continue; }
|
|
try { Traiter_demande((int)$nom); }
|
|
catch (Throwable $e) { echo "Bateau {$nom} : erreur - ".$e->getMessage()."\n"; } // N'empêche pas les demandes suivantes
|
|
}
|