Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download api/ads_schedule_loop.py from fsanyoto/runloopable: direct link, hf CLI and curl.
- Browser
- Download file 4.32 kB
-
https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/api/ads_schedule_loop.py
- Command line
-
hf download hf://spaces/fsanyoto/runloopable/api/ads_schedule_loop.py
-
curl -L -o ads_schedule_loop.py https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/api/ads_schedule_loop.py
4.32 kB
| """Dispatch approved Ads schedule hours through C10 and the single named-write door.""" | |
| import logging | |
| import threading | |
| _log = logging.getLogger(__name__) | |
| _missing = False | |
| _thread = None | |
| _stop = None | |
| def run_tenant(tenant, runtime, *, now=None, client=None): | |
| """Claim each due local hour durably before transport and preserve uncertain outcomes without retry.""" | |
| from modules import amazon_operator as op | |
| from zoneinfo import ZoneInfo | |
| now = now or op._now() | |
| if now.utcoffset() is None: | |
| raise ValueError("The schedule clock must be timezone aware.") | |
| counts = {"sent": 0, "refused": 0, "unknown": 0} | |
| if not op.schedule_switch(tenant, st=runtime): | |
| return counts | |
| client = client if client is not None else op._ads_client() | |
| if client is None or "scheduled_change" not in getattr(client, "NAMED_WRITES", {}): | |
| return counts | |
| cursor = None | |
| while True: | |
| page = op.schedule_records(tenant, st=runtime, cursor=cursor) | |
| for saved in page["items"]: | |
| record = op.schedule_approval(tenant, saved["id"], st=runtime) | |
| if not record: | |
| continue | |
| local = now.astimezone(ZoneInfo(record["timezone"])) | |
| for rule in record["rules"]: | |
| if (rule["weekday"], rule["hour"]) != (local.weekday(), local.hour): | |
| continue | |
| claim = op.claim_schedule_run(tenant, record["id"], rule, st=runtime, now=now) | |
| if claim is None: | |
| continue | |
| # Re-read the off switches after the claim and immediately before the client door. | |
| if not op.schedule_approval(tenant, record["id"], st=runtime): | |
| op.finish_schedule_run(tenant, claim["id"], st=runtime, status="refused", | |
| cause="The schedule or App was switched off before send.", now=now) | |
| counts["refused"] += 1 | |
| continue | |
| status, result, cause = "unknown", None, None | |
| try: | |
| result = client.send_named_write(tenant, "scheduled_change", claim["sent_payload"], | |
| approval_id=record["id"]) | |
| if (isinstance(result, dict) and result.get("sent") is True | |
| and result.get("kind") == "scheduled_change" and result.get("item_id") == claim["id"] | |
| and result.get("approval_id") == record["id"] | |
| and result.get("operation") == rule["action"]): | |
| status = "sent" | |
| else: | |
| cause = "The provider returned no confirmed send receipt. This claim cannot retry." | |
| except Exception: | |
| cause = "The provider outcome is unknown. This claim cannot retry." | |
| op.finish_schedule_run(tenant, claim["id"], st=runtime, status=status, result=result, cause=cause, now=now) | |
| counts[status] += 1 | |
| cursor = page["cursor"] | |
| if cursor is None: | |
| return counts | |
| def run_once(*, for_each_tenant=None, now=None, client=None): | |
| """Admit independent per-tenant jobs without waiting for their bodies.""" | |
| global _missing | |
| if for_each_tenant is None: | |
| try: | |
| from tenant_loops import for_each_tenant | |
| except ImportError: | |
| if not _missing: | |
| _log.warning("Ads schedules unavailable: tenant_loops is not installed.") | |
| _missing = True | |
| return {} | |
| _missing = False | |
| return for_each_tenant(lambda tenant, runtime: run_tenant(tenant, runtime, now=now, client=client), | |
| name="amazon-ads-schedule", logger=_log, kind="heavy") | |
| def start(): | |
| """Start the scheduler only when process_role admits its heavy coordinator.""" | |
| global _thread, _stop | |
| import process_role | |
| if _thread is not None and _thread.is_alive(): | |
| return _thread | |
| stop = threading.Event() | |
| _stop = stop | |
| def run(): | |
| while not stop.is_set(): | |
| run_once() | |
| stop.wait(30) | |
| _thread = process_role.start("amazon-ads-schedule", "heavy", run) | |
| return _thread | |
| def stop(): | |
| if _stop is not None: | |
| _stop.set() | |