diff --git a/pipe/protokoll.py b/pipe/protokoll.py index e04ddfe..cf1450d 100644 --- a/pipe/protokoll.py +++ b/pipe/protokoll.py @@ -12,6 +12,7 @@ from __future__ import annotations import json import shutil +import time from datetime import datetime from pathlib import Path @@ -79,3 +80,26 @@ def archiviere_und_leere() -> Path | None: return ziel except Exception: return None + + +ARCHIV_FRIST_TAGE = 90 + + +def raeume_archiv_auf(tage: int = ARCHIV_FRIST_TAGE) -> int: + """Löscht Verlauf-Archivkopien (nicht audit.log!) älter als `tage` Tage. + + Ein neues verlauf_*.log entsteht bei jedem "Tag archivieren" - ohne + Aufräumen sammeln sich diese sonst unbegrenzt an. Wirft nie, gibt die + Anzahl gelöschter Dateien zurück.""" + if not ARCHIV_ORDNER.is_dir(): + return 0 + grenze = time.time() - tage * 86400 + geloescht = 0 + for datei in ARCHIV_ORDNER.glob("verlauf_*.log"): + try: + if datei.stat().st_mtime < grenze: + datei.unlink() + geloescht += 1 + except Exception: + continue + return geloescht diff --git a/pipe/watch.py b/pipe/watch.py index 49bca05..7fac5c0 100644 --- a/pipe/watch.py +++ b/pipe/watch.py @@ -152,10 +152,18 @@ def main(argv: list[str] | None = None) -> int: try: while True: durchlauf(eingang) - if config.LOESCHFRIST_TAGE and time.monotonic() - letzter_loeschlauf >= LOESCH_TAKT_S: - anzahl = loeschen.laeuft() - if anzahl: - print(f"🗑 {anzahl} Anruf(e) nach Aufbewahrungsfrist geloescht.") + if time.monotonic() - letzter_loeschlauf >= LOESCH_TAKT_S: + if config.LOESCHFRIST_TAGE: + anzahl = loeschen.laeuft() + if anzahl: + print(f"🗑 {anzahl} Anruf(e) nach Aufbewahrungsfrist geloescht.") + # Verlauf-Archivkopien (pipe.protokoll, "Tag archivieren") + # sammeln sich sonst unbegrenzt an - unabhaengig von + # LOESCHFRIST_TAGE, das nur Anrufe betrifft, nicht Log-Kopien. + archiv_geloescht = protokoll.raeume_archiv_auf() + if archiv_geloescht: + print(f"🗑 {archiv_geloescht} alte Verlauf-Archivkopie(n) geloescht " + f"(> {protokoll.ARCHIV_FRIST_TAGE} Tage).") letzter_loeschlauf = time.monotonic() time.sleep(TAKT_S) except KeyboardInterrupt: diff --git a/telefon/starten.sh b/telefon/starten.sh index 17e3141..caef1c8 100755 --- a/telefon/starten.sh +++ b/telefon/starten.sh @@ -18,6 +18,20 @@ if [ -f .env ]; then done < .env fi +# Rohe Prozess-Logs (stdout/stderr von watcher/leitstand/monitor + die +# eigene Ausgabe hier, wenn per launchd-Watchdog alle 5 Min aufgerufen) +# wachsen sonst unbegrenzt - anders als verlauf.log (protokoll.kuerze(), nach +# jedem Anruf) und audit.log (bewusst permanent, siehe pipe/audit.py). Ab +# 2 MB auf die letzten 2000 Zeilen kuerzen, in-place (gleiches Inode) - sicher +# fuer >>-Redirects (O_APPEND springt bei jedem Schreiben ans Dateiende, +# unabhaengig vom vorherigen Offset). +LOG_GRENZE_BYTES=2000000 +for log in telefon/autostart.log telefon/watcher.log telefon/leitstand.log telefon/monitor.log; do + if [ -f "$log" ] && [ "$(wc -c < "$log" 2>/dev/null || echo 0)" -gt "$LOG_GRENZE_BYTES" ]; then + tail -n 2000 "$log" > "$log.tmp" 2>/dev/null && cat "$log.tmp" > "$log" && rm -f "$log.tmp" + fi +done + fehler=0 melde() { printf ' %-38s %s\n' "$1" "$2"; } @@ -99,20 +113,20 @@ if [ "${status:-}" = "UP" ]; then else # nice 10: Transkription und Kategorisierung dürfen warten, ein laufendes # Telefonat nicht. Auf 16 GB RAM konkurrieren beide sonst um die Maschine. - nohup nice -n 10 python3 -u -m pipe.watch > telefon/watcher.log 2>&1 & + nohup nice -n 10 python3 -u -m pipe.watch >> telefon/watcher.log 2>&1 & echo "Verarbeitung gestartet (Log: telefon/watcher.log)" fi if pgrep -f "[p]ipe.server" >/dev/null; then echo "Leitstand laeuft bereits." else - nohup python3 -u -m pipe.server > telefon/leitstand.log 2>&1 & + nohup python3 -u -m pipe.server >> telefon/leitstand.log 2>&1 & sleep 2 - head -1 telefon/leitstand.log + tail -1 telefon/leitstand.log fi if pgrep -f "[p]ipe.monitor" >/dev/null; then echo "Stoerungswache laeuft bereits." else - nohup python3 -u -m pipe.monitor > telefon/monitor.log 2>&1 & + nohup python3 -u -m pipe.monitor >> telefon/monitor.log 2>&1 & echo "Stoerungswache gestartet (Log: telefon/monitor.log)" fi sleep 2