diff --git a/presquegratos.py b/presquegratos.py index 4b71a2d..7b7c6e5 100644 --- a/presquegratos.py +++ b/presquegratos.py @@ -566,6 +566,7 @@ async def run_weekly(): # -------------------- Scheduler -------------------- async def run_forever_sunday_noon(): + logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") """Run at next Sunday 12:00 Europe/Brussels, then every 7 days.""" while True: now = datetime.now(TZ) diff --git a/update_and_run.sh b/update_and_run.sh index eca19c5..5c60700 100644 --- a/update_and_run.sh +++ b/update_and_run.sh @@ -1,29 +1,58 @@ #!/bin/sh set -eu -# Arrêt propre: en sh, on utilise INT/TERM (sans "SIG") +log() { printf '%s %s\n' "[$(date -u +%FT%TZ)]" "$*"; } + stop() { - echo "[stop] shutting down..." - # Tuer proprement les 2 enfants si présents + log "stopping..." [ -n "${PID1-}" ] && kill -TERM "$PID1" 2>/dev/null || true [ -n "${PID2-}" ] && kill -TERM "$PID2" 2>/dev/null || true + [ -n "${TPID-}" ] && kill -TERM "$TPID" 2>/dev/null || true wait || true exit 0 } trap stop INT TERM -# (facultatif si tu fais un git pull ici) -# git fetch --all || true -# git reset --hard origin/main || true +cd /app +export GIT_TERMINAL_PROMPT=0 -# Lancer les 2 bots en arrière-plan -python -u post_rss_to_ghost.py & PID1=$! -python -u presquegratos.py & PID2=$! +# MAJ forcée du code à chaque (re)démarrage +if [ -d .git ]; then + i=0 + while [ $i -lt 5 ]; do + if git fetch --all --prune && git reset --hard origin/main; then + log "git updated to origin/main" + break + fi + i=$((i+1)) + log "git update failed (attempt $i/5); retrying in 10s..." + sleep 10 + done + [ $i -ge 5 ] && log "WARNING: git update failed after 5 attempts — continuing with current code" +else + log "WARNING: /app is not a git repo; skipping git update" +fi -# Attendre qu'un des deux termine; l'autre sera tué dans stop() -# /bin/sh n’a pas toujours wait -n, on fait un petit poll +# Dossiers logs +mkdir -p /var/log +: > /var/log/daily.log +: > /var/log/weekly.log + +# Lancer les 2 bots (logs non bufferisés) +python -u post_rss_to_ghost.py > /var/log/daily.log 2>&1 & PID1=$! +python -u presquegratos.py > /var/log/weekly.log 2>&1 & PID2=$! + +# Suivre les 2 fichiers de logs dans la sortie du conteneur +tail -F /var/log/daily.log /var/log/weekly.log & +TPID=$! + +# Attente portable (pas de wait -n en /bin/sh) while :; do if ! kill -0 "$PID1" 2>/dev/null; then wait "$PID1" || true; break; fi if ! kill -0 "$PID2" 2>/dev/null; then wait "$PID2" || true; break; fi sleep 1 done + +# Si un des scripts sort, on arrête le tail (le trap TERM arrêtera l'autre script) +kill -TERM "$TPID" 2>/dev/null || true +wait || true