Fix self heal stale progress cleanup
This commit is contained in:
@@ -19,6 +19,15 @@ SYNC_FULL_SCAN_DONE_KEY = "iaai:state:sync_full_scan_done"
|
||||
SYNC_LISTING_CHECKPOINT_KEY = "iaai:state:sync_listing_checkpoint"
|
||||
SYNC_LISTING_FOLLOWUP_PENDING_KEY = "iaai:state:sync_listing_followup_pending"
|
||||
SIGKILL_FALLBACK = getattr(signal, "SIGKILL", signal.SIGTERM)
|
||||
TERMINAL_PROGRESS_STAGES = {
|
||||
"segment_done",
|
||||
"segment_failed",
|
||||
"segment_task_completed",
|
||||
"segment_task_failed",
|
||||
"segment_task_soft_timeout",
|
||||
"sync_done",
|
||||
"failed",
|
||||
}
|
||||
|
||||
|
||||
def _env_bool(name: str, default: bool) -> bool:
|
||||
@@ -148,13 +157,43 @@ def _has_inflight_work(redis_client: Redis, queue_name: str) -> tuple[bool, dict
|
||||
queue_len = _safe_int(redis_client.llen(queue_name), 0)
|
||||
has_lock = 1 if redis_client.get(SYNC_LISTING_LOCK_KEY) else 0
|
||||
has_task_progress = 0
|
||||
for _ in redis_client.scan_iter(match="iaai:state:task_progress:*"):
|
||||
has_task_progress = 1
|
||||
break
|
||||
progress_keys_seen = 0
|
||||
stale_progress_deleted = 0
|
||||
stale_seconds = max(180, min(_env_int("IAAI_SELF_HEAL_STALL_SECONDS", 720), 1800))
|
||||
now_ts = int(time.time())
|
||||
for key in redis_client.scan_iter(match="iaai:state:task_progress:*"):
|
||||
progress_keys_seen += 1
|
||||
if queue_len > 0 or has_lock == 1:
|
||||
has_task_progress = 1
|
||||
break
|
||||
try:
|
||||
payload = redis_client.get(key)
|
||||
if not payload:
|
||||
continue
|
||||
data = json.loads(payload)
|
||||
stage = str(data.get("stage") or "")
|
||||
ts = _safe_int(data.get("ts"), 0)
|
||||
is_terminal = stage in TERMINAL_PROGRESS_STAGES
|
||||
is_stale = ts <= 0 or now_ts - ts > stale_seconds
|
||||
if is_terminal or is_stale:
|
||||
redis_client.delete(key)
|
||||
stale_progress_deleted += 1
|
||||
continue
|
||||
has_task_progress = 1
|
||||
break
|
||||
except Exception:
|
||||
# Битые progress payload не должны держать worker в вечном false-stall цикле.
|
||||
try:
|
||||
redis_client.delete(key)
|
||||
stale_progress_deleted += 1
|
||||
except Exception:
|
||||
pass
|
||||
flags = {
|
||||
"queue_len": queue_len,
|
||||
"has_lock": has_lock,
|
||||
"has_task_progress": has_task_progress,
|
||||
"progress_keys_seen": progress_keys_seen,
|
||||
"stale_progress_deleted": stale_progress_deleted,
|
||||
}
|
||||
return (queue_len > 0 or has_lock == 1 or has_task_progress == 1), flags
|
||||
|
||||
|
||||
Reference in New Issue
Block a user