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