import os from lyra_worker.config import get_config _TRUE = {"1", "true", "yes", "on"} def press_paused(config: dict[str, str]) -> bool: """True when the whole press is paused — the worker stops claiming new jobs.""" return str(config.get("floor.paused", "")).strip().lower() in _TRUE def job_delay_seconds(config: dict[str, str]) -> float: """Seconds to wait between finishing one download and claiming the next. Invalid or absent -> 0.0 (back-to-back). Negatives are clamped to 0.""" try: return max(0.0, float(int(config.get("floor.jobDelaySeconds", "") or 0))) except (TypeError, ValueError): return 0.0 def kill_requested(config: dict[str, str]) -> bool: """True when a force-stop of the in-flight download has been requested.""" return str(config.get("floor.killRequested", "")).strip().lower() in _TRUE def consume_kill_switch(conn, exit_fn=os._exit) -> bool: """If a force-stop is pending, clear the flag and terminate the process (default os._exit) so the in-flight download is aborted. The container restart policy brings the worker back; startup clears staging and requeues the aborted job. Returns whether it fired. exit_fn is injectable for tests.""" config = get_config(conn) if not kill_requested(config): return False with conn.cursor() as cur: cur.execute( 'UPDATE "Config" SET value = \'false\', "updatedAt" = now() WHERE key = %s', ("floor.killRequested",), ) conn.commit() exit_fn(0) return True