diff --git a/homelab-ai-bot/savetv_sync.py b/homelab-ai-bot/savetv_sync.py index 903cfeb1d..2fb369be1 100644 --- a/homelab-ai-bot/savetv_sync.py +++ b/homelab-ai-bot/savetv_sync.py @@ -3,7 +3,7 @@ SaveTV → NAS Sync Wartet 12h ± 0-30min nach Download auf Hetzner bevor Datei auf NAS kommt. """ -import os, json, time, random, urllib.request, urllib.parse, urllib.error, email.utils +import os, json, time, random, fcntl, urllib.request, urllib.parse, urllib.error, email.utils from datetime import datetime ZIEL = "/mnt/nas/Filme zum nachbearbeiten" @@ -11,6 +11,7 @@ BASE = "http://138.201.84.95:9443/files" API = "http://138.201.84.95:9443/api/films" CALLBACK = "http://138.201.84.95:9443/api/nas_synced" LOG = "/var/log/savetv_sync.log" +LOCKFILE = "/tmp/savetv_sync.lock" MIN_ALTER_H = 24 JITTER_MIN = 30 # ± bis zu 30 Minuten Zufall @@ -115,4 +116,10 @@ def sync(): log(f"Fertig. {kopiert} neue Filme kopiert.") if __name__ == "__main__": - sync() + with open(LOCKFILE, "w") as lock: + try: + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + except BlockingIOError: + log("Sync läuft bereits, überspringe diesen Start.") + raise SystemExit(0) + sync()