Protect task diagnostics and stream incremental run output
Build custom container images / build (map[base_image:php:8-fpm-alpine build_args:PHP_VERSION=8 context:php8-pgsql fingerprint_command:{ apk info -v | LC_ALL=C sort; find /usr/local/lib/php/extensions /usr/local/etc/php/conf.d -type f -exec sha256sum {} + | LC_ALL=C sort; } name:ph… (push) Successful in 38s
Build custom container images / build (map[base_image:postgres:18 build_args:PG_VERSION=18 POSTGIS_VERSION=3 VCHORD_VERSION=0.5.3 context:postgres fingerprint_command:{ dpkg-query -W -f='${binary:Package}=${Version}\n' | LC_ALL=C sort; find /usr/lib/postgresql -type f -exec sha256su… (push) Successful in 47s
Build custom container images / build (map[base_image:python:3 build_args:PYTHON_VERSION=3 context:python-tools fingerprint_command:{ dpkg-query -W -f='${binary:Package}=${Version}\n' | LC_ALL=C sort; pip freeze | LC_ALL=C sort; } name:python-tools oci_labels:org.opencontainers.ima… (push) Successful in 1m24s
Build custom container images / build (map[base_image:python:3.12-slim build_args:PYTHON_VERSION=3.12-slim context:linkki-tiedotus fingerprint_command:{ dpkg-query -W -f='${binary:Package}=${Version}\n' | LC_ALL=C sort; pip freeze | LC_ALL=C sort; } name:linkki-tiedotus oci_labels:… (push) Successful in 38s
Build custom container images / build (map[base_image:python:3.12-slim build_args:PYTHON_VERSION=3.12-slim context:timepoll fingerprint_command:{ dpkg-query -W -f='${binary:Package}=${Version}\n' | LC_ALL=C sort; pip freeze | LC_ALL=C sort; } name:timepoll oci_labels:org.opencontai… (push) Successful in 43s

This commit is contained in:
ajp_anton committed 2026-10-26 23:39:50 +00:00
1 parent 0aa67439fd
commit 55bbbe2fd7
8 files changed
+319 -51

No files matched your search

@@ -0,0 +1,87 @@
import pytest
from app.web import create_app
from .conftest import write_task
from .test_runner import wait_for, wait_for_output
@pytest.fixture
def output_run(settings):
app = create_app(settings)
store = app.extensions["run_store"]
run, _ = store.enqueue(target_kind="task", target_path="example/task.py", queue_group="example")
store.claim_next("example")
path = settings.state_root / "test.log"
path.write_bytes(b"first\n")
with store._connect() as connection:
connection.execute("UPDATE runs SET log_path = ? WHERE id = ?", (str(path), run.id))
return app.test_client(), run.id, path, store
def test_log_polling_returns_only_appended_bytes_and_status(output_run):
client, run_id, path, store = output_run
first = client.get(f"/runs/{run_id}/output?offset=0").get_json()
assert first == {"output": "first\n", "offset": 6, "reset": False, "has_more": False, "status": "running"}
with path.open("ab") as stream:
stream.write("next: \u20ac\n".encode())
store.finish(run_id, status="succeeded")
next_chunk = client.get(f"/runs/{run_id}/output?offset=6").get_json()
assert next_chunk["output"] == "next: \u20ac\n"
assert next_chunk["offset"] == path.stat().st_size
assert next_chunk["status"] == "succeeded"
assert client.get(f"/runs/{run_id}/output").get_json() == {"output": "first\nnext: \u20ac\n"}
def test_incomplete_utf8_is_deferred_until_the_next_poll(output_run):
client, run_id, path, _ = output_run
path.write_bytes(b"ok\xe2\x82")
first = client.get(f"/runs/{run_id}/output?offset=0").get_json()
assert first["output"] == "ok"
assert first["offset"] == 2
with path.open("ab") as stream:
stream.write(b"\xac")
second = client.get(f"/runs/{run_id}/output?offset=2").get_json()
assert second["output"] == "\u20ac"
assert second["offset"] == 5
def test_truncated_log_resets_the_browser_offset(output_run):
client, run_id, path, _ = output_run
path.write_bytes(b"new")
result = client.get(f"/runs/{run_id}/output?offset=6").get_json()
assert result["reset"] is True
assert result["output"] == "new"
assert result["offset"] == 3
def test_large_log_is_drained_in_bounded_chunks(output_run):
client, run_id, path, store = output_run
path.write_bytes(b"x" * (1024 * 1024 - 1) + "\u20ac".encode())
store.finish(run_id, status="succeeded")
first = client.get(f"/runs/{run_id}/output?offset=0").get_json()
assert first["has_more"] is True
assert first["offset"] == 1024 * 1024 - 1
second = client.get(f"/runs/{run_id}/output?offset={first['offset']}").get_json()
assert second["output"] == "\u20ac"
assert second["has_more"] is False
@pytest.mark.parametrize("offset", ["-1", "abc", "1.5", "9" * 100, str(2**63)])
def test_invalid_log_offset_is_rejected(output_run, offset):
client, run_id, _, _ = output_run
assert client.get(f"/runs/{run_id}/output?offset={offset}").status_code == 400
def test_running_task_output_is_visible_before_completion(settings):
write_task(settings.task_root, "example", "long.py", 'import time\nprint("working", flush=True)\ntime.sleep(30)\n')
app = create_app(settings)
manager = app.extensions["run_manager"]
run, _ = manager.run_task(app.extensions["catalog"].task("example/long.py"))
try:
wait_for_output(settings.state_root / "runs" / f"{run.id}.log", "working")
response = app.test_client().get(f"/runs/{run.id}/output?offset=0").get_json()
assert response["status"] == "running"
assert response["output"] == "working\n"
finally:
manager.request_cancel(run.id)
wait_for(manager, run.id, {"cancelled"})
@@ -0,0 +1,95 @@
import json
import os
import time
from datetime import UTC, datetime, timedelta
from app.web import create_app
from .conftest import write_task
from .test_runner import wait_for
def test_active_inputs_and_artifacts_never_expire(manager, monkeypatch):
run, _ = manager.store.enqueue(target_kind="task", target_path="example/task.py", queue_group="example")
manager.store.claim_next("example")
root = manager.settings.state_root / "runs"
for name in (f"{run.id}.artifacts", f"{run.id}.uploads"):
path = root / name
path.mkdir(parents=True)
(path / "data").write_text("active")
os.utime(path, (time.time() - 3 * 86400,) * 2)
input_path = root / f"{run.id}.input.json"
input_path.write_text("{}")
monkeypatch.setattr(manager.store, "run_lifetimes", lambda: {})
manager.cleanup_transient_files()
assert input_path.exists()
assert (root / f"{run.id}.artifacts/data").exists()
assert (root / f"{run.id}.uploads/data").exists()
def test_artifact_retention_starts_at_completion_not_directory_creation(manager):
run, _ = manager.store.enqueue(target_kind="task", target_path="example/task.py", queue_group="example")
manager.store.finish(run.id, status="succeeded")
root = manager.settings.state_root / "runs"
artifact = root / f"{run.id}.artifacts"
artifact.mkdir(parents=True)
(artifact / "report.json").write_text("{}")
os.utime(artifact, (time.time() - 3 * 86400,) * 2)
(root / f"{run.id}.input.json").write_text("{}")
manager.cleanup_transient_files()
assert (artifact / "report.json").exists()
assert not (root / f"{run.id}.input.json").exists()
old = (datetime.now(UTC) - timedelta(days=3)).isoformat()
with manager.store._connect() as connection:
connection.execute("UPDATE runs SET finished_at = ? WHERE id = ?", (old, run.id))
manager.cleanup_transient_files()
assert not artifact.exists()
def test_failed_task_keeps_valid_diagnostics_but_not_incomplete_downloads(manager):
write_task(manager.settings.task_root, "example", "fail.py", '''
import os
from pathlib import Path
root = Path(os.environ["SERVER_MAINTENANCE_ARTIFACTS"])
(root / "report.json").write_text('{"completed_steps": 2}')
(root / "broken.json").write_text('{')
(root / "partial.zip").write_bytes(b"partial")
raise SystemExit(1)
''')
task = manager.catalog.task("example/fail.py")
run, _ = manager.run_task(task)
wait_for(manager, run.id, {"failed"})
assert manager.artifacts(run.id) == ["report.json"]
assert not (manager.settings.state_root / "runs" / f"{run.id}.input.json").exists()
def test_failed_json_report_is_accessible(settings):
app = create_app(settings)
manager = app.extensions["run_manager"]
run, _ = manager.store.enqueue(target_kind="task", target_path="example/fail.py", queue_group="example")
manager.store.finish(run.id, status="failed")
root = settings.state_root / "runs" / f"{run.id}.artifacts"
root.mkdir(parents=True)
(root / "report.json").write_text('{"completed_steps":2}')
client = app.test_client()
response = client.get(f"/runs/{run.id}/artifacts/report.json/view")
assert response.status_code == 200
assert json.loads(response.data) == {"completed_steps": 2}
def test_aborted_artifact_download_can_be_retried(settings):
app = create_app(settings)
manager = app.extensions["run_manager"]
run, _ = manager.store.enqueue(target_kind="task", target_path="example/archive.py", queue_group="example")
manager.store.finish(run.id, status="succeeded")
root = settings.state_root / "runs" / f"{run.id}.artifacts"
root.mkdir(parents=True)
path = root / "review.zip"
path.write_bytes(b"x" * 100_000)
client = app.test_client()
response = client.get(f"/runs/{run.id}/artifacts/review.zip", buffered=False)
next(iter(response.response))
response.close()
assert path.exists()
retry = client.get(f"/runs/{run.id}/artifacts/review.zip")
assert len(retry.data) == 100_000
assert not path.exists()