-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrigger.py
More file actions
68 lines (56 loc) · 2.14 KB
/
trigger.py
File metadata and controls
68 lines (56 loc) · 2.14 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
import os
import time
import logging
from datetime import datetime, timezone, timedelta
import requests
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[logging.StreamHandler()],
)
logger = logging.getLogger(__name__)
TRIGGER_URL = os.environ["TRIGGER_URL"]
TRIGGER_TOKEN = os.environ["TRIGGER_TOKEN"]
KST = timezone(timedelta(hours=9))
SCHEDULED_TIMES = (
(6, 5),
(11, 5),
(16, 5),
(21, 3),
)
def next_fire_time(current: datetime) -> datetime:
candidate_day = current
for _ in range(8):
if candidate_day.weekday() != 5:
for hour, minute in SCHEDULED_TIMES:
candidate = candidate_day.replace(hour=hour, minute=minute, second=0, microsecond=0)
if current < candidate:
return candidate
candidate_day = (candidate_day + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
raise RuntimeError("Could not find a valid fire time")
def fire():
headers = {
"Authorization": f"Bearer {TRIGGER_TOKEN}",
"anthropic-version": "2023-06-01",
"anthropic-beta": "experimental-cc-routine-2026-04-01",
"Content-Type": "application/json",
}
payload = {"text": "optional extra turn appended to the session"}
try:
resp = requests.post(TRIGGER_URL, headers=headers, json=payload, timeout=30)
resp.raise_for_status()
logger.info("Triggered successfully: %s", resp.status_code)
except requests.RequestException as e:
logger.error("Trigger failed: %s", e)
if __name__ == "__main__":
target = next_fire_time(datetime.now(KST))
logger.info("Fixed KST schedule mode: first trigger at %s KST", target.strftime("%Y-%m-%d %H:%M:%S"))
while True:
now = datetime.now(KST)
wait = (target - now).total_seconds()
if wait > 0:
logger.info("Sleeping %.1fs until %s KST", wait, target.strftime("%H:%M:%S"))
time.sleep(max(1, wait))
fire()
target = next_fire_time(datetime.now(KST))
logger.info("Next trigger at %s KST", target.strftime("%Y-%m-%d %H:%M:%S"))