deja-cli 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
deja/core/scheduler.py ADDED
@@ -0,0 +1,65 @@
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ import sys
5
+ from typing import TYPE_CHECKING
6
+
7
+ from apscheduler.schedulers.background import BackgroundScheduler
8
+
9
+ if TYPE_CHECKING:
10
+ from deja.core.reflection import ReflectionEngine
11
+
12
+
13
+ def make_scheduler(
14
+ engine: "ReflectionEngine",
15
+ loop: asyncio.AbstractEventLoop,
16
+ ) -> BackgroundScheduler:
17
+ """Create and configure the background scheduler for the watch daemon.
18
+
19
+ Schedule:
20
+ - Every 5 minutes: check token thresholds, auto-trigger observer/reflector
21
+ - Nightly 2:00am UTC: run_decay
22
+ - Nightly 2:05am UTC: run_promote
23
+ - Nightly 2:10am UTC: run_archive
24
+ """
25
+ scheduler = BackgroundScheduler(timezone="UTC")
26
+
27
+ def _run(coro_fn, label: str, *args):
28
+ """Submit a coroutine to the async event loop from the scheduler thread."""
29
+ try:
30
+ future = asyncio.run_coroutine_threadsafe(coro_fn(*args), loop)
31
+ result = future.result(timeout=300)
32
+ if result:
33
+ print(f"[deja scheduler] {label}: {result}", file=sys.stderr)
34
+ except Exception as e:
35
+ print(f"[deja scheduler] {label} error: {e}", file=sys.stderr)
36
+
37
+ scheduler.add_job(
38
+ lambda: _run(engine.check_and_trigger, "token-trigger check"),
39
+ "interval",
40
+ minutes=5,
41
+ id="token_trigger",
42
+ )
43
+ scheduler.add_job(
44
+ lambda: _run(engine.run_decay, "decay"),
45
+ "cron",
46
+ hour=2,
47
+ minute=0,
48
+ id="nightly_decay",
49
+ )
50
+ scheduler.add_job(
51
+ lambda: _run(engine.run_promote, "promote"),
52
+ "cron",
53
+ hour=2,
54
+ minute=5,
55
+ id="nightly_promote",
56
+ )
57
+ scheduler.add_job(
58
+ lambda: _run(engine.run_archive, "archive"),
59
+ "cron",
60
+ hour=2,
61
+ minute=10,
62
+ id="nightly_archive",
63
+ )
64
+
65
+ return scheduler