Migrate from pip-install-e + setuptools to a uv workspace with a
committed uv.lock for deterministic deps. Switch both members to
hatchling, and move package sources into nested standard layout
(l4d2host/l4d2host/, l4d2web/l4d2web/) so builds work from a
read-only source tree — setuptools wrote egg-info to source under
the old layout, which broke uv sync on the root-owned /opt/left4me/src.
Local dev install: `pip install -e ./l4d2host -e ./l4d2web` -> `uv sync`.
.envrc switches from `layout python python3.13` to `use uv`. Python
pinned to 3.13 via .python-version.
l4d2web now declares its cross-dep on l4d2host explicitly via
[tool.uv.sources] (workspace = true). l4d2web/alembic.ini and
l4d2web/alembic/ stay at the project root (standard alembic layout).
Test fixes:
- tests/__init__.py added to both test dirs so pytest doesn't shadow
l4d2host as a namespace package via outer-dir walk.
- 3 CWD-relative paths in tests (l4d2web/static/css/{tokens,layout}.css
and js/sse.js) anchored to Path(__file__) so they survive layout
changes.
- Two test_install.py tests now monkeypatch HOME to tmp_path so they
stop silently mutating ~/.steam/sdk32 on every run.
628 tests pass under sandboxed `uv run pytest`.
Per docs/superpowers/plans/2026-05-15-uv-workspace-execution.md;
prereq for the ckn-bw bundle's uv-sync action (queued).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
129 lines
4.1 KiB
Python
129 lines
4.1 KiB
Python
from datetime import UTC, datetime
|
|
import time
|
|
|
|
from flask import Blueprint, Response, current_app, redirect, render_template, request
|
|
from sqlalchemy import select
|
|
|
|
from l4d2web.auth import current_user, is_safe_next, require_login
|
|
from l4d2web.db import session_scope
|
|
from l4d2web.models import Job, JobLog, Server, User
|
|
from l4d2web.services.job_worker import append_job_log
|
|
|
|
|
|
bp = Blueprint("job", __name__)
|
|
TERMINAL_JOB_STATES = {"succeeded", "failed", "cancelled"}
|
|
|
|
|
|
def format_sse_event(seq: int, event: str, data: str) -> str:
|
|
lines = [f"id: {seq}", f"event: {event}"]
|
|
for line in data.splitlines() or [""]:
|
|
lines.append(f"data: {line}")
|
|
return "\n".join(lines) + "\n\n"
|
|
|
|
|
|
def can_access_job(job: Job, user: User) -> bool:
|
|
if user.admin:
|
|
return True
|
|
if job.user_id is None:
|
|
return False
|
|
return job.user_id == user.id
|
|
|
|
|
|
@bp.get("/jobs/<int:job_id>")
|
|
@require_login
|
|
def job_detail(job_id: int) -> str | Response:
|
|
user = current_user()
|
|
assert user is not None
|
|
|
|
with session_scope() as db:
|
|
row = db.execute(
|
|
select(Job, User, Server)
|
|
.outerjoin(User, User.id == Job.user_id)
|
|
.outerjoin(Server, Server.id == Job.server_id)
|
|
.where(Job.id == job_id)
|
|
).first()
|
|
if row is None:
|
|
return Response(status=404)
|
|
job, owner, server = row
|
|
if not can_access_job(job, user):
|
|
return Response(status=403)
|
|
|
|
return render_template("job_detail.html", job=job, owner=owner, server=server)
|
|
|
|
|
|
@bp.post("/jobs/<int:job_id>/cancel")
|
|
@require_login
|
|
def cancel_job(job_id: int) -> Response:
|
|
user = current_user()
|
|
assert user is not None
|
|
|
|
next_url = request.form.get("next")
|
|
if not is_safe_next(next_url):
|
|
next_url = f"/jobs/{job_id}"
|
|
|
|
with session_scope() as db:
|
|
job = db.scalar(select(Job).where(Job.id == job_id))
|
|
if job is None:
|
|
return Response(status=404)
|
|
if not can_access_job(job, user):
|
|
return Response(status=403)
|
|
now = datetime.now(UTC)
|
|
if job.state == "queued":
|
|
job.state = "cancelled"
|
|
job.exit_code = 1
|
|
job.finished_at = now
|
|
job.updated_at = now
|
|
append_job_log(db, job.id, "stderr", "job cancelled before execution")
|
|
elif job.state == "running":
|
|
job.state = "cancelling"
|
|
job.updated_at = now
|
|
append_job_log(db, job.id, "stderr", "job cancellation requested; attempting to terminate running process")
|
|
elif job.state == "cancelling":
|
|
return redirect(next_url)
|
|
else:
|
|
return Response("job cannot be cancelled", status=409)
|
|
|
|
return redirect(next_url)
|
|
|
|
|
|
@bp.get("/jobs/<int:job_id>/stream")
|
|
@require_login
|
|
def stream_job(job_id: int) -> Response:
|
|
user = current_user()
|
|
assert user is not None
|
|
|
|
last_seq = int(request.args.get("last_seq") or request.headers.get("Last-Event-ID") or "0")
|
|
limit = int(current_app.config["JOB_LOG_REPLAY_LIMIT"])
|
|
poll_seconds = float(current_app.config.get("JOB_WORKER_POLL_SECONDS", 1))
|
|
|
|
with session_scope() as db:
|
|
job = db.scalar(select(Job).where(Job.id == job_id))
|
|
if job is None:
|
|
return Response(status=404)
|
|
if not can_access_job(job, user):
|
|
return Response(status=403)
|
|
|
|
def generate():
|
|
next_seq = last_seq
|
|
while True:
|
|
with session_scope() as db:
|
|
job = db.scalar(select(Job).where(Job.id == job_id))
|
|
if job is None:
|
|
return
|
|
rows = db.scalars(
|
|
select(JobLog)
|
|
.where(JobLog.job_id == job_id, JobLog.seq > next_seq)
|
|
.order_by(JobLog.seq)
|
|
.limit(limit)
|
|
).all()
|
|
terminal = job.state in TERMINAL_JOB_STATES
|
|
|
|
for row in rows:
|
|
next_seq = row.seq
|
|
yield format_sse_event(row.seq, row.stream, row.line)
|
|
|
|
if terminal and len(rows) < limit:
|
|
return
|
|
time.sleep(poll_seconds)
|
|
|
|
return Response(generate(), mimetype="text/event-stream")
|