-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynctrigger.py
More file actions
73 lines (63 loc) · 2.57 KB
/
synctrigger.py
File metadata and controls
73 lines (63 loc) · 2.57 KB
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
#!/usr/bin/env python3
"""
synctrigger.py — Reliable scheduling heartbeat for apAutoSync
──────────────────────────────────────────────────────────────
Cloud Scheduler is blocked on this project (requires project Owner to enable
cloudscheduler.googleapis.com). This script runs alongside the reference agents
and calls apAutoSync every 30 minutes so campaign participation data stays fresh.
Usage:
python3 synctrigger.py &
The process logs one line per sync run to stdout. PIDs and status are visible
in the workspace process table alongside the 7 reference agent processes.
"""
import time
import logging
import os
try:
import requests
except ImportError:
import subprocess, sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests", "-q"])
import requests
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [synctrigger] %(levelname)s %(message)s",
datefmt="%Y-%m-%dT%H:%M:%SZ",
)
SYNC_URL = "https://us-central1-campaign-gamification.cloudfunctions.net/apAutoSync"
SYNC_SECRET = os.environ.get("SYNC_SCHEDULER_SECRET", "ap-sync-2026")
INTERVAL_SEC = 1800 # 30 minutes — matches intended Cloud Scheduler cadence
def trigger_sync() -> None:
try:
r = requests.post(
SYNC_URL,
headers={
"X-Scheduler-Secret": SYNC_SECRET,
"Content-Type": "application/json",
"User-Agent": "Numbers-SyncTrigger/1.0",
},
json={},
timeout=600, # apAutoSync has a 540s function timeout
)
if r.ok:
data = r.json()
logging.info(
"sync ok — new=%d dups=%d agents_excluded=%d pages=%d capped=%s",
data.get("new_entries", 0),
data.get("duplicates_skipped", 0),
data.get("agents_excluded", 0),
data.get("pages_read", 0),
data.get("capped_early", False),
)
else:
logging.warning("sync http %d: %s", r.status_code, r.text[:300])
except requests.exceptions.Timeout:
logging.warning("sync timed out after 600s (function may still be running)")
except Exception as exc:
logging.error("sync error: %s", exc)
if __name__ == "__main__":
logging.info("synctrigger started — interval=%ds url=%s", INTERVAL_SEC, SYNC_URL)
while True:
trigger_sync()
logging.info("next sync in %ds", INTERVAL_SEC)
time.sleep(INTERVAL_SEC)