File size: 4,975 Bytes
53bf512
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
"""amazon_ads_desk.py: one Amazon Ads pass for the sync desk, under the Amazon mirror writer lease."""
import contextlib
import re
import time

#: One Ads pass stops starting reports after this many seconds. A partial pass requeues itself.
ADS_PASS_SECONDS = 600
#: A pass that meets another Amazon writer waits this long before it tries again.
LEASE_RETRY_SECONDS = 600
#: "Sync finished. " plus this many characters fits the 299 character receipt.
DETAIL_LIMIT = 284

NO_CREDENTIAL = "This tenant has no stored Amazon Ads credential. Connect Amazon Ads before syncing."
NOT_RESIDENT = "This tenant's mirror is not resident on this server. Sync requires its tenant worker."
FIRST_LOAD_HELD = "The Amazon first load holds the Amazon writer lease. Run Amazon Ads sync after it ends."
LEASE_WAIT = "Another Amazon job holds the writer lease. Amazon Ads tries again in 10 minutes."
PENDING_WAIT = "Amazon Ads reports are pending. The next pass collects them."

_TABLE = re.compile(r"[a-z][a-z0-9_]{0,79}")


class Refused(Exception):
    """An authored sentence that ends the pass before any Ads request."""


class LeaseWait(Refused):
    """Another job holds the Amazon writer lease. `retry_at` is the epoch to try again."""

    def __init__(self, retry_at):
        super().__init__(LEASE_WAIT)
        self.retry_at = retry_at


def has_credential(rt):
    """True when the tenant keychain holds an `amazon_ads` entry. Fixture mode never counts."""
    from core import keychain
    try:
        return any(row.get("type") == "amazon_ads" for row in keychain.list_entries(rt))
    except Exception:                                                  # noqa: BLE001
        return False


def _resident(rt, tenant):
    """The process-global mirror must already be this tenant's file: never repoint it under readers."""
    from harness import datastore
    current = datastore.DB_PATH.resolve()
    return (rt.datastore_path.resolve() == current
            and datastore.path_for(tenant).resolve() == current)


def _log(*parts, **_kwargs):
    text = " ".join(str(part) for part in parts).strip()
    if text:
        print(f"[aios-api] amazon_ads sync desk: {text[:220]}", flush=True)


def receipt_detail(report, *, partial=False, after=None):
    """Rows per Ads table in the mirror, plus a pending sentence. At most DETAIL_LIMIT characters."""
    parts = [f"{name} {int(row.get('in_mirror') or 0)}"
             for name, row in (report.get("tables") or {}).items()
             if isinstance(row, dict) and _TABLE.fullmatch(str(name)) and row.get("state") != "not_offered"]
    tail = ""
    if partial:
        minutes = max(1, round(float(after or ADS_PASS_SECONDS) / 60))
        tail = (" More Amazon Ads reports are pending. The next pass runs in "
                f"{minutes} minute{'s' if minutes != 1 else ''}.")
    if not parts:
        return tail.strip()
    head = "Amazon Ads rows: "
    budget = DETAIL_LIMIT - len(head) - len(tail) - 1
    shown = []
    for part in parts:
        left = len(parts) - len(shown) - 1
        if len(", ".join(shown + [part]) + (f" and {left} more tables" if left else "")) > budget:
            break
        shown.append(part)
    left = len(parts) - len(shown)
    return head + ", ".join(shown) + (f" and {left} more tables" if left else "") + "." + tail


def run_pass(rt, tenant, *, log=None, now=time.time):
    """Pull due Ads reports into the tenant's Amazon mirror. -> the desk's view of the report.

    Raises `Refused` before any Ads request, and `LeaseWait` when another Amazon writer runs.
    The store persists through `amazon_store.persist_mirror` because this call lends no connection.
    """
    from harness import amazon_ads_store as ads, amazon_store as amazon
    import process_role
    if not _resident(rt, tenant):
        raise Refused(NOT_RESIDENT)
    if not has_credential(rt):
        raise Refused(NO_CREDENTIAL)
    if amazon.first_load_job_live(rt):
        raise Refused(FIRST_LOAD_HELD)
    with contextlib.ExitStack() as stack:
        try:
            stack.enter_context(process_role.connector_writer(rt, "amazon"))
        except RuntimeError as exc:
            if "holds the amazon writer lease" not in str(exc):
                raise
            raise LeaseWait(now() + LEASE_RETRY_SECONDS) from None
        report = ads.sync(rt.key, rt=rt, log=log or _log, max_seconds=ADS_PASS_SECONDS)
    waits = list(report.get("waits") or [])
    problems = [problem for problem in report.get("problems") or [] if problem not in waits]
    partial = bool(report.get("partial")) and not problems
    after = report.get("continueAfterSeconds")
    if not (isinstance(after, (int, float)) and not isinstance(after, bool) and after > 0):
        after = ADS_PASS_SECONDS
    return {"report": report, "problems": problems, "partial": partial,
            "continueAt": now() + float(after) if partial else None,
            "detail": receipt_detail(report, partial=partial, after=after)}