"""The scheduler: the five wordings, the table, the firing, and the routes. python -m pytest pipeline/tests/test_schedule.py -q Three things here are worth knowing before changing anything. The daylight-saving tests are the reason `next_after` takes a zone at all, and they are written around a real transition rather than a synthetic one, because the bug they guard is precisely that `+ timedelta(days=1)` looks correct. The firing tests call `JobQueue._fire_due` directly. It is a private, and that is deliberate: driving it through `start()` would mean asserting on a loop that ticks on a clock, and a test that sleeps to make a scheduler fire is a test that fails on a busy machine. One test does start a real queue, to prove the tick is wired into the loop at all. `SCHEDULES` is a module-level registry, like `JOBS`, so anything here that declares into it does so through the `declared` fixture, which puts it back. """ from __future__ import annotations import os import subprocess import sys import tempfile from datetime import UTC, datetime, timedelta from pathlib import Path from zoneinfo import ZoneInfo import pytest sys.path.insert(0, str(Path(__file__).resolve().parents[2])) from pipeline import NULL, JobQueue, Jobs, RunContext, job from pipeline.schedule import SCHEDULES, BadCadence, Cadence, declare ROOT = Path(__file__).resolve().parents[2] COPENHAGEN = ZoneInfo("Europe/Copenhagen") @job("s_tick", "A scheduled tick") def s_tick(config, store, *, context: RunContext = NULL, note: str = "") -> dict: store.append(note) return {"note": note} @job("s_solo", "One at a time", solo=True) def s_solo(config, store, *, context: RunContext = NULL) -> dict: return {} @job("s_needs", "Needing a payload") def s_needs(config, store, *, context: RunContext = NULL, target: str) -> dict: return {"target": target} @pytest.fixture def jobs(): with tempfile.TemporaryDirectory() as tmp: yield Jobs(Path(tmp) / "s.db") @pytest.fixture def queue(jobs): """A queue that is never started: the tests below fire it by hand. `tz="UTC"` so a cadence's next run is the same number wherever this runs. """ return JobQueue(jobs, {}, [], tz="UTC") @pytest.fixture def declared(): """Borrow the declaration registry and give it back. `SCHEDULES` is global, like `JOBS`, and a test that left something in it would give every later test in the session a schedule it never asked for. """ saved = dict(SCHEDULES) SCHEDULES.clear() yield SCHEDULES SCHEDULES.clear() SCHEDULES.update(saved) def ago(seconds: float) -> str: return (datetime.now(UTC) - timedelta(seconds=seconds)).isoformat() # ------------------------------------------------------------ the grammar WORDINGS = [ ("every 15m", "every 15m"), ("every 6h", "every 6h"), ("every 2d", "every 2d"), ("daily at 06:00", "daily at 06:00"), ("weekly mon 07:00", "weekly mon 07:00"), # Written how people write them, stored how the table stores them. ("weekly monday 07:00", "weekly mon 07:00"), ("Weekly Sunday 23:59", "weekly sun 23:59"), ("EVERY 15 M", "every 15m"), (" daily at 6:05 ", "daily at 06:05"), ("every 1d", "every 1d"), ] @pytest.mark.parametrize("typed,canonical", WORDINGS) def test_every_wording_parses_and_comes_back_canonical(typed, canonical): """Five forms, one spelling each. The round trip is the point: the table stores `str(cadence)`, so two people writing the same schedule two ways end up with one row that reads the same to both of them. """ cadence = Cadence.parse(typed) assert str(cadence) == canonical assert str(Cadence.parse(canonical)) == canonical assert Cadence.parse(typed) == Cadence.parse(canonical) def test_the_units_are_what_they_say(): assert Cadence.parse("every 15m").seconds == 900 assert Cadence.parse("every 6h").seconds == 6 * 3600 assert Cadence.parse("every 2d").seconds == 2 * 86400 # A clock cadence has no fixed length, and says so rather than guessing. assert Cadence.parse("daily at 06:00").seconds == 0 @pytest.mark.parametrize("wording", [ "sometimes", "", "cron 0 6 * * *", "every 15", "every m", "every 0h", "every 15s", "daily 06:00", "daily at 25:00", "daily at 06:60", "weekly 07:00", "weekly funday 07:00", "every 15m please", ]) def test_a_wording_outside_the_five_is_refused_with_all_five(wording): """And the refusal is the sentence a person is shown. It goes out of the API as `{"detail": ...}` and into a toast, so it has to say what to type instead rather than merely that this was wrong. """ with pytest.raises(ValueError) as refused: Cadence.parse(wording) said = str(refused.value) assert isinstance(refused.value, BadCadence) for form in ("every 15m", "every 6h", "every 2d", "daily at 06:00", "weekly mon 07:00"): assert form in said # ------------------------------------------------------------- next_after def test_next_after_is_strictly_after(): """A schedule fired at its own time must not find itself due again on the same instant, which would be one firing per tick for ever.""" at_six = datetime(2025, 6, 1, 6, 0, tzinfo=COPENHAGEN) when = Cadence.parse("daily at 06:00").next_after(at_six, COPENHAGEN) assert when.astimezone(COPENHAGEN) == at_six + timedelta(days=1) def test_next_after_always_comes_back_in_utc(): """Because it is written straight into the table, where every other time is UTC and the column is compared as text.""" when = Cadence.parse("daily at 06:00").next_after( datetime(2025, 6, 1, 12, 0, tzinfo=COPENHAGEN), COPENHAGEN) assert when.tzinfo is UTC assert when.isoformat().endswith("+00:00") def test_weekly_lands_on_the_day_it_names(): monday = Cadence.parse("weekly mon 07:00") # A Wednesday: the next Monday is five days away, not tomorrow. from_wed = monday.next_after(datetime(2025, 6, 4, 9, 0, tzinfo=COPENHAGEN), COPENHAGEN).astimezone(COPENHAGEN) assert from_wed.weekday() == 0 and (from_wed.hour, from_wed.minute) == (7, 0) assert from_wed.date() == datetime(2025, 6, 9).date() # Monday morning before seven: today, not next week. from_mon = monday.next_after(datetime(2025, 6, 9, 6, 0, tzinfo=COPENHAGEN), COPENHAGEN).astimezone(COPENHAGEN) assert from_mon.date() == datetime(2025, 6, 9).date() def test_daily_keeps_its_wall_clock_across_both_dst_changes(): """06:00 is 06:00 on both sides, which is the whole reason for the zone. Spring forward is a 23-hour day and autumn back is a 25-hour one, so the real elapsed time between two runs is not 24 hours — and it must not be, because what the wording promised is a time of day. """ daily = Cadence.parse("daily at 06:00") # Europe/Copenhagen, 2025: forward on 30 March, back on 26 October. forward = daily.next_after( datetime(2025, 3, 29, 6, 30, tzinfo=COPENHAGEN), COPENHAGEN) local = forward.astimezone(COPENHAGEN) assert (local.hour, local.minute) == (6, 0) assert local.date() == datetime(2025, 3, 30).date() # 05:30Z to 04:00Z: 22h30m of real time for a 23h30m step of the clock. assert forward - datetime(2025, 3, 29, 6, 30, tzinfo=COPENHAGEN) \ == timedelta(hours=22, minutes=30) back = daily.next_after( datetime(2025, 10, 25, 6, 30, tzinfo=COPENHAGEN), COPENHAGEN) local = back.astimezone(COPENHAGEN) assert (local.hour, local.minute) == (6, 0) assert local.date() == datetime(2025, 10, 26).date() assert back - datetime(2025, 10, 25, 6, 30, tzinfo=COPENHAGEN) \ == timedelta(hours=24, minutes=30) def test_weekly_keeps_its_wall_clock_across_a_dst_change(): when = Cadence.parse("weekly sun 07:00").next_after( datetime(2025, 3, 26, 9, 0, tzinfo=COPENHAGEN), COPENHAGEN) local = when.astimezone(COPENHAGEN) assert local.date() == datetime(2025, 3, 30).date() # the change itself assert (local.hour, local.minute) == (7, 0) def test_an_interval_is_elapsed_time_and_ignores_the_change(): """The other half of the promise, and the reason both wordings exist. `every 24h` across a spring-forward is 24 real hours and therefore lands an hour later on the clock. A person who wanted 06:00 wrote `daily`. """ start = datetime(2025, 3, 29, 6, 30, tzinfo=COPENHAGEN) when = Cadence.parse("every 24h").next_after(start, COPENHAGEN) assert when - start == timedelta(hours=24) assert when.astimezone(COPENHAGEN).hour == 7 def test_a_naive_moment_is_read_as_utc(): """Rather than raising. Every stamp in the table is UTC, so a caller that lost the tzinfo on the way is very nearly certainly holding one.""" naive = datetime(2025, 6, 1, 12, 0) assert Cadence.parse("every 1h").next_after(naive) \ == datetime(2025, 6, 1, 13, 0, tzinfo=UTC) # ---------------------------------------------------------------- the table def test_the_table_holds_a_schedule_through_its_life(jobs): row = jobs.add_schedule("nightly", "s_tick", "daily at 06:00", payload={"note": "hello"}, label="A nightly tick", next_run="2030-01-01T06:00:00+00:00") assert row is not None assert (row.name, row.kind, row.cadence) == ("nightly", "s_tick", "daily at 06:00") assert row.payload == {"note": "hello"} and row.enabled is True assert row.last_job_id == 0 and row.last_run == "" and row.last_state == "" assert [s.name for s in jobs.schedules()] == ["nightly"] assert jobs.schedule(row.id) == row edited = jobs.update_schedule(row.id, cadence="every 15m", enabled=False, payload={"note": "other"}) assert edited.cadence == "every 15m" and edited.enabled is False assert edited.payload == {"note": "other"} # Untouched columns stay as they were. assert edited.name == "nightly" and edited.label == "A nightly tick" assert jobs.delete_schedule(row.id) is True assert jobs.delete_schedule(row.id) is False # two tabs; not an error assert jobs.schedules() == [] def test_a_name_is_taken_once(jobs): assert jobs.add_schedule("one", "s_tick", "every 15m") is not None assert jobs.add_schedule("one", "s_tick", "every 6h", only_if_new=True) is None assert jobs.schedules()[0].cadence == "every 15m" def test_schedules_carry_the_state_of_their_last_job(jobs): row = jobs.add_schedule("one", "s_tick", "every 15m") queued = jobs.enqueue("s_tick") jobs.mark_fired(row.id, queued.id, "2030-01-01T00:00:00+00:00") assert jobs.schedules()[0].last_state == "queued" jobs.claim({"cpu": 1}) jobs.finish(queued.id, "failed", error="no") assert jobs.schedules()[0].last_state == "failed" # And the time it fired, which is not the time it is next due. listed = jobs.schedules()[0] assert listed.last_run and listed.next_run == "2030-01-01T00:00:00+00:00" def test_due_schedules_passes_over_what_is_off_or_not_yet(jobs): jobs.add_schedule("due", "s_tick", "every 15m", next_run=ago(60)) jobs.add_schedule("off", "s_tick", "every 15m", next_run=ago(60), enabled=False) jobs.add_schedule("later", "s_tick", "every 15m", next_run="2099-01-01T00:00:00+00:00") jobs.add_schedule("never", "s_tick", "every 15m") # no time at all due = jobs.due_schedules(datetime.now(UTC).isoformat()) assert [s.name for s in due] == ["due"] # --------------------------------------------------------------- the firing def test_the_tick_fires_a_due_row_and_moves_it_on(queue, jobs): row = jobs.add_schedule("regular", "s_tick", "every 15m", payload={"note": "scheduled"}, next_run=ago(30)) queue._fire_due() queued = jobs.snapshot().queued assert [j.kind for j in queued] == ["s_tick"] assert queued[0].payload == {"note": "scheduled"} after = jobs.schedule(row.id) assert after.last_job_id == queued[0].id assert after.last_run # Fifteen minutes from now, not from the moment it was due. assert after.next_run > datetime.now(UTC).isoformat() assert after.next_run < (datetime.now(UTC) + timedelta(minutes=16)).isoformat() def test_a_schedule_does_not_pile_up_behind_its_own_job(queue, jobs): """The job from the last firing is still queued, so this one is skipped. Skipped and *moved on*, not left due: a schedule whose job takes longer than its cadence would otherwise fire on every tick for as long as the job ran, and the queue would fill with copies of work already under way. """ row = jobs.add_schedule("regular", "s_tick", "every 15m", next_run=ago(30)) queue._fire_due() first = jobs.schedule(row.id).last_job_id jobs.update_schedule(row.id, next_run=ago(1)) # due again queue._fire_due() assert len(jobs.snapshot().queued) == 1 again = jobs.schedule(row.id) assert again.last_job_id == first # nothing new was queued assert again.next_run > datetime.now(UTC).isoformat() def test_a_solo_kind_already_underway_holds_its_schedule_back(queue, jobs): """The other way a schedule collides: not with itself, with somebody else. A button, a second schedule or a cascade started one, and `enqueue` would hand that job back — so the schedule would record work it did not ask for as its own firing. """ pressed = jobs.enqueue("s_solo", dedup_key="s_solo") row = jobs.add_schedule("solo", "s_solo", "every 15m", next_run=ago(30)) queue._fire_due() after = jobs.schedule(row.id) assert after.last_job_id == 0 # it never fired assert after.next_run > datetime.now(UTC).isoformat() assert [j.id for j in jobs.snapshot().queued] == [pressed.id] def test_downtime_is_caught_up_once_and_not_once_per_slot(queue, jobs): """Three days off an hourly schedule is one job, not seventy-two. The row carries one `next_run`, and a firing sets it from *now* rather than stepping through what was missed — so there is nowhere for a backlog to accumulate even in principle. """ row = jobs.add_schedule("hourly", "s_tick", "every 1h", next_run=ago(3 * 86400)) queue._fire_due() assert len(jobs.snapshot().queued) == 1 after = jobs.schedule(row.id) assert after.next_run > datetime.now(UTC).isoformat() assert after.next_run < (datetime.now(UTC) + timedelta(minutes=61)).isoformat() def test_an_unknown_kind_never_fires_and_keeps_its_place(queue, jobs): """A handler that went away in a deploy. The row stays exactly as it is — time included — so reinstalling the plugin runs it rather than starting another cadence from scratch.""" row = jobs.add_schedule("gone", "s_vanished", "every 15m", next_run=ago(30)) queue._fire_due() assert jobs.snapshot().queued == [] assert jobs.schedule(row.id).next_run == row.next_run queue._fire_due() # and it says so once assert queue._said_about == {(row.id, "kind")} def test_a_cadence_the_table_cannot_parse_never_fires(queue, jobs): """Only reachable by editing the file by hand, since every route parses first. It must not take the tick down with it.""" jobs.add_schedule("bent", "s_tick", "every 15m", next_run=ago(30)) jobs._write("UPDATE schedules SET cadence='sometimes'") queue._fire_due() assert jobs.snapshot().queued == [] assert jobs.schedules()[0].cadence == "sometimes" # left to be fixed def test_a_disabled_row_is_not_fired(queue, jobs): jobs.add_schedule("off", "s_tick", "every 15m", next_run=ago(30), enabled=False) queue._fire_due() assert jobs.snapshot().queued == [] def test_run_now_fires_without_moving_the_next_run(queue, jobs): row = jobs.add_schedule("nightly", "s_tick", "daily at 06:00", next_run="2099-01-01T06:00:00+00:00") started = queue.run_schedule(row.id) assert started is not None and started.kind == "s_tick" after = jobs.schedule(row.id) assert after.next_run == "2099-01-01T06:00:00+00:00" # But it did just run, and that is the job it queued — so the next tick # will not start a second one while this is still going. assert after.last_job_id == started.id and after.last_run # ------------------------------------------------------------ reconciliation def test_declared_defaults_get_a_row_on_start(declared, jobs): declare("nightly tick", "s_tick", "daily at 06:00", {"note": "declared"}, label="A tick, nightly") JobQueue(jobs, {}, [], tz="UTC")._reconcile_schedules() row = jobs.schedules()[0] assert (row.name, row.kind, row.cadence) == ("nightly tick", "s_tick", "daily at 06:00") assert row.payload == {"note": "declared"} and row.label == "A tick, nightly" assert row.next_run # and it knows when it is next def test_reconciliation_keeps_what_the_user_edited(declared, jobs): """The whole of what a declaration means: a starting point, not a setting. Switching a default off, or slowing it down, has to survive the next deploy — otherwise the only way to be rid of one is to edit the code and redeploy, which is not a thing the person running the app can do. """ declare("nightly tick", "s_tick", "daily at 06:00") queue = JobQueue(jobs, {}, [], tz="UTC") queue._reconcile_schedules() row = jobs.schedules()[0] jobs.update_schedule(row.id, cadence="every 2d", enabled=False, payload={"note": "mine"}) queue._reconcile_schedules() # a restart again = jobs.schedules()[0] assert again.id == row.id assert again.cadence == "every 2d" assert again.enabled is False assert again.payload == {"note": "mine"} def test_reconciliation_refreshes_the_label_and_nothing_else(declared, jobs): """The label is the declaration's: no screen edits it, so a relabel in the code has to reach an install that already has the row, or the old wording stays on screen forever. The cadence and the switch stay the user's.""" declare("nightly tick", "s_tick", "daily at 06:00", label="Old wording") queue = JobQueue(jobs, {}, [], tz="UTC") queue._reconcile_schedules() row = jobs.schedules()[0] jobs.update_schedule(row.id, cadence="every 2d", enabled=False) declared.clear() declare("nightly tick", "s_tick", "daily at 07:00", label="New wording") queue._reconcile_schedules() # a restart, after a deploy again = jobs.schedules()[0] assert again.id == row.id assert again.label == "New wording" assert (again.cadence, again.enabled) == ("every 2d", False) def test_a_row_whose_kind_was_changed_keeps_its_label(declared, jobs): """A row edited to run another kind is the declaration's in name only, and a label written for the declared job would misname it.""" declare("nightly", "s_tick", "daily at 06:00", label="A tick") queue = JobQueue(jobs, {}, [], tz="UTC") queue._reconcile_schedules() row = jobs.schedules()[0] jobs.update_schedule(row.id, kind="s_solo", label="One at a time") declared.clear() declare("nightly", "s_tick", "daily at 06:00", label="A tick, renamed") queue._reconcile_schedules() assert jobs.schedules()[0].label == "One at a time" def test_a_declaration_that_goes_away_leaves_its_row(declared, jobs): """Nothing here deletes. A deploy that silently forgot a schedule somebody was relying on is the failure this avoids; an orphan says `unknown kind` and waits to be deleted deliberately.""" declare("nightly tick", "s_tick", "daily at 06:00") queue = JobQueue(jobs, {}, [], tz="UTC") queue._reconcile_schedules() declared.clear() queue._reconcile_schedules() assert [s.name for s in jobs.schedules()] == ["nightly tick"] def test_declaring_one_name_twice_differently_is_refused(declared): declare("nightly", "s_tick", "daily at 06:00") declare("nightly", "s_tick", "daily at 06:00") # the same: fine with pytest.raises(ValueError) as refused: declare("nightly", "s_tick", "daily at 07:00") assert "nightly" in str(refused.value) def test_a_declared_cadence_is_parsed_where_it_is_written(declared): """At import, with a traceback pointing at the line — rather than at 06:00 in a log nobody is reading.""" with pytest.raises(ValueError): declare("bad", "s_tick", "every so often") assert "bad" not in declared # ------------------------------------------------------------- a real queue def test_a_started_queue_fires_what_is_overdue(jobs, declared): """The one test that drives the loop rather than the method. Everything above proves the firing decides correctly; this proves it is wired into `start()` at all, which is the wire nobody notices is missing until a schedule silently stops running. """ ran: list[str] = [] jobs.add_schedule("regular", "s_tick", "every 15m", payload={"note": "from the loop"}, next_run=ago(30)) queue = JobQueue(jobs, {}, ran, tz="UTC", workers={"cpu": 1}) assert queue.drain(timeout=10) is True assert ran == ["from the loop"] assert jobs.schedules()[0].last_state == "done" def test_a_queue_with_schedules_off_fires_nothing(jobs, declared): ran: list[str] = [] jobs.add_schedule("regular", "s_tick", "every 15m", next_run=ago(30)) queue = JobQueue(jobs, {}, ran, tz="UTC", schedules=False) assert queue.drain(timeout=10) is True assert ran == [] assert jobs.schedules()[0].last_job_id == 0 def test_an_unknown_timezone_is_a_warning_and_not_a_refusal(jobs, caplog): """A mistyped zone must not be the reason a worker will not start.""" queue = JobQueue(jobs, {}, [], tz="Mars/Olympus_Mons") assert queue.zone is not None assert Cadence.parse("daily at 06:00").next_after(datetime.now(UTC), queue.zone) # ------------------------------------------------------------------ the web @pytest.fixture def client(jobs, declared): pytest.importorskip("fastapi") from fastapi import FastAPI from fastapi.testclient import TestClient from pipeline.web import connect app = FastAPI() connect(app, jobs, {}, [], tz="UTC", schedules=False) with TestClient(app) as client: yield client def test_the_element_is_served(client): answer = client.get("/_jobs/schedules.js") assert answer.status_code == 200 assert "javascript" in answer.headers["content-type"] assert "desk-schedules" in answer.text def test_adding_listing_editing_and_deleting(client): added = client.post("/api/schedules", json={ "kind": "s_tick", "cadence": "weekly monday 07:00", "payload": {"note": "over http"}}) assert added.status_code == 201 row = added.json() # Named after its kind, since none was given, and stored canonically. assert row["name"] == "s_tick" and row["cadence"] == "weekly mon 07:00" assert row["next_run"] and row["unknown"] is False assert [s["id"] for s in client.get("/api/schedules").json()] == [row["id"]] off = client.patch(f"/api/schedules/{row['id']}", json={"enabled": False}) assert off.status_code == 200 and off.json()["enabled"] is False # And only that: a PATCH of one field does not resubmit the rest. assert off.json()["cadence"] == "weekly mon 07:00" assert off.json()["payload"] == {"note": "over http"} faster = client.patch(f"/api/schedules/{row['id']}", json={"cadence": "every 15m"}) assert faster.json()["cadence"] == "every 15m" # A new cadence moves the time with it, rather than leaving the old one. assert faster.json()["next_run"] != row["next_run"] gone = client.delete(f"/api/schedules/{row['id']}") assert gone.status_code == 200 and gone.json() == {"deleted": True} assert client.delete(f"/api/schedules/{row['id']}").json() == {"deleted": False} def test_a_second_schedule_of_one_kind_is_named_by_counting(client): first = client.post("/api/schedules", json={"kind": "s_tick", "cadence": "every 15m"}).json() second = client.post("/api/schedules", json={"kind": "s_tick", "cadence": "every 6h"}).json() assert (first["name"], second["name"]) == ("s_tick", "s_tick 2") def test_a_name_somebody_typed_is_refused_rather_than_altered(client): client.post("/api/schedules", json={"name": "nightly", "kind": "s_tick", "cadence": "every 15m"}) clash = client.post("/api/schedules", json={"name": "nightly", "kind": "s_tick", "cadence": "every 6h"}) assert clash.status_code == 409 assert "nightly" in clash.json()["detail"] def test_an_unregistered_kind_is_a_404_naming_what_there_is(client): refused = client.post("/api/schedules", json={"kind": "s_nonesuch", "cadence": "every 15m"}) assert refused.status_code == 404 detail = refused.json()["detail"] assert "s_nonesuch" in detail and "s_tick" in detail def test_a_payload_missing_what_the_handler_needs_is_a_422_naming_it(client): refused = client.post("/api/schedules", json={"kind": "s_needs", "cadence": "every 15m"}) assert refused.status_code == 422 assert "target" in refused.json()["detail"] # And with it, the same request is fine. assert client.post("/api/schedules", json={ "kind": "s_needs", "cadence": "every 15m", "payload": {"target": "somewhere"}}).status_code == 201 def test_a_bad_cadence_is_a_422_listing_the_accepted_wordings(client): refused = client.post("/api/schedules", json={"kind": "s_tick", "cadence": "sometimes"}) assert refused.status_code == 422 detail = refused.json()["detail"] assert isinstance(detail, str) for form in ("every 15m", "every 6h", "every 2d", "daily at 06:00", "weekly mon 07:00"): assert form in detail def test_an_edit_is_validated_the_same_way(client): row = client.post("/api/schedules", json={"kind": "s_tick", "cadence": "every 15m"}).json() assert client.patch(f"/api/schedules/{row['id']}", json={"cadence": "now and then"}).status_code == 422 assert client.patch(f"/api/schedules/{row['id']}", json={"kind": "s_needs"}).status_code == 422 assert client.patch("/api/schedules/9999", json={"enabled": False}).status_code == 404 def test_run_now_queues_the_job(client, jobs): row = client.post("/api/schedules", json={"kind": "s_tick", "cadence": "daily at 06:00"}).json() started = client.post(f"/api/schedules/{row['id']}/run") assert started.status_code == 202 assert started.json()["kind"] == "s_tick" after = client.get("/api/schedules").json()[0] assert after["last_job_id"] == started.json()["id"] assert after["next_run"] == row["next_run"] assert client.post("/api/schedules/9999/run").status_code == 404 def test_a_row_whose_handler_went_away_is_flagged_not_hidden(client, jobs): jobs.add_schedule("orphan", "s_vanished", "every 15m") row = next(s for s in client.get("/api/schedules").json() if s["name"] == "orphan") assert row["unknown"] is True # Listed and deletable, which is the whole of what can be done about it. assert client.delete(f"/api/schedules/{row['id']}").json() == {"deleted": True} def test_the_socket_pushes_the_schedule_list_on_every_change(client): """So a row edited in one tab redraws in the others without polling. The opening message is the *job* list — the socket is shared — which is why `` reads `/api/schedules` once on connect and follows this afterwards. """ with client.websocket_connect("/ws/jobs") as socket: assert socket.receive_json()["type"] == "jobs" added = client.post("/api/schedules", json={"kind": "s_tick", "cadence": "every 15m"}).json() pushed = _next_of(socket, "schedules") assert [s["id"] for s in pushed["schedules"]] == [added["id"]] assert pushed["schedules"][0]["cadence"] == "every 15m" client.patch(f"/api/schedules/{added['id']}", json={"cadence": "every 6h"}) assert _next_of(socket, "schedules")["schedules"][0]["cadence"] == "every 6h" client.delete(f"/api/schedules/{added['id']}") assert _next_of(socket, "schedules")["schedules"] == [] def _next_of(socket, type_: str, tries: int = 12) -> dict: """The next message of one type, stepping over the job pushes between.""" for _ in range(tries): message = socket.receive_json() if message["type"] == type_: return message raise AssertionError(f"no {type_} message arrived") # --------------------------------------------------------------- the worker def _worker(db: Path, marks: Path, *extra: str) -> subprocess.CompletedProcess: argv = [sys.executable, "-m", "pipeline.worker", "--db", str(db), "--bootstrap", "pipeline.tests.scheduled_handlers:marking", "--bootstrap-arg", str(marks), "--until-idle", *extra] return subprocess.run(argv, env=dict(os.environ, PYTHONPATH=str(ROOT)), cwd=ROOT, capture_output=True, timeout=120) def test_the_standalone_worker_fires_schedules(tmp_path): """A machine running only `python -m pipeline.worker` still gets its 06:00 job, which is what makes it a complete replacement for the web app's worker rather than half of one.""" db, marks = tmp_path / "w.db", tmp_path / "marks" marks.mkdir() jobs = Jobs(db) jobs.add_schedule("marker", "sw_mark", "every 1h", next_run=ago(60)) done = _worker(db, marks) assert done.returncode == 0, done.stdout.decode("utf-8", "replace") assert (marks / "fired").exists() assert jobs.schedules()[0].last_job_id def test_no_schedules_turns_it_off_for_a_second_worker(tmp_path): """One scheduler per file, as many workers as you like: the pile-up guard reads `last_job_id`, which neither of two simultaneous firings has written yet when the other looks.""" db, marks = tmp_path / "w.db", tmp_path / "marks" marks.mkdir() jobs = Jobs(db) jobs.add_schedule("marker", "sw_mark", "every 1h", next_run=ago(60)) done = _worker(db, marks, "--no-schedules") assert done.returncode == 0, done.stdout.decode("utf-8", "replace") assert not (marks / "fired").exists() assert jobs.schedules()[0].last_job_id == 0 # Not even the declared default was reconciled: nothing about schedules # happens in a worker that was told not to do them. assert [s.name for s in jobs.schedules()] == ["marker"]