Files
ms1inscription-v5/auto_chronotrack_sync.php

73 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* MSIN-4328 — Cron sync auto ChronoTrack (différentiel).
*
* Cron recommandé (toutes les minutes) — IMPORTANT : -k si certificat preprod invalide :
* /usr/bin/curl -sk https://VOTRE-DOMAINE/auto_chronotrack_sync.php > /dev/null 2>&1
*
* Preuve dexécution : fichier log/chronotrack_cron_last.txt (mtime = dernier appel).
*/
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/log/php_log.txt');
@set_time_limit(300);
$strLogDir = __DIR__ . '/log';
if (!is_dir($strLogDir)) {
@mkdir($strLogDir, 0755, true);
}
$strHeartbeat = $strLogDir . '/chronotrack_cron_last.txt';
/**
* Écrit une preuve locale que le script a été appelé (cron ou navigateur).
*/
function fxChronotrackCronHeartbeat($strHeartbeat, $strLine) {
$strPayload = $strLine . "\n"
. 'host=' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . "\n"
. 'remote=' . (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '') . "\n"
. 'ua=' . (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '') . "\n";
@file_put_contents($strHeartbeat, $strPayload, LOCK_EX);
}
// Preuve immédiate (même si lock / erreur après)
fxChronotrackCronHeartbeat($strHeartbeat, 'HIT ' . date('Y-m-d H:i:s'));
$strLockPath = rtrim(sys_get_temp_dir(), '/\\') . DIRECTORY_SEPARATOR . 'ms1_chronotrack_auto_sync.lock';
$fpLock = @fopen($strLockPath, 'c');
if ($fpLock === false || !flock($fpLock, LOCK_EX | LOCK_NB)) {
if (is_resource($fpLock)) {
fclose($fpLock);
}
fxChronotrackCronHeartbeat($strHeartbeat, 'BUSY ' . date('Y-m-d H:i:s'));
header('Content-Type: text/plain; charset=utf-8');
echo 'busy';
exit;
}
session_start();
try {
require_once __DIR__ . '/php/inc_fonctions.php';
require_once __DIR__ . '/php/chronotrack_api/inc_bootstrap.php';
$arrResult = fxChronotrackApiSyncRunAutoCron();
$strLine = 'auto_chronotrack_sync [' . date('Y-m-d H:i:s') . '] '
. ($arrResult['message'] ?? 'done');
error_log($strLine);
fxChronotrackCronHeartbeat($strHeartbeat, 'OK ' . $strLine);
header('Content-Type: text/plain; charset=utf-8');
echo $strLine . "\n";
} catch (Throwable $ex) {
$strErr = 'ERROR ' . date('Y-m-d H:i:s') . ' ' . $ex->getMessage();
error_log('auto_chronotrack_sync ' . $strErr);
fxChronotrackCronHeartbeat($strHeartbeat, $strErr);
header('Content-Type: text/plain; charset=utf-8');
echo $strErr . "\n";
}
flock($fpLock, LOCK_UN);
fclose($fpLock);