Files
docker-images/python-tools/app/web.py
T
ajp_anton 3bfd199b9c
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 40s
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 49s
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 1m37s
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 36s
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 42s
Add cooperative task recovery and persistent live status
2026-10-29 22:12:04 +00:00

410 lines
16 KiB
Python

"""Flask application."""
from __future__ import annotations
import codecs
import ipaddress
import json
from contextlib import ExitStack
from datetime import datetime
from pathlib import Path
from flask import Flask, abort, redirect, render_template, request, send_file, url_for
from .config import Settings
from .discovery import TaskCatalog
from .inputs import PendingUpload, validate_inputs, validate_uploads
from .runner import RunManager, TaskConflictError
from .scheduler import BackgroundScheduler
from .store import RunStore
def create_app(settings: Settings | None = None) -> Flask:
settings = settings or Settings.from_environment()
catalog = TaskCatalog(settings.task_root)
store = RunStore(settings.database_path)
store.initialize()
manager = RunManager(settings, catalog, store)
scheduler = BackgroundScheduler(catalog, manager)
project_root = Path(__file__).resolve().parent.parent
app = Flask(
__name__,
template_folder=str(project_root / "templates"),
static_folder=str(project_root / "static"),
)
app.config["settings"] = settings
app.extensions["catalog"] = catalog
app.extensions["run_manager"] = manager
app.extensions["run_store"] = store
app.extensions["background_scheduler"] = scheduler
@app.before_request
def allow_only_proxy() -> None:
if not settings.allowed_proxy_ips:
return
remote_address = request.remote_addr
if remote_address is None:
abort(403)
try:
normalized = str(ipaddress.ip_address(remote_address))
except ValueError:
abort(403)
if normalized not in settings.allowed_proxy_ips:
abort(403)
@app.template_filter("timestamp")
def timestamp(value: str | None) -> str:
if not value:
return "—"
return datetime.fromisoformat(value).astimezone().strftime("%Y-%m-%d %H:%M:%S %Z")
def run_links(run):
if run is None:
return None
display_status = run.status
if run.recovery_of_run_id is not None and run.status in {"queued", "running"}:
display_status = "Recovery queued" if run.status == "queued" else "recovering"
return {
"run_id": run.id,
"status": run.status,
"display_status": display_status,
"recovery_of_run_id": run.recovery_of_run_id,
"artifacts_url": url_for("run_artifacts", run_id=run.id),
"detail_url": url_for("run_detail", run_id=run.id),
"output_url": url_for("run_output", run_id=run.id),
"state_url": url_for("run_state", run_id=run.id),
}
def task_state(task, active_runs=None):
state = manager.task_status(task, active_runs=active_runs)
return {
"active_run": run_links(state["active_run"]),
"blocked_by": run_links(state["blocked_by"]),
"progress": state["progress"],
}
def live_states(groups, latest):
active = store.active_runs()
tasks = {task.id: task_state(task, active) for group in groups for task in group.tasks}
group_states = {}
for group in groups:
run = latest[("group", group.id)]
active = run if run and run.status in {"queued", "running"} else None
blocker = next((
tasks[task.id]["active_run"] or tasks[task.id]["blocked_by"]
for task in group.tasks
if tasks[task.id]["active_run"] or tasks[task.id]["blocked_by"]
), None)
group_states[group.id] = {
"active_run": run_links(active),
"blocked_by": None if active else blocker,
"progress": manager.progress(active) if active else "",
}
return {"tasks": tasks, "groups": group_states}
def unavailable(error):
if isinstance(error, TaskConflictError):
return task_conflict(error)
message = str(error)
if request.accept_mimetypes.best == "application/json":
return {"error": message}, 503
return render_template("unavailable.html", message=message, blocking_run=None), 503
@app.errorhandler(TaskConflictError)
def task_conflict(error):
blocking_run = run_links(error.run)
message = f"Task blocked by run #{error.run.id} ({blocking_run['display_status']})."
if request.accept_mimetypes.best == "application/json":
return {"error": message, "blocked_by": blocking_run}, 409
return render_template("unavailable.html", message=message, blocking_run=blocking_run), 409
def render_index(
*,
form_values: dict[str, dict[str, list[str]]] | None = None,
input_errors: dict[str, dict[str, str]] | None = None,
status: int = 200,
):
groups = catalog.groups()
latest = {
("group", group.id): store.last_run("group", group.id) for group in groups
}
for group in groups:
for task in group.tasks:
latest[("task", task.id)] = store.last_run("task", task.id)
return (
render_template(
"index.html",
groups=groups,
latest=latest,
live_states=live_states(groups, latest),
form_values=form_values or {},
input_errors=input_errors or {},
),
status,
)
@app.get("/")
def index():
return render_index()
@app.get("/tasks/state")
def tasks_state():
groups = catalog.groups()
latest = {("group", group.id): store.last_run("group", group.id) for group in groups}
response = app.json.response(live_states(groups, latest))
response.headers["Cache-Control"] = "no-store"
return response
@app.post("/tasks/<path:task_id>/run")
def run_task(task_id: str):
task = catalog.task(task_id)
if task is None:
abort(404)
submitted_values = {
field.name: request.form.getlist(field.name) for field in task.inputs
}
input_data, errors = validate_inputs(task.inputs, submitted_values)
uploads, upload_errors = validate_uploads(task.inputs, request.files)
errors.update(upload_errors)
if errors:
if request.accept_mimetypes.best == "application/json":
return {"errors": errors}, 400
return render_index(
form_values={
task.id: {
field.name: [] if field.sensitive else submitted_values[field.name]
for field in task.inputs
}
},
input_errors={task.id: errors},
status=400,
)
try:
run, _ = manager.run_task(task, input_data, uploads)
except RuntimeError as error:
return unavailable(error)
if request.accept_mimetypes.best == "application/json":
return run_links(run), 202
return redirect(url_for("run_detail", run_id=run.id))
@app.post("/groups/<path:group_id>/run")
def run_group(group_id: str):
group = catalog.group(group_id)
if group is None or not group.can_run_as_group:
abort(404)
try:
run, _ = manager.run_group(group)
except RuntimeError as error:
return unavailable(error)
if request.accept_mimetypes.best == "application/json":
return run_links(run), 202
return redirect(url_for("run_detail", run_id=run.id))
@app.get("/runs/<int:run_id>")
def run_detail(run_id: int):
run = store.get(run_id)
if run is None:
abort(404)
execution_task = execution_replay_task(run)
output, offset, _, _ = read_run_output(run)
return render_template(
"run.html",
run=run,
children=store.children(run.id),
output=output,
output_offset=offset,
execution_task=execution_task,
recovery_run=run_links(store.recovery_for(run.id)),
progress=manager.progress(run),
display_status=run_links(run)["display_status"],
live_states={"tasks": {execution_task.id: task_state(execution_task)} if execution_task else {}, "groups": {}},
)
def execution_replay_task(run):
if run.status != "succeeded" or run.target_kind != "task":
return None
task = catalog.task(run.target_path)
if task is None or task.execution is None:
return None
if manager.review_upload_paths(run, task) is None:
return None
return task
@app.post("/runs/<int:run_id>/execute")
def execute_dry_run(run_id: int):
run = store.get(run_id)
if run is None:
abort(404)
task = execution_replay_task(run)
if task is None:
abort(404)
input_data = json.loads(run.input_json)
input_data[task.execution.field.name] = task.execution.execute_value
raw_values = {
name: ([str(item) for item in value] or [""]) if isinstance(value, list) else [str(value)]
for name, value in input_data.items()
}
validated, errors = validate_inputs(task.inputs, raw_values)
if errors:
abort(409)
paths = manager.review_upload_paths(run, task)
if paths is None:
abort(409, description="The reviewed upload is no longer available. Upload the file and review again.")
with ExitStack() as stack:
uploads = {}
for field in task.inputs:
if field.name not in paths:
continue
path = paths[field.name]
try:
stream = stack.enter_context(path.open("rb"))
except OSError:
abort(409, description="The reviewed upload is no longer available. Upload the file and review again.")
uploads[field.name] = PendingUpload(field.name, path.suffix, field.maximum_bytes, stream)
try:
executed, created = manager.run_task(task, validated, uploads)
except RuntimeError as error:
return unavailable(error)
if created and executed.status == "queued":
manager.discard_review_uploads(run.id)
if request.accept_mimetypes.best == "application/json":
return run_links(executed), 202
return redirect(url_for("run_detail", run_id=executed.id))
@app.post("/runs/<int:run_id>/cancel")
def cancel_run(run_id: int):
run = manager.request_cancel(run_id)
if run is None:
abort(404)
return redirect(url_for("run_detail", run_id=run.id))
@app.get("/runs/<int:run_id>/output")
def run_output(run_id: int):
run = store.get(run_id)
if run is None:
abort(404)
raw_offset = request.args.get("offset")
if raw_offset is None:
return {"output": read_run_output(run)[0]}
if not raw_offset.isdecimal() or len(raw_offset) > 19 or int(raw_offset) > 2**63 - 1:
abort(400)
output, offset, reset, has_more = read_run_output(run, int(raw_offset), 1024 * 1024)
return {
"output": output, "offset": offset, "reset": reset, "has_more": has_more,
"status": run.status,
"display_status": run_links(run)["display_status"],
"recovery_of_run_id": run.recovery_of_run_id,
"progress": manager.progress(run),
}
def read_run_output(run, offset: int = 0, maximum_bytes: int | None = None) -> tuple[str, int, bool, bool]:
if not run.log_path:
return "", 0, offset != 0, False
try:
with Path(run.log_path).open("rb") as stream:
stream.seek(0, 2)
size = stream.tell()
reset = offset > size
if reset:
offset = 0
stream.seek(offset)
data = stream.read() if maximum_bytes is None else stream.read(maximum_bytes)
except FileNotFoundError:
return "", 0, offset != 0, False
has_more = offset + len(data) < size
decoder = codecs.getincrementaldecoder("utf-8")("replace")
output = decoder.decode(data, final=run.status not in {"queued", "running"} and not has_more)
pending, _ = decoder.getstate()
return output, offset + len(data) - len(pending), reset, has_more
@app.get("/runs/<int:run_id>/artifacts")
def run_artifacts(run_id: int):
run = store.get(run_id)
if run is None:
abort(404)
artifacts = []
for name in manager.artifacts(run.id):
artifact = {
"name": name,
"url": url_for("download_artifact", run_id=run.id, filename=name),
}
if name.casefold().endswith(".json"):
artifact["view_url"] = url_for("view_artifact", run_id=run.id, filename=name)
artifacts.append(artifact)
return {"artifacts": artifacts}
@app.get("/runs/<int:run_id>/artifacts/<filename>/view")
def view_artifact(run_id: int, filename: str):
run = store.get(run_id)
if run is None or run.status in {"queued", "running"} or not filename.casefold().endswith(".json"):
abort(404)
artifact = manager.artifact_path(run.id, filename)
if artifact is None:
abort(404)
return send_file(artifact, mimetype="application/json", as_attachment=False, conditional=False)
@app.get("/runs/<int:run_id>/artifacts/<filename>")
def download_artifact(run_id: int, filename: str):
run = store.get(run_id)
if run is None or run.status in {"queued", "running"} or (run.status != "succeeded" and not filename.casefold().endswith(".json")):
abort(404)
artifact = manager.artifact_path(run.id, filename)
if artifact is None:
abort(404)
response = send_file(
artifact,
as_attachment=True,
download_name=artifact.name,
conditional=False,
)
original_response = response.response
def delete_after_transfer():
try:
yield from original_response
manager.delete_artifact(run.id, artifact.name)
finally:
close = getattr(original_response, "close", None)
if close is not None:
close()
response.response = delete_after_transfer()
response.direct_passthrough = False
return response
@app.get("/runs/<int:run_id>/state")
def run_state(run_id: int):
run = store.get(run_id)
if run is None:
abort(404)
return {
**run_links(run),
"progress": manager.progress(run),
"finished_at": run.finished_at,
"recovery_run": run_links(store.recovery_for(run.id)),
"exit_code": run.exit_code,
"cancel_requested_at": run.cancel_requested_at,
}
@app.get("/history/<target_kind>/<path:target_path>")
def history(target_kind: str, target_path: str):
if target_kind not in {"task", "group"}:
abort(404)
return render_template(
"history.html",
target_kind=target_kind,
target_path=target_path,
runs=store.history(target_kind, target_path),
)
try:
manager.startup()
manager.cleanup_transient_files()
scheduler.start()
except BaseException:
scheduler.stop()
manager.shutdown(timeout=5)
raise
return app