-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcodec_scheduler.py
More file actions
443 lines (382 loc) · 16.3 KB
/
codec_scheduler.py
File metadata and controls
443 lines (382 loc) · 16.3 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
"""CODEC Scheduler — Cron-like scheduling for agent crews and commands."""
import json
import os
import time
import logging
import sys
from datetime import datetime
import requests
try:
from codec_audit import log_event
except ImportError:
def log_event(*a, **kw): pass
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] [SCHEDULER] %(message)s",
datefmt="%H:%M:%S",
)
log = logging.getLogger("scheduler")
SCHEDULE_PATH = os.path.expanduser("~/.codec/schedules.json")
DASHBOARD_URL = "http://127.0.0.1:8090"
os.makedirs(os.path.expanduser("~/.codec"), exist_ok=True)
# ── Storage ─────────────────────────────────────────────────────────────────
def load_schedules() -> list:
try:
with open(SCHEDULE_PATH) as f:
return json.load(f)
except Exception:
return []
def save_schedules(schedules: list):
with open(SCHEDULE_PATH, "w") as f:
json.dump(schedules, f, indent=2)
# ── Management ──────────────────────────────────────────────────────────────
def add_schedule(
crew_name: str,
topic: str = "",
cron_hour: int = 8,
cron_minute: int = 0,
days: list | None = None,
) -> dict:
"""Add a scheduled agent crew run. days: 0=Mon … 6=Sun, default every day."""
schedules = load_schedules()
schedule = {
"id": f"sched_{int(time.time())}",
"crew": crew_name,
"topic": topic,
"hour": cron_hour,
"minute": cron_minute,
"days": days if days is not None else [0, 1, 2, 3, 4, 5, 6],
"enabled": False,
"last_run": None,
"created": datetime.now().isoformat(),
}
schedules.append(schedule)
save_schedules(schedules)
log.info(f"Schedule added: {crew_name} at {cron_hour:02d}:{cron_minute:02d}")
return schedule
def remove_schedule(sched_id: str) -> bool:
schedules = load_schedules()
before = len(schedules)
schedules = [s for s in schedules if s["id"] != sched_id]
save_schedules(schedules)
return len(schedules) < before
def toggle_schedule(sched_id: str, enabled: bool) -> bool:
schedules = load_schedules()
for s in schedules:
if s["id"] == sched_id:
s["enabled"] = enabled
save_schedules(schedules)
return True
return False
# ── Execution ────────────────────────────────────────────────────────────────
def _notify(title, body, status="success", schedule_id=None):
"""Save notification to dashboard and send macOS notification."""
import uuid as _uuid, subprocess as _sp, re as _re
# Extract Google Doc URL if present (crew returns it as first line)
doc_url = None
doc_match = _re.search(r'(https://docs\.google\.com/document/d/[^\s]+)', body)
if doc_match:
doc_url = doc_match.group(1)
# Clean body: remove raw URL line, add markdown link
body = _re.sub(r'https://docs\.google\.com/document/d/[^\s]+\n*', '', body).strip()
body = f"📄 [View Full Report]({doc_url})\n\n{body}"
# 1. Save to notifications.json (same format as dashboard)
notif_path = os.path.expanduser("~/.codec/notifications.json")
try:
try:
with open(notif_path) as f:
notifications = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
notifications = []
notif = {
"id": f"notif_{_uuid.uuid4().hex[:10]}",
"type": "task_report",
"title": title,
"body": body[:2000],
"status": status,
"created": datetime.now().strftime("%Y-%m-%dT%H:%M:%S"),
"read": False,
"schedule_id": schedule_id,
}
if doc_url:
notif["doc_url"] = doc_url
notifications.insert(0, notif)
os.makedirs(os.path.dirname(notif_path), exist_ok=True)
with open(notif_path, "w") as f:
json.dump(notifications, f, indent=2)
except Exception as e:
log.warning(f" Failed to save notification: {e}")
# 2. macOS notification
mac_body = f"Report ready — tap to view" if doc_url else body[:120]
try:
_sp.run(["osascript", "-e",
f'display notification "{mac_body}" with title "CODEC Task" subtitle "{title}"'],
capture_output=True, timeout=5)
except Exception:
pass
def _run_crew(sched: dict):
"""Fire off a crew via the background job endpoint and optionally poll."""
payload: dict = {"crew": sched["crew"]}
if sched.get("topic"):
payload["topic"] = sched["topic"]
title = sched.get("topic", sched["crew"])
_headers = {"Content-Type": "application/json", "x-internal": "codec"}
try:
r = requests.post(
f"{DASHBOARD_URL}/api/agents/run",
json=payload,
headers=_headers,
timeout=30,
)
if r.status_code == 200:
data = r.json()
job_id = data.get("job_id")
if job_id:
log.info(f" Job started: {job_id} — polling for result…")
# Poll up to 10 min
for _ in range(120):
time.sleep(5)
sr = requests.get(
f"{DASHBOARD_URL}/api/agents/status/{job_id}",
headers={"x-internal": "codec"},
timeout=10,
)
if sr.status_code == 200:
job_data = sr.json()
st = job_data.get("status")
if st not in ("running", "pending"):
result_text = job_data.get("result", "")
if isinstance(result_text, dict):
result_text = result_text.get("result", str(result_text))
log.info(f" ✅ {sched['crew']} finished: {st}")
_notify(title, str(result_text)[:500] if result_text else "Task completed.",
status="success" if st == "complete" else "error",
schedule_id=sched.get("id", sched["crew"]))
return True
else:
log.info(f" ✅ {sched['crew']} completed synchronously")
_notify(title, "Task completed successfully.",
schedule_id=sched.get("id", sched["crew"]))
return True
else:
log.warning(f" ⚠️ /api/agents/run returned {r.status_code}")
_notify(title, f"Failed: server returned {r.status_code}", status="error",
schedule_id=sched.get("id", sched["crew"]))
except Exception as e:
log.error(f" ❌ Crew run failed: {e}")
_notify(title, f"Error: {e}", status="error",
schedule_id=sched.get("id", sched["crew"]))
return False
def check_and_run():
"""Check every minute whether any schedules should fire.
Uses "past scheduled time" logic so tasks fire even if the scheduler
starts after the exact minute. ``last_run`` is only set after a
successful execution; ``last_attempt`` prevents rapid re-firing within
the same minute.
"""
schedules = load_schedules()
now = datetime.now()
today_str = now.strftime("%Y-%m-%d")
now_minutes = now.hour * 60 + now.minute # minutes since midnight
for sched in schedules:
if not sched.get("enabled"):
continue
if now.weekday() not in sched.get("days", list(range(7))):
continue
sched_minutes = sched["hour"] * 60 + sched["minute"]
if now_minutes < sched_minutes:
continue # not yet reached scheduled time today
# Already successfully ran today — skip
last_run = sched.get("last_run")
if last_run and last_run[:10] == today_str:
continue
# Prevent rapid re-firing within the same minute
last_attempt = sched.get("last_attempt")
if last_attempt and last_attempt[:16] == now.strftime("%Y-%m-%dT%H:%M"):
continue
# Record attempt timestamp (prevents double-fire from race / same-minute loop)
sched["last_attempt"] = now.isoformat()
save_schedules(schedules)
log.info(f"🚀 Scheduled run: {sched['crew']} — {sched.get('topic', '')}")
log_event("scheduled", "codec-scheduler", f"Schedule fired: {sched.get('label', sched.get('crew', '?'))}", {"schedule_id": sched.get('id')})
success = _run_crew(sched)
title = sched.get("topic", sched.get("crew", "?"))
log_event("scheduled", "codec-scheduler", f"Schedule done: {title}", {"success": success})
# Only mark last_run on success so failed tasks are retried next minute
if success:
schedules = load_schedules() # reload in case file changed during long run
for s in schedules:
if s["id"] == sched["id"]:
s["last_run"] = datetime.now().isoformat()
break
save_schedules(schedules)
_PID_FILE = os.path.expanduser("~/.codec/scheduler.pid")
def _acquire_pid_lock() -> bool:
"""Ensure only one scheduler daemon runs. Returns True if lock acquired."""
# Check if existing PID is still alive
if os.path.exists(_PID_FILE):
try:
with open(_PID_FILE) as f:
old_pid = int(f.read().strip())
os.kill(old_pid, 0) # signal 0 = check if alive
log.warning(f"Scheduler already running (PID {old_pid}). Exiting.")
return False
except (ProcessLookupError, ValueError):
pass # stale PID file, we can take over
except PermissionError:
log.warning("Scheduler PID exists and is owned by another user. Exiting.")
return False
# Write our PID
os.makedirs(os.path.dirname(_PID_FILE), exist_ok=True)
with open(_PID_FILE, "w") as f:
f.write(str(os.getpid()))
return True
def _release_pid_lock():
try:
os.remove(_PID_FILE)
except FileNotFoundError:
pass
def run_daemon(check_interval: int = 60):
"""Run check_and_run every minute, aligned to the start of each minute."""
if not _acquire_pid_lock():
sys.exit(0)
try:
schedules = load_schedules()
log.info(f"Scheduler daemon starting (PID {os.getpid()}) — {len(schedules)} schedule(s) loaded")
while True:
try:
check_and_run()
except Exception as e:
log.error(f"Scheduler loop error: {e}")
# Sleep until the next minute boundary to avoid drift
now = time.time()
sleep_secs = check_interval - (now % check_interval)
time.sleep(max(1, sleep_secs))
finally:
_release_pid_lock()
# ── CODEC Skill (voice control) ──────────────────────────────────────────────
SKILL_NAME = "scheduler"
SKILL_TRIGGERS = [
"schedule agent", "schedule crew", "run every morning",
"run every monday", "schedule daily", "run daily briefing",
"set up schedule", "every morning at", "every monday",
"schedule competitor analysis", "run briefing at",
]
SKILL_DESCRIPTION = "Schedule CODEC agent crews to run automatically on a cron schedule"
_DAY_MAP = {
"monday": 0, "tuesday": 1, "wednesday": 2, "thursday": 3,
"friday": 4, "saturday": 5, "sunday": 6,
"weekdays": [0, 1, 2, 3, 4], "weekends": [5, 6],
"every day": [0, 1, 2, 3, 4, 5, 6], "daily": [0, 1, 2, 3, 4, 5, 6],
}
_CREW_MAP = {
"daily briefing": "daily_briefing",
"briefing": "daily_briefing",
"competitor": "competitor_analysis",
"competitor analysis": "competitor_analysis",
"social media": "social_media",
"code review": "code_review",
"data analysis": "data_analysis",
}
def _parse_schedule_intent(task: str) -> dict | None:
"""Parse natural language like 'run my daily briefing every morning at 8'."""
import re
tl = task.lower()
# Detect crew
crew = "daily_briefing"
for phrase, name in _CREW_MAP.items():
if phrase in tl:
crew = name
break
# Detect hour
hour = 8
m = re.search(r"at (\d{1,2})(?::(\d{2}))?\s*(?:am|pm)?", tl)
if m:
hour = int(m.group(1))
minute_str = m.group(2)
minute = int(minute_str) if minute_str else 0
if "pm" in tl and hour < 12:
hour += 12
else:
minute = 0
# Detect days
days = [0, 1, 2, 3, 4, 5, 6]
for phrase, val in _DAY_MAP.items():
if phrase in tl:
days = val if isinstance(val, list) else [val]
break
return {"crew": crew, "hour": hour, "minute": minute, "days": days}
def run(task: str, context: str = "") -> str:
"""Voice-triggered schedule creation."""
tl = task.lower()
if "list" in tl or "show" in tl or "what schedule" in tl:
schedules = load_schedules()
if not schedules:
return "No schedules set up yet. Say 'schedule daily briefing at 8am' to create one."
day_names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
lines = [f"{len(schedules)} schedule(s):"]
for s in schedules:
days_str = ", ".join(day_names[d] for d in s.get("days", []))
status = "✅" if s.get("enabled") else "❌"
lines.append(f" {status} {s['crew']} at {s['hour']:02d}:{s['minute']:02d} [{days_str}]")
return "\n".join(lines)
if "remove" in tl or "delete" in tl or "cancel" in tl:
schedules = load_schedules()
if schedules:
remove_schedule(schedules[-1]["id"])
return f"Removed last schedule: {schedules[-1]['crew']}"
return "No schedules to remove."
intent = _parse_schedule_intent(task)
if not intent:
return "I couldn't parse that schedule. Try: 'run daily briefing every morning at 8'"
s = add_schedule(
intent["crew"],
cron_hour=intent["hour"],
cron_minute=intent["minute"],
days=intent["days"],
)
day_names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
days_str = ", ".join(day_names[d] for d in s["days"])
return (
f"Scheduled: {s['crew']} will run at {s['hour']:02d}:{s['minute']:02d} "
f"on {days_str}. Say 'list schedules' to see all."
)
# ── CLI ──────────────────────────────────────────────────────────────────────
if __name__ == "__main__":
if len(sys.argv) < 2:
run_daemon()
elif sys.argv[1] == "list":
schedules = load_schedules()
day_names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
if not schedules:
print("No schedules.")
for s in schedules:
days_str = ", ".join(day_names[d] for d in s.get("days", []))
status = "✅" if s.get("enabled") else "❌"
print(f" {status} {s['id']}: {s['crew']} at {s['hour']:02d}:{s['minute']:02d} [{days_str}]")
elif sys.argv[1] == "add" and len(sys.argv) > 2:
crew = sys.argv[2]
hour = 8
minute = 0
days = None
i = 3
while i < len(sys.argv):
if sys.argv[i] == "--hour" and i + 1 < len(sys.argv):
hour = int(sys.argv[i + 1]); i += 2
elif sys.argv[i] == "--minute" and i + 1 < len(sys.argv):
minute = int(sys.argv[i + 1]); i += 2
elif sys.argv[i] == "--days" and i + 1 < len(sys.argv):
days = [int(d) for d in sys.argv[i + 1].split(",")]; i += 2
else:
i += 1
s = add_schedule(crew, cron_hour=hour, cron_minute=minute, days=days)
print(f"Added: {s['id']} — {crew} at {hour:02d}:{minute:02d}")
elif sys.argv[1] == "remove" and len(sys.argv) > 2:
if remove_schedule(sys.argv[2]):
print(f"Removed {sys.argv[2]}")
else:
print(f"Not found: {sys.argv[2]}")
elif sys.argv[1] == "run":
check_and_run()
else:
run_daemon()