"""Conservative operational bounds shared by configuration and runtime consumers.""" import math from threading import TIMEOUT_MAX MAX_OPERATIONAL_SECONDS = min(TIMEOUT_MAX, (2**31 - 1) / 1000.0) MIN_OPERATIONAL_SECONDS = 1e-6 # PostgreSQL interval/timestamp microsecond precision. def validate_duration(value): valid = False if type(value) in (int, float): try: valid = math.isfinite(value) and MIN_OPERATIONAL_SECONDS <= value <= MAX_OPERATIONAL_SECONDS except OverflowError: pass if not valid: raise ValueError("Operational duration is not representable by runtime or PostgreSQL") return float(value) def validate_worker_durations(settings): for name in ("poll_seconds", "heartbeat_seconds", "lease_seconds", "retry_base_seconds", "retry_cap_seconds"): validate_duration(getattr(settings, name)) if settings.heartbeat_seconds >= settings.lease_seconds: raise ValueError("Heartbeat must be shorter than lease") if settings.retry_base_seconds > settings.retry_cap_seconds: raise ValueError("Retry base must not exceed retry cap")