Archivage : verrou exclusif par boîtier, chdir dans cron_quarantaine
This commit is contained in:
+107
-89
@@ -4,7 +4,7 @@
|
||||
// (MMSI divergent, ou aucune affectation active - cf synthèse §38-39).
|
||||
// Exécuté manuellement ou via cron, après qu'un administrateur ait mis à jour
|
||||
// les données concernées (TB_Bateaux.MMSI, TB_Affectations_bateaux) :
|
||||
// php /var/www/cron_quarantaine.php
|
||||
// /usr/bin/php /var/www/cron_quarantaine.php
|
||||
//
|
||||
// Ne retente PAS automatiquement : parcourt les fichiers du répertoire de
|
||||
// quarantaine, et pour chacun, relit sa ligne TB_Archives_traitees existante
|
||||
@@ -15,6 +15,10 @@
|
||||
// modifié, nouvelle tentative au prochain lancement.
|
||||
// ============================================================================
|
||||
|
||||
// Même précaution que cron_archivage.php : certains fichiers inclus utilisent des chemins relatifs,
|
||||
// résolus par rapport au répertoire de travail - imprévisible pour une tâche planifiée.
|
||||
chdir("/var/www/html");
|
||||
|
||||
require_once __DIR__ . "/config/config.php";
|
||||
require_once __DIR__ . "/config/fonctions_bdd.php";
|
||||
require_once __DIR__ . "/fonctions_archivage.php";
|
||||
@@ -101,117 +105,131 @@ function Executer_cron_quarantaine()
|
||||
{
|
||||
$chemin = REPERTOIRE_QUARANTAINE . "/" . $nom;
|
||||
|
||||
$ligne = Lire_ligne_en_attente($db, $nom);
|
||||
if ($ligne === null)
|
||||
{
|
||||
echo " - {$nom} : aucune ligne TB_Archives_traitees en attente trouvée - incohérence, à examiner manuellement.\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
$adresse_mac = Adresse_mac_depuis_nom($nom);
|
||||
$contenu_chiffre = file_get_contents($chemin);
|
||||
|
||||
$affectation = Resoudre_affectation_active($db, $adresse_mac);
|
||||
if (!$affectation['existe'])
|
||||
$verrou = Verrouiller_boitier($adresse_mac);
|
||||
if (!$verrou['ok'])
|
||||
{
|
||||
echo " - {$nom} : toujours aucune affectation active - reste en quarantaine.\n";
|
||||
continue;
|
||||
}
|
||||
$id_bateau = $affectation['id_bateau'];
|
||||
|
||||
$dechiffrement = Decrypter_archive($contenu_chiffre);
|
||||
if (!$dechiffrement['ok'])
|
||||
{
|
||||
// Un échec de déchiffrement ne se résoudra pas en attendant (contrairement à l'affectation ou
|
||||
// au MMSI, qui dépendent d'une action administrative future) - rejet direct, comme
|
||||
// cron_archivage.php (cf synthèse §38) :
|
||||
echo " - {$nom} : ÉCHEC de déchiffrement (" . $dechiffrement['raison'] . ") - rejetée.\n";
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, null, 'rejetee', "Échec de déchiffrement : " . $dechiffrement['raison']);
|
||||
if (!is_dir(REPERTOIRE_ERREURS)) @mkdir(REPERTOIRE_ERREURS, 0750, true);
|
||||
rename($chemin, REPERTOIRE_ERREURS . "/" . $nom);
|
||||
echo " - {$nom} : " . $verrou['raison'] . " - réexamen au prochain passage.\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
$mmsi_recu = $dechiffrement['entete']['No_mmsi'] ?? null;
|
||||
$mmsi_attendu_connu = null;
|
||||
if ($mmsi_recu !== null)
|
||||
try
|
||||
{
|
||||
$verification_mmsi = Verifier_mmsi($db, $id_bateau, $mmsi_recu);
|
||||
$mmsi_attendu_connu = $verification_mmsi['attendu'];
|
||||
if ($verification_mmsi['statut'] === 'quarantaine')
|
||||
$ligne = Lire_ligne_en_attente($db, $nom);
|
||||
if ($ligne === null)
|
||||
{
|
||||
echo " - {$nom} : MMSI toujours divergent ({$mmsi_recu} / {$verification_mmsi['attendu']}) - reste en quarantaine.\n";
|
||||
echo " - {$nom} : aucune ligne TB_Archives_traitees en attente trouvée - incohérence, à examiner manuellement.\n";
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// La cause de la quarantaine est levée : traitement complet, même déroulé que cron_archivage.php :
|
||||
$origine_bdd = $dechiffrement['entete']['Origine_bdd'] ?? 'normale';
|
||||
$version_cible = $dechiffrement['entete']['Version_soft_no_package'] ?? null;
|
||||
$generation_active = Resoudre_generation_active($db, $id_bateau);
|
||||
$nouvelle_generation = Nouvelle_generation_necessaire($origine_bdd, $generation_active);
|
||||
$contenu_chiffre = file_get_contents($chemin);
|
||||
|
||||
$fichier_sql = Resoudre_fichier_sql($db, $version_cible);
|
||||
if ($fichier_sql === false)
|
||||
{
|
||||
echo " - {$nom} : version logicielle '{$version_cible}' inconnue de TB_Versions - rejetée.\n";
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, null, 'rejetee', "Version logicielle '{$version_cible}' inconnue de TB_Versions", $mmsi_recu, $mmsi_attendu_connu);
|
||||
if (!is_dir(REPERTOIRE_ERREURS)) @mkdir(REPERTOIRE_ERREURS, 0750, true);
|
||||
rename($chemin, REPERTOIRE_ERREURS . "/" . $nom);
|
||||
continue;
|
||||
}
|
||||
$contenu_fichier_sql = @file_get_contents(REPERTOIRE_VERSIONS_SQL . "/" . $fichier_sql);
|
||||
if ($contenu_fichier_sql === false)
|
||||
{
|
||||
echo " - {$nom} : fichier {$fichier_sql} introuvable sur le VPS - rejetée.\n";
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, null, 'rejetee', "Fichier {$fichier_sql} introuvable sur le VPS", $mmsi_recu, $mmsi_attendu_connu);
|
||||
if (!is_dir(REPERTOIRE_ERREURS)) @mkdir(REPERTOIRE_ERREURS, 0750, true);
|
||||
rename($chemin, REPERTOIRE_ERREURS . "/" . $nom);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($nouvelle_generation)
|
||||
{
|
||||
$zone_usine = Extraire_zone_usine($contenu_fichier_sql);
|
||||
if ($zone_usine === false)
|
||||
$affectation = Resoudre_affectation_active($db, $adresse_mac);
|
||||
if (!$affectation['existe'])
|
||||
{
|
||||
echo " - {$nom} : marqueur Factory_structure introuvable dans {$fichier_sql} - rejetée.\n";
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, null, 'rejetee', "Marqueur Factory_structure introuvable dans {$fichier_sql}", $mmsi_recu, $mmsi_attendu_connu);
|
||||
echo " - {$nom} : toujours aucune affectation active - reste en quarantaine.\n";
|
||||
continue;
|
||||
}
|
||||
$id_bateau = $affectation['id_bateau'];
|
||||
|
||||
$dechiffrement = Decrypter_archive($contenu_chiffre);
|
||||
if (!$dechiffrement['ok'])
|
||||
{
|
||||
// Un échec de déchiffrement ne se résoudra pas en attendant (contrairement à l'affectation ou
|
||||
// au MMSI, qui dépendent d'une action administrative future) - rejet direct, comme
|
||||
// cron_archivage.php (cf synthèse §38) :
|
||||
echo " - {$nom} : ÉCHEC de déchiffrement (" . $dechiffrement['raison'] . ") - rejetée.\n";
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, null, 'rejetee', "Échec de déchiffrement : " . $dechiffrement['raison']);
|
||||
if (!is_dir(REPERTOIRE_ERREURS)) @mkdir(REPERTOIRE_ERREURS, 0750, true);
|
||||
rename($chemin, REPERTOIRE_ERREURS . "/" . $nom);
|
||||
continue;
|
||||
}
|
||||
$motif = ($origine_bdd === 'factory' || $origine_bdd === 'restauration') ? $origine_bdd : 'creation_initiale';
|
||||
$resultat_generation = Creer_generation($db, $id_bateau, $motif, $zone_usine);
|
||||
if (!$resultat_generation['ok'])
|
||||
|
||||
$mmsi_recu = $dechiffrement['entete']['No_mmsi'] ?? null;
|
||||
$mmsi_attendu_connu = null;
|
||||
if ($mmsi_recu !== null)
|
||||
{
|
||||
echo " - {$nom} : échec de création de génération : " . $resultat_generation['raison'] . " - rejetée.\n";
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, null, 'rejetee', "Échec de création de génération : " . $resultat_generation['raison'], $mmsi_recu, $mmsi_attendu_connu);
|
||||
$verification_mmsi = Verifier_mmsi($db, $id_bateau, $mmsi_recu);
|
||||
$mmsi_attendu_connu = $verification_mmsi['attendu'];
|
||||
if ($verification_mmsi['statut'] === 'quarantaine')
|
||||
{
|
||||
echo " - {$nom} : MMSI toujours divergent ({$mmsi_recu} / {$verification_mmsi['attendu']}) - reste en quarantaine.\n";
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// La cause de la quarantaine est levée : traitement complet, même déroulé que cron_archivage.php :
|
||||
$origine_bdd = $dechiffrement['entete']['Origine_bdd'] ?? 'normale';
|
||||
$version_cible = $dechiffrement['entete']['Version_soft_no_package'] ?? null;
|
||||
$generation_active = Resoudre_generation_active($db, $id_bateau);
|
||||
$nouvelle_generation = Nouvelle_generation_necessaire($origine_bdd, $generation_active);
|
||||
|
||||
$fichier_sql = Resoudre_fichier_sql($db, $version_cible);
|
||||
if ($fichier_sql === false)
|
||||
{
|
||||
echo " - {$nom} : version logicielle '{$version_cible}' inconnue de TB_Versions - rejetée.\n";
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, null, 'rejetee', "Version logicielle '{$version_cible}' inconnue de TB_Versions", $mmsi_recu, $mmsi_attendu_connu);
|
||||
if (!is_dir(REPERTOIRE_ERREURS)) @mkdir(REPERTOIRE_ERREURS, 0750, true);
|
||||
rename($chemin, REPERTOIRE_ERREURS . "/" . $nom);
|
||||
continue;
|
||||
}
|
||||
$contenu_fichier_sql = @file_get_contents(REPERTOIRE_VERSIONS_SQL . "/" . $fichier_sql);
|
||||
if ($contenu_fichier_sql === false)
|
||||
{
|
||||
echo " - {$nom} : fichier {$fichier_sql} introuvable sur le VPS - rejetée.\n";
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, null, 'rejetee', "Fichier {$fichier_sql} introuvable sur le VPS", $mmsi_recu, $mmsi_attendu_connu);
|
||||
if (!is_dir(REPERTOIRE_ERREURS)) @mkdir(REPERTOIRE_ERREURS, 0750, true);
|
||||
rename($chemin, REPERTOIRE_ERREURS . "/" . $nom);
|
||||
continue;
|
||||
}
|
||||
$nom_base_cible = $resultat_generation['nom_base'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$nom_base_cible = $generation_active['nom_base'];
|
||||
}
|
||||
|
||||
$chargement = Charger_contenu_archive($nom_base_cible, $dechiffrement['sql']);
|
||||
if (!$chargement['ok'])
|
||||
{
|
||||
echo " - {$nom} : échec de chargement dans {$nom_base_cible} : " . $chargement['raison'] . " - rejetée.\n";
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, null, 'rejetee', "Échec de chargement dans {$nom_base_cible} : " . $chargement['raison'], $mmsi_recu, $mmsi_attendu_connu);
|
||||
if (!is_dir(REPERTOIRE_ERREURS)) @mkdir(REPERTOIRE_ERREURS, 0750, true);
|
||||
rename($chemin, REPERTOIRE_ERREURS . "/" . $nom);
|
||||
continue;
|
||||
}
|
||||
if ($nouvelle_generation)
|
||||
{
|
||||
$zone_usine = Extraire_zone_usine($contenu_fichier_sql);
|
||||
if ($zone_usine === false)
|
||||
{
|
||||
echo " - {$nom} : marqueur Factory_structure introuvable dans {$fichier_sql} - rejetée.\n";
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, null, 'rejetee', "Marqueur Factory_structure introuvable dans {$fichier_sql}", $mmsi_recu, $mmsi_attendu_connu);
|
||||
if (!is_dir(REPERTOIRE_ERREURS)) @mkdir(REPERTOIRE_ERREURS, 0750, true);
|
||||
rename($chemin, REPERTOIRE_ERREURS . "/" . $nom);
|
||||
continue;
|
||||
}
|
||||
$motif = ($origine_bdd === 'factory' || $origine_bdd === 'restauration') ? $origine_bdd : 'creation_initiale';
|
||||
$resultat_generation = Creer_generation($db, $id_bateau, $motif, $zone_usine);
|
||||
if (!$resultat_generation['ok'])
|
||||
{
|
||||
echo " - {$nom} : échec de création de génération : " . $resultat_generation['raison'] . " - rejetée.\n";
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, null, 'rejetee', "Échec de création de génération : " . $resultat_generation['raison'], $mmsi_recu, $mmsi_attendu_connu);
|
||||
if (!is_dir(REPERTOIRE_ERREURS)) @mkdir(REPERTOIRE_ERREURS, 0750, true);
|
||||
rename($chemin, REPERTOIRE_ERREURS . "/" . $nom);
|
||||
continue;
|
||||
}
|
||||
$nom_base_cible = $resultat_generation['nom_base'];
|
||||
}
|
||||
else
|
||||
{
|
||||
$nom_base_cible = $generation_active['nom_base'];
|
||||
}
|
||||
|
||||
$generation_pour_enregistrement = Resoudre_generation_active($db, $id_bateau);
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, $generation_pour_enregistrement['cle'], 'traitee', null, $mmsi_recu, $mmsi_attendu_connu);
|
||||
unlink($chemin);
|
||||
echo " - {$nom} : quarantaine levée, chargée dans {$nom_base_cible}.\n";
|
||||
$chargement = Charger_contenu_archive($nom_base_cible, $dechiffrement['sql']);
|
||||
if (!$chargement['ok'])
|
||||
{
|
||||
echo " - {$nom} : échec de chargement dans {$nom_base_cible} : " . $chargement['raison'] . " - rejetée.\n";
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, null, 'rejetee', "Échec de chargement dans {$nom_base_cible} : " . $chargement['raison'], $mmsi_recu, $mmsi_attendu_connu);
|
||||
if (!is_dir(REPERTOIRE_ERREURS)) @mkdir(REPERTOIRE_ERREURS, 0750, true);
|
||||
rename($chemin, REPERTOIRE_ERREURS . "/" . $nom);
|
||||
continue;
|
||||
}
|
||||
|
||||
$generation_pour_enregistrement = Resoudre_generation_active($db, $id_bateau);
|
||||
Trancher_archive_en_attente($db, $nom, $id_bateau, $generation_pour_enregistrement['cle'], 'traitee', null, $mmsi_recu, $mmsi_attendu_connu);
|
||||
unlink($chemin);
|
||||
echo " - {$nom} : quarantaine levée, chargée dans {$nom_base_cible}.\n";
|
||||
}
|
||||
finally
|
||||
{
|
||||
Liberer_boitier($verrou['poignee']);
|
||||
}
|
||||
}
|
||||
|
||||
Fermeture_bdd();
|
||||
|
||||
Reference in New Issue
Block a user