File size: 4,315 Bytes
9e15897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""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()