This commit is contained in:
Ross
2025-12-09 12:16:59 +00:00
parent c63d4d1585
commit e6943ccfc7
5 changed files with 76 additions and 4 deletions
+31 -3
View File
@@ -7,7 +7,7 @@ from django.utils import timezone
from .models import RotaRun, RotaSchedule
def _run_rota_job(run_id: int, use_external_generator: bool = False):
def _run_rota_job(run_id: int, use_external_generator: bool = False, generate_builder: bool = False, builder_solve: bool = False):
"""Background job that executes the scheduler and updates RotaRun.
If `use_external_generator` is True, callers should implement a project
@@ -47,6 +47,30 @@ def _run_rota_job(run_id: int, use_external_generator: bool = False):
"generated_at": timezone.now().isoformat(),
}
# Optionally build the RotaBuilder and attach generated HTML to the
# result. This allows the web UI to present the generated HTML for a
# run without requiring a separate on-demand build.
if generate_builder:
try:
rb = rota.to_rota_builder()
# build the model and optionally solve
rb.build_and_solve(solve=builder_solve)
html = rb.get_worker_timetable_html(include_html_tag=True)
# store under result so it's accessible from UI
result["builder_html"] = html
# Persist the HTML on the RotaRun record for easy retrieval
try:
run.export_html = html
run.save(update_fields=["export_html"])
except Exception:
# best-effort: if saving fails, include a log entry
run.log = (run.log or "") + "\nFailed to persist export_html"
except Exception as exc:
# include failure information in run.log and result
tb = traceback.format_exc()
run.log = (run.log or "") + "\n" + tb
result["builder_error"] = str(exc)
run.mark_finished(result=result)
except Exception:
@@ -59,7 +83,7 @@ def _run_rota_job(run_id: int, use_external_generator: bool = False):
print(tb)
def run_rota_async(rota_id: int, use_external_generator: bool = False) -> RotaRun:
def run_rota_async(rota_id: int, use_external_generator: bool = False, generate_builder: bool = False, builder_solve: bool = False) -> RotaRun:
"""Create a RotaRun and start background thread to run it.
Returns the created RotaRun instance.
@@ -68,7 +92,11 @@ def run_rota_async(rota_id: int, use_external_generator: bool = False) -> RotaRu
rota = RotaSchedule.objects.get(pk=rota_id)
run = RotaRun.objects.create(rota=rota)
thread = threading.Thread(target=_run_rota_job, args=(run.id, use_external_generator), daemon=True)
thread = threading.Thread(
target=_run_rota_job,
args=(run.id, use_external_generator, generate_builder, builder_solve),
daemon=True,
)
thread.start()
return run