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.
test_scheduled_failures_visible.py · 5.5 KB · 154 lines
| 1 | """A scheduled method that fails must look failed. |
| 2 | |
| 3 | The scheduling simulation (2026-12-01) found a method that raised, or hit the |
| 4 | memory limit, still counted as a run: run_count went up, the row had no error |
| 5 | and the object's log had no line. A broken job looked exactly like a healthy |
| 6 | one. The task row now carries last_error, last_error_at and |
| 7 | consecutive_failures, the target object's log gets an ERROR line, and the |
| 8 | scheduler board shows it. |
| 9 | """ |
| 10 | from __future__ import annotations |
| 11 | |
| 12 | import json |
| 13 | import textwrap |
| 14 | |
| 15 | import pytest |
| 16 | |
| 17 | import object_daemon |
| 18 | import object_logs |
| 19 | import object_sandbox |
| 20 | import object_schedule |
| 21 | import object_state |
| 22 | from python_object_runtime import PythonObjectRuntime |
| 23 | from runtime_modes import configure, run_object, write_object |
| 24 | |
| 25 | KEY = "task_obj.tools_job.work" |
| 26 | |
| 27 | JOB = textwrap.dedent(''' |
| 28 | _schedule = None |
| 29 | |
| 30 | def work(): |
| 31 | if _state_manager.get("mode") == "fail": |
| 32 | raise RuntimeError("the supplier feed is down") |
| 33 | return {"ok": True} |
| 34 | |
| 35 | def GET(request): |
| 36 | return _schedule(30, "work") |
| 37 | ''') |
| 38 | |
| 39 | |
| 40 | def setup_job(monkeypatch, tmp_path, source=JOB, name="tools_job"): |
| 41 | data_dir, root = configure(monkeypatch, tmp_path) |
| 42 | (root / "triggers").mkdir(parents=True, exist_ok=True) |
| 43 | (root / "triggers" / "scheduler.py").write_text("def POST(request):\n return {}\n") |
| 44 | write_object(root, name, source) |
| 45 | object_daemon._REPORTED_GONE.clear() |
| 46 | assert run_object("in_process", root, name, "GET").ok |
| 47 | return data_dir, root |
| 48 | |
| 49 | |
| 50 | def row(data_dir, key=KEY): |
| 51 | return json.loads(object_state.get_object_state("scheduler", data_dir)[key]) |
| 52 | |
| 53 | |
| 54 | def run_due(data_dir, key=KEY): |
| 55 | manager = object_state.ObjectStateManager("scheduler", base_dir=data_dir) |
| 56 | task = json.loads(manager.get(key)) |
| 57 | task["next_run"] = 1 |
| 58 | manager.set(key, json.dumps(task)) |
| 59 | object_daemon.process_scheduler(PythonObjectRuntime()) |
| 60 | |
| 61 | |
| 62 | def test_a_raising_method_records_the_error_and_counts_consecutive_failures(tmp_path, monkeypatch): |
| 63 | data_dir, _root = setup_job(monkeypatch, tmp_path) |
| 64 | object_state.ObjectStateManager("tools_job", data_dir).set("mode", "fail") |
| 65 | |
| 66 | run_due(data_dir) |
| 67 | first = row(data_dir) |
| 68 | assert "the supplier feed is down" in first["last_error"] |
| 69 | assert first["last_error_at"] >= first["last_run"] > 0 |
| 70 | assert first["consecutive_failures"] == 1 |
| 71 | |
| 72 | run_due(data_dir) |
| 73 | assert row(data_dir)["consecutive_failures"] == 2 |
| 74 | |
| 75 | logs = object_logs.get_object_logs("tools_job", data_dir, level="ERROR") |
| 76 | assert len(logs) == 2 |
| 77 | assert "the supplier feed is down" in logs[0]["message"] and "work" in logs[0]["message"] |
| 78 | |
| 79 | |
| 80 | def test_a_success_resets_the_streak_but_keeps_the_last_error(tmp_path, monkeypatch): |
| 81 | data_dir, _root = setup_job(monkeypatch, tmp_path) |
| 82 | state = object_state.ObjectStateManager("tools_job", data_dir) |
| 83 | state.set("mode", "fail") |
| 84 | run_due(data_dir) |
| 85 | state.set("mode", "ok") |
| 86 | run_due(data_dir) |
| 87 | healed = row(data_dir) |
| 88 | assert healed["consecutive_failures"] == 0 |
| 89 | assert "supplier feed" in healed["last_error"] # history survives |
| 90 | |
| 91 | |
| 92 | def test_a_healthy_method_has_no_error_fields(tmp_path, monkeypatch): |
| 93 | data_dir, _root = setup_job(monkeypatch, tmp_path) |
| 94 | run_due(data_dir) |
| 95 | healthy = row(data_dir) |
| 96 | assert healthy["run_count"] == 1 |
| 97 | assert "last_error" not in healthy and not healthy.get("consecutive_failures") |
| 98 | assert object_logs.get_object_logs("tools_job", data_dir, level="ERROR") == [] |
| 99 | |
| 100 | |
| 101 | def test_a_missing_method_is_recorded_as_a_failure_and_logged_once(tmp_path, monkeypatch): |
| 102 | data_dir, root = setup_job(monkeypatch, tmp_path) |
| 103 | write_object(root, "tools_job", "def GET(request):\n return {}\n") # work() removed |
| 104 | |
| 105 | run_due(data_dir) |
| 106 | run_due(data_dir) |
| 107 | missing = row(data_dir) |
| 108 | assert "no method 'work'" in missing["last_error"] |
| 109 | assert missing["consecutive_failures"] == 2 |
| 110 | assert missing["run_count"] == 0 # it never ran |
| 111 | # the object log says so once, not every interval |
| 112 | assert len(object_logs.get_object_logs("tools_job", data_dir, level="ERROR")) == 1 |
| 113 | |
| 114 | |
| 115 | def test_a_timeout_of_a_user_method_is_recorded(tmp_path, monkeypatch): |
| 116 | monkeypatch.setenv(object_sandbox.ISOLATION_ENV, object_sandbox.UNSAFE_DEV) |
| 117 | monkeypatch.setenv(object_sandbox.TIMEOUT_ENV, "1") |
| 118 | monkeypatch.setenv(object_schedule.MIN_INTERVAL_ENV, "1") |
| 119 | source = textwrap.dedent(''' |
| 120 | def work(): |
| 121 | while True: |
| 122 | pass |
| 123 | |
| 124 | def GET(request): |
| 125 | return _schedule(60, "work") |
| 126 | ''') |
| 127 | try: |
| 128 | data_dir, _root = setup_job(monkeypatch, tmp_path, source, name="u_7_spin") |
| 129 | key = "task_obj.u_7_spin.work" |
| 130 | run_due(data_dir, key) |
| 131 | failed = row(data_dir, key) |
| 132 | assert failed["consecutive_failures"] == 1 |
| 133 | assert "timed out" in failed["last_error"].lower() or "timeout" in failed["last_error"].lower() |
| 134 | assert object_logs.get_object_logs("u_7_spin", data_dir, level="ERROR") |
| 135 | finally: |
| 136 | object_sandbox.shutdown() |
| 137 | |
| 138 | |
| 139 | def test_the_board_shows_a_failing_task_even_without_run_history(tmp_path, monkeypatch): |
| 140 | import importlib.util |
| 141 | |
| 142 | data_dir, _root = setup_job(monkeypatch, tmp_path) |
| 143 | object_state.ObjectStateManager("tools_job", data_dir).set("mode", "fail") |
| 144 | run_due(data_dir) |
| 145 | run_due(data_dir) |
| 146 | |
| 147 | spec = importlib.util.spec_from_file_location( |
| 148 | "board", "packages/system-dashboard/objects/system/scheduler.py" |
| 149 | ) |
| 150 | board = importlib.util.module_from_spec(spec) |
| 151 | spec.loader.exec_module(board) |
| 152 | tasks, _ = board._tasks() |
| 153 | html = board._task_rows_html(tasks, {}) |
| 154 | assert "failing x2" in html and "supplier feed is down" in html |