Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_scheduler_admin.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_scheduler_admin.py · 8.2 KB · 193 lines
1"""The scheduler observability slice (Dan: "celery flower would do better"):
2runs recorded as records by the daemon, and the /scheduler admin page --
3task board + run history + run_now/pause/resume controls."""
4
5import json
6import pathlib
7import time
8
9import object_daemon
10import object_execution
11import object_records
12import object_state
13import python_object_runtime
14from conftest import stage_collection
15
16REPO_ROOT = pathlib.Path(__file__).resolve().parents[1]
17PACKAGES = REPO_ROOT / "packages"
18DASH_OBJECTS = PACKAGES / "system-dashboard" / "objects"
19RUNTIME = python_object_runtime.PythonObjectRuntime()
20
21ADMIN = {"user_id": "dan", "roles": ["admin"]}
22MEMBER = {"user_id": "pat", "roles": []}
23
24
25def setup_env(tmp_path, monkeypatch, *, with_runs_collection=True):
26 objects_dir = tmp_path / "objects"
27 (objects_dir / "triggers").mkdir(parents=True)
28 (objects_dir / "triggers" / "scheduler.py").write_text(
29 "def POST(request):\n return {'ok': True}\n")
30 (objects_dir / "action").mkdir()
31 (objects_dir / "action" / "probe.py").write_text(
32 "def POST(request):\n return {'flipped': 3}\n")
33 (objects_dir / "action" / "broken.py").write_text(
34 "def POST(request):\n raise RuntimeError('boom')\n")
35 data_dir = tmp_path / "data"
36 data_dir.mkdir()
37 if with_runs_collection:
38 stage_collection(data_dir, "system-dashboard", "scheduler_runs")
39 monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(objects_dir))
40 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
41 return objects_dir, data_dir
42
43
44def register(data_dir, key, task):
45 object_state.ObjectStateManager("scheduler", base_dir=data_dir).set(
46 key, json.dumps(task))
47
48
49def due_task(object_id, task_id="t1"):
50 return {"id": task_id, "object_id": object_id, "method": "POST",
51 "payload": {}, "schedule": "2020-01-01T00:00:00+00:00",
52 "type": "onetime", "status": "active"}
53
54
55def runs(data_dir):
56 return object_records.read_collection_records("scheduler_runs", base_dir=data_dir)
57
58
59def page(method, payload):
60 result = object_execution.execute_object(
61 RUNTIME,
62 object_execution.ObjectExecutionRequest(
63 "system_scheduler", method=method, payload=payload),
64 roots=[DASH_OBJECTS])
65 return result.result
66
67
68# --- daemon records runs ------------------------------------------------------
69
70def test_successful_run_is_recorded_with_result(tmp_path, monkeypatch):
71 _, data_dir = setup_env(tmp_path, monkeypatch)
72 register(data_dir, "task_ok", due_task("action_probe"))
73 object_daemon.process_scheduler(python_object_runtime.PythonObjectRuntime())
74 rows = runs(data_dir)
75 assert len(rows) == 1
76 row = rows[0]
77 assert row["ok"] == "true"
78 assert row["task_id"] == "t1"
79 assert row["object_id"] == "action_probe"
80 assert json.loads(row["result"]) == {"flipped": 3}
81 assert row["error"] == ""
82 assert int(row["duration_ms"]) >= 0
83
84
85def test_failed_run_is_recorded_with_error(tmp_path, monkeypatch):
86 _, data_dir = setup_env(tmp_path, monkeypatch)
87 register(data_dir, "task_bad", due_task("action_broken", task_id="t2"))
88 object_daemon.process_scheduler(python_object_runtime.PythonObjectRuntime())
89 row = runs(data_dir)[0]
90 assert row["ok"] == "false"
91 assert row["result"] == ""
92 assert "boom" in row["error"]
93 assert row["error_type"]
94
95
96def test_missing_runs_collection_never_breaks_the_pass(tmp_path, monkeypatch):
97 _, data_dir = setup_env(tmp_path, monkeypatch, with_runs_collection=False)
98 register(data_dir, "task_ok", due_task("action_probe"))
99 object_daemon.process_scheduler(python_object_runtime.PythonObjectRuntime())
100 task = json.loads(
101 object_state.ObjectStateManager("scheduler", base_dir=data_dir).get("task_ok"))
102 assert task["run_count"] == 1 # executed fine, just unrecorded
103
104
105# --- the /scheduler page ------------------------------------------------------
106
107def test_page_gates_on_admin_identity(tmp_path, monkeypatch):
108 setup_env(tmp_path, monkeypatch)
109 anon = page("GET", {})
110 assert "Sign in" in anon["body"]
111 member = page("GET", {"_identity": MEMBER})
112 assert "admin role" in member["body"]
113 for body in (anon["body"], member["body"]):
114 assert "Recent Runs" not in body # no data leaks below the gate
115
116
117def test_admin_page_shows_tasks_and_run_history(tmp_path, monkeypatch):
118 _, data_dir = setup_env(tmp_path, monkeypatch)
119 register(data_dir, "task_ok", due_task("action_probe"))
120 object_daemon.process_scheduler(python_object_runtime.PythonObjectRuntime())
121 body = page("GET", {"_identity": ADMIN})["body"]
122 assert "action_probe" in body
123 assert "flipped" in body # result JSON surfaced
124 assert "Run now" in body
125 assert "Runs (24h)" in body
126
127
128def test_run_now_marks_task_due_and_next_pass_executes(tmp_path, monkeypatch):
129 _, data_dir = setup_env(tmp_path, monkeypatch)
130 task = due_task("action_probe")
131 task["schedule"] = "2099-01-01T00:00:00+00:00" # far future
132 register(data_dir, "task_ok", task)
133 denied = page("POST", {"_identity": MEMBER, "action": "run_now", "key": "task_ok"})
134 assert denied["status"] == 403
135 result = page("POST", {"_identity": ADMIN, "action": "run_now", "key": "task_ok"})
136 assert result["status"] == 200
137 assert result["next_run"] <= int(time.time())
138 object_daemon.process_scheduler(python_object_runtime.PythonObjectRuntime())
139 assert len(runs(data_dir)) == 1 # ran despite the far-future schedule
140
141
142def test_pause_and_resume(tmp_path, monkeypatch):
143 _, data_dir = setup_env(tmp_path, monkeypatch)
144 register(data_dir, "task_ok", due_task("action_probe"))
145 paused = page("POST", {"_identity": ADMIN, "action": "pause", "key": "task_ok"})
146 assert paused["task_status"] == "paused"
147 object_daemon.process_scheduler(python_object_runtime.PythonObjectRuntime())
148 assert runs(data_dir) == [] # paused task never runs
149 resumed = page("POST", {"_identity": ADMIN, "action": "resume", "key": "task_ok"})
150 assert resumed["task_status"] == "active"
151 assert resumed["next_run"] is None # daemon recomputes from the schedule
152 object_daemon.process_scheduler(python_object_runtime.PythonObjectRuntime())
153 assert len(runs(data_dir)) == 1 # onetime schedule in the past -> due again
154
155
156def test_schedules_render_in_plain_english(tmp_path, monkeypatch):
157 """A cron string is precise and opaque; an operator who cannot read WHEN
158 a task runs cannot tell whether it ran when it should have."""
159 setup_env(tmp_path, monkeypatch)
160 import importlib.util
161 spec = importlib.util.spec_from_file_location(
162 "_sched", DASH_OBJECTS / "system" / "scheduler.py")
163 module = importlib.util.module_from_spec(spec)
164 spec.loader.exec_module(module)
165 describe = module.describe_schedule
166
167 assert describe("10 6 * * *") == "Daily at 06:10 UTC"
168 assert describe("25 6 * * *") == "Daily at 06:25 UTC"
169 assert describe("*/5 * * * *") == "Every 5 minutes"
170 assert describe("0 * * * *") == "Hourly at :00"
171 assert describe("30 3 * * 1") == "Weekly on Monday at 03:30 UTC"
172 assert describe("0 4 1 * *") == "Monthly on day 1 at 04:00 UTC"
173 assert describe("2026-01-01T00:00:00+00:00", "onetime").startswith("Once at 2026-01-01")
174 assert describe("15 9 * * 1-5") == "" # unsupported shape: fall back to the raw cron
175 assert describe("") == ""
176
177
178def test_admin_page_shows_english_schedule_and_a_cron_legend(tmp_path, monkeypatch):
179 _, data_dir = setup_env(tmp_path, monkeypatch)
180 task = due_task("action_probe")
181 task.update({"type": "cron", "schedule": "10 6 * * *"})
182 register(data_dir, "task_cron", task)
183 body = page("GET", {"_identity": ADMIN})["body"]
184 assert "Daily at 06:10 UTC" in body
185 assert "10 6 * * *" in body # the cron itself stays as small print
186 assert "day-of-week" in body # field legend for the unusual schedules
187
188
189def test_post_validates_action_and_key(tmp_path, monkeypatch):
190 setup_env(tmp_path, monkeypatch)
191 assert page("POST", {"_identity": ADMIN, "action": "explode", "key": "task_x"})["status"] == 400
192 assert page("POST", {"_identity": ADMIN, "action": "run_now", "key": "nope"})["status"] == 400
193 assert page("POST", {"_identity": ADMIN, "action": "run_now", "key": "task_missing"})["status"] == 404