mailu-sync: cap wait in listener
This commit is contained in:
parent
d5a19ca9c3
commit
bed3563ae6
@ -1,5 +1,6 @@
|
||||
import http.server
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import threading
|
||||
|
||||
@ -7,15 +8,17 @@ from time import time
|
||||
|
||||
# Simple debounce to avoid hammering on bursts
|
||||
MIN_INTERVAL_SECONDS = 10
|
||||
WAIT_TIMEOUT_SECONDS = float(os.environ.get("MAILU_SYNC_WAIT_TIMEOUT_SEC", "20"))
|
||||
last_run = 0.0
|
||||
lock = threading.Lock()
|
||||
sync_done = threading.Event()
|
||||
sync_done.set()
|
||||
sync_running = False
|
||||
last_rc = None
|
||||
|
||||
|
||||
def _run_sync_blocking() -> int:
|
||||
global last_run, sync_running
|
||||
global last_run, sync_running, last_rc
|
||||
with lock:
|
||||
if sync_running:
|
||||
return 0
|
||||
@ -27,6 +30,7 @@ def _run_sync_blocking() -> int:
|
||||
proc = subprocess.run(["python", "/app/sync.py"], check=False)
|
||||
rc = int(proc.returncode)
|
||||
print(f"mailu-sync-listener: sync completed rc={rc}", flush=True)
|
||||
last_rc = rc
|
||||
return rc
|
||||
finally:
|
||||
with lock:
|
||||
@ -66,16 +70,20 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||
if wait:
|
||||
with lock:
|
||||
already_running = sync_running
|
||||
if already_running:
|
||||
sync_done.wait(timeout=120)
|
||||
with lock:
|
||||
still_running = sync_running
|
||||
self.send_response(200 if not still_running else 503)
|
||||
self.end_headers()
|
||||
return
|
||||
|
||||
rc = _run_sync_blocking()
|
||||
self.send_response(200 if rc == 0 else 500)
|
||||
if not already_running:
|
||||
_trigger_sync_async()
|
||||
|
||||
sync_done.wait(timeout=WAIT_TIMEOUT_SECONDS)
|
||||
with lock:
|
||||
still_running = sync_running
|
||||
rc = last_rc
|
||||
|
||||
if still_running:
|
||||
# Avoid blocking callers while a sync is in flight.
|
||||
self.send_response(200)
|
||||
else:
|
||||
self.send_response(200 if rc == 0 else 500)
|
||||
self.end_headers()
|
||||
return
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user