Build complete runtime segments

This commit is contained in:
qananasikq
2026-08-19 17:11:00 +03:00
parent 8297742a64
commit 7068b7e8fa
5 changed files with 1054 additions and 232 deletions
+24 -5
View File
@@ -111,7 +111,12 @@ def _reset_bootstrap_checkpoint_for_db_idle(redis_client: Redis) -> None:
pipe.execute()
def _has_inflight_work(redis_client: Redis, queue_name: str) -> tuple[bool, dict[str, int]]:
def _has_inflight_work(
redis_client: Redis,
queue_name: str,
*,
progress_max_age_seconds: int = 900,
) -> tuple[bool, dict[str, int]]:
"""Есть ли признаки активной/зависшей работы, даже если очередь пуста."""
queue_len = _safe_int(redis_client.llen(queue_name), 0)
has_segment_lock = 0
@@ -119,9 +124,19 @@ def _has_inflight_work(redis_client: Redis, queue_name: str) -> tuple[bool, dict
has_segment_lock = 1
break
has_task_progress = 0
for _ in redis_client.scan_iter(match="mobilede:state:task_progress:*"):
has_task_progress = 1
break
now_ts = int(time.time())
max_age = max(60, int(progress_max_age_seconds))
for key in redis_client.scan_iter(match="mobilede:state:task_progress:*"):
try:
payload = redis_client.get(key)
data = json.loads(payload) if payload else {}
progress_ts = _safe_int(data.get("ts"), 0)
if progress_ts > 0 and now_ts - progress_ts <= max_age:
has_task_progress = 1
break
redis_client.delete(key)
except Exception:
redis_client.delete(key)
flags = {
"queue_len": queue_len,
"has_segment_lock": has_segment_lock,
@@ -193,7 +208,11 @@ def main() -> None:
redis_client = _get_redis()
redis_client.ping()
has_inflight, inflight = _has_inflight_work(redis_client, queue_name)
has_inflight, inflight = _has_inflight_work(
redis_client,
queue_name,
progress_max_age_seconds=stall_seconds,
)
if not has_inflight:
time.sleep(check_interval)
continue