askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_daemon_scheduler_native.py · 4.7 KB · 115 lines
| 1 | """The daemon's scheduler pass on the NATIVE runtime. |
| 2 | |
| 3 | Found live 2026-07-24: the droplet daemon printed "Object runtime: NOT |
| 4 | installed (scheduler/queue/events passes disabled)" -- process_scheduler |
| 5 | only knew the legacy dbbasic_object_core runtime, so scheduled tasks |
| 6 | (invoice aging, recurring journals) had NO working schedule path in |
| 7 | production, the exact q9 failure mode (auto-approve coded, never |
| 8 | scheduled). The daemon now falls back to python_object_runtime; these |
| 9 | tests pin that the whole pass -- trigger state read, due-task execution, |
| 10 | bookkeeping -- works on it. |
| 11 | """ |
| 12 | |
| 13 | import json |
| 14 | import time |
| 15 | |
| 16 | import pytest |
| 17 | |
| 18 | import object_daemon |
| 19 | from python_object_runtime import PythonObjectRuntime |
| 20 | |
| 21 | |
| 22 | def setup_env(tmp_path, monkeypatch): |
| 23 | objects_dir = tmp_path / "objects" |
| 24 | (objects_dir / "triggers").mkdir(parents=True) |
| 25 | (objects_dir / "triggers" / "scheduler.py").write_text( |
| 26 | "def POST(request):\n return {'ok': True}\n") |
| 27 | (objects_dir / "action").mkdir() |
| 28 | (objects_dir / "action" / "probe.py").write_text( |
| 29 | "import os\n" |
| 30 | "def POST(request):\n" |
| 31 | " with open(os.environ['DBBASIC_DATA_DIR'] + '/probe.txt', 'w') as fh:\n" |
| 32 | " fh.write(str(request.get('mark')))\n" |
| 33 | " return {'ok': True}\n") |
| 34 | data_dir = tmp_path / "data" |
| 35 | data_dir.mkdir() |
| 36 | monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(objects_dir)) |
| 37 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 38 | return objects_dir, data_dir |
| 39 | |
| 40 | |
| 41 | def register(objects_dir, key, task): |
| 42 | runtime = PythonObjectRuntime() |
| 43 | obj = runtime.load_object(objects_dir / "triggers" / "scheduler.py") |
| 44 | obj.state_manager.set(key, json.dumps(task)) |
| 45 | |
| 46 | |
| 47 | def read_task(objects_dir, key): |
| 48 | runtime = PythonObjectRuntime() |
| 49 | obj = runtime.load_object(objects_dir / "triggers" / "scheduler.py") |
| 50 | obj.state_manager.reload() |
| 51 | return json.loads(obj.state_manager.get(key)) |
| 52 | |
| 53 | |
| 54 | def test_due_onetime_task_executes_target_and_completes(tmp_path, monkeypatch): |
| 55 | objects_dir, data_dir = setup_env(tmp_path, monkeypatch) |
| 56 | register(objects_dir, "task_probe", { |
| 57 | "id": "probe-once", "object_id": "action_probe", "method": "POST", |
| 58 | "payload": {"mark": "ran-by-scheduler"}, |
| 59 | "schedule": "2020-01-01T00:00:00+00:00", "type": "onetime", |
| 60 | "status": "active", |
| 61 | }) |
| 62 | object_daemon.process_scheduler(PythonObjectRuntime()) |
| 63 | assert (data_dir / "probe.txt").read_text() == "ran-by-scheduler" |
| 64 | task = read_task(objects_dir, "task_probe") |
| 65 | assert task["status"] == "completed" |
| 66 | assert task["run_count"] == 1 |
| 67 | assert task["next_run"] is None |
| 68 | |
| 69 | |
| 70 | def test_future_task_is_left_alone(tmp_path, monkeypatch): |
| 71 | objects_dir, data_dir = setup_env(tmp_path, monkeypatch) |
| 72 | register(objects_dir, "task_future", { |
| 73 | "id": "probe-later", "object_id": "action_probe", "method": "POST", |
| 74 | "payload": {}, "schedule": "2099-01-01T00:00:00+00:00", |
| 75 | "type": "onetime", "status": "active", |
| 76 | }) |
| 77 | object_daemon.process_scheduler(PythonObjectRuntime()) |
| 78 | assert not (data_dir / "probe.txt").exists() |
| 79 | task = read_task(objects_dir, "task_future") |
| 80 | assert task.get("run_count", 0) == 0 |
| 81 | assert task["status"] == "active" |
| 82 | |
| 83 | |
| 84 | def test_cron_task_runs_and_reschedules(tmp_path, monkeypatch): |
| 85 | pytest.importorskip("croniter") |
| 86 | objects_dir, data_dir = setup_env(tmp_path, monkeypatch) |
| 87 | register(objects_dir, "task_cron", { |
| 88 | "id": "probe-cron", "object_id": "action_probe", "method": "POST", |
| 89 | "payload": {"mark": "cron"}, "schedule": "* * * * *", "type": "cron", |
| 90 | "status": "active", |
| 91 | # Pre-set next_run in the past so the pass fires immediately (the |
| 92 | # daemon normally computes it on first sight, which lands in the |
| 93 | # future for a fresh cron task). |
| 94 | "next_run": int(time.time()) - 60, |
| 95 | }) |
| 96 | object_daemon.process_scheduler(PythonObjectRuntime()) |
| 97 | assert (data_dir / "probe.txt").read_text() == "cron" |
| 98 | task = read_task(objects_dir, "task_cron") |
| 99 | assert task["status"] == "active" # cron tasks stay active |
| 100 | assert task["run_count"] == 1 |
| 101 | assert task["next_run"] > int(time.time()) - 5 # rescheduled forward |
| 102 | |
| 103 | |
| 104 | def test_inactive_and_malformed_entries_are_skipped(tmp_path, monkeypatch): |
| 105 | objects_dir, data_dir = setup_env(tmp_path, monkeypatch) |
| 106 | register(objects_dir, "task_paused", { |
| 107 | "id": "paused", "object_id": "action_probe", "method": "POST", |
| 108 | "payload": {}, "schedule": "2020-01-01T00:00:00+00:00", |
| 109 | "type": "onetime", "status": "paused", |
| 110 | }) |
| 111 | runtime = PythonObjectRuntime() |
| 112 | obj = runtime.load_object(objects_dir / "triggers" / "scheduler.py") |
| 113 | obj.state_manager.set("task_garbage", "not json") |
| 114 | object_daemon.process_scheduler(PythonObjectRuntime()) |
| 115 | assert not (data_dir / "probe.txt").exists() |