Repository navigation
Expand file tree
/
Copy pathscheduler.py
More file actions
283 lines (231 loc) · 7.31 KB
/
Copy pathscheduler.py
File metadata and controls
283 lines (231 loc) · 7.31 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
import asyncio
import traceback
from datetime import UTC, datetime, timedelta, timezone
from zoneinfo import ZoneInfo
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.date import DateTrigger
from . import db, runner
scheduler = AsyncIOScheduler()
_alarm_callback = None
def set_alarm_callback(cb):
global _alarm_callback
_alarm_callback = cb
def _now() -> str:
return datetime.now(timezone.utc).isoformat()
def _iso(dt: datetime | None) -> str | None:
return dt.isoformat() if dt else None
async def _broadcast_alarm(task: dict) -> None:
if _alarm_callback is None:
return
try:
await _alarm_callback(task)
except Exception:
pass
async def execute_reminder(task_id: str) -> None:
task = await asyncio.to_thread(db.get_task, task_id)
if not task:
return
if task.get("board_column") == "done":
return
if not task.get("reminder_at"):
return
if task.get("reminder_acknowledged"):
return
try:
reminder_dt = datetime.fromisoformat(task["reminder_at"]).astimezone(
timezone.utc
)
except Exception:
return
# Allow a small tolerance for scheduler jitter
if reminder_dt > datetime.now(timezone.utc) + timedelta(seconds=5):
await ReminderService(scheduler).sync_task(task_id)
return
await _broadcast_alarm(task)
class ReminderService:
def __init__(self, scheduler):
self.scheduler = scheduler
def _job_id(self, task_id: str) -> str:
return f"shelldeck_reminder_{task_id}"
def remove_task(self, task_id: str) -> None:
try:
self.scheduler.remove_job(self._job_id(task_id))
except Exception:
pass
def sync_task(self, task_id: str) -> None:
self.remove_task(task_id)
task = db.get_task(task_id)
if not task or task.get("board_column") == "done":
return
reminder_at = task.get("reminder_at")
if not reminder_at:
return
if task.get("reminder_acknowledged"):
return
try:
run_date = datetime.fromisoformat(reminder_at).astimezone(timezone.utc)
except Exception:
return
self.scheduler.add_job(
execute_reminder,
trigger=DateTrigger(run_date=run_date, timezone=UTC),
id=self._job_id(task_id),
replace_existing=True,
misfire_grace_time=86400,
kwargs={"task_id": task_id},
)
async def sync_all(self) -> None:
for job in list(self.scheduler.get_jobs()):
if job.id.startswith("shelldeck_reminder_"):
self.scheduler.remove_job(job.id)
tasks = await asyncio.to_thread(db.list_tasks)
for task in tasks:
self.sync_task(task["id"])
reminder_service = ReminderService(scheduler)
def validate_cron(expression: str) -> CronTrigger:
parts = expression.split()
if len(parts) != 5:
raise ValueError("Cron expression must contain five fields.")
minute, hour, day, month, weekday = parts
return CronTrigger(
minute=minute,
hour=hour,
day=day,
month=month,
day_of_week=weekday,
timezone=UTC,
)
def cron_trigger(cron: str, tz: str = "UTC") -> CronTrigger:
parts = cron.split()
if len(parts) != 5:
raise ValueError("Cron expression must contain five fields.")
try:
tzinfo = ZoneInfo(tz)
except Exception:
tzinfo = UTC
minute, hour, day, month, weekday = parts
return CronTrigger(
minute=minute,
hour=hour,
day=day,
month=month,
day_of_week=weekday,
timezone=tzinfo,
)
async def execute_job(job_id: str):
job = await asyncio.to_thread(db.get_job, job_id)
if not job or not job.get("enabled"):
return
run_at = _now()
await asyncio.to_thread(
db.update_job,
job_id,
last_status="running",
last_run_on=run_at,
)
try:
result = await asyncio.to_thread(
runner.run_command,
job["project_id"],
job["command"],
None,
job.get("timeout_seconds", 60),
)
except Exception:
result = {
"status": "failed",
"exit_code": None,
"output": "",
"error": traceback.format_exc(),
"duration_ms": 0,
}
status = result["status"]
duration_ms = result["duration_ms"]
exit_code = result["exit_code"]
output = result["output"]
error = result["error"]
successful = status == "success"
try:
last_run_dt = datetime.fromisoformat(run_at)
trigger = cron_trigger(job["cron"], job.get("timezone", "UTC"))
next_run_dt = trigger.get_next_fire_time(last_run_dt, last_run_dt)
next_run = _iso(next_run_dt)
except Exception:
next_run = _iso(_next_run_time(job_id))
await asyncio.to_thread(
db.update_job_after_run,
job_id,
status,
run_at,
duration_ms,
error,
next_run,
successful,
)
await asyncio.to_thread(
db.add_job_run,
job_id,
run_at,
status,
exit_code,
output,
error,
duration_ms,
)
class SchedulerService:
def __init__(self):
self.scheduler = scheduler
def aps_job_id(self, db_job_id: str) -> str:
return f"shelldeck_job_{db_job_id}"
def remove(self, db_job_id: str) -> None:
job_id = self.aps_job_id(db_job_id)
if self.scheduler.get_job(job_id):
self.scheduler.remove_job(job_id)
def next_run(self, db_job_id: str) -> datetime | None:
job = self.scheduler.get_job(self.aps_job_id(db_job_id))
return job.next_run_time if job else None
async def sync_job(self, job: dict) -> None:
db_id = job["id"]
self.remove(db_id)
if not job.get("enabled"):
await asyncio.to_thread(
db.update_job,
db_id,
next_run_on=None,
)
return
trigger = cron_trigger(job["cron"], job.get("timezone", "UTC"))
self.scheduler.add_job(
execute_job,
trigger=trigger,
id=self.aps_job_id(db_id),
replace_existing=True,
kwargs={"job_id": db_id},
misfire_grace_time=60,
)
now = datetime.now(timezone.utc)
next_run_dt = trigger.get_next_fire_time(None, now)
next_run = _iso(next_run_dt)
await asyncio.to_thread(
db.update_job,
db_id,
next_run_on=next_run,
)
async def sync_all(self) -> None:
jobs = await asyncio.to_thread(db.list_jobs)
for job in jobs:
await self.sync_job(job)
def run_now(self, db_job_id: str) -> None:
self.scheduler.add_job(
execute_job,
kwargs={"job_id": db_job_id},
)
scheduler_service = SchedulerService()
def _next_run_time(db_job_id: str) -> datetime | None:
return scheduler_service.next_run(db_job_id)
async def start() -> None:
scheduler.start()
await scheduler_service.sync_all()
def shutdown() -> None:
scheduler.shutdown(wait=False)