From e2819fac675e736e667550f7854d9c7f8360baef Mon Sep 17 00:00:00 2001 From: ash Date: Wed, 21 Feb 2024 14:22:11 +0000 Subject: [PATCH] Return number of successful/failed resends from /resend-pending-builds --- softpack_core/service.py | 32 +++++++++++++++++++++---- tests/integration/test_resend_builds.py | 15 +++++++++++- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/softpack_core/service.py b/softpack_core/service.py index d8032f1..e9f11ab 100644 --- a/softpack_core/service.py +++ b/softpack_core/service.py @@ -10,7 +10,7 @@ import typer import uvicorn -from fastapi import APIRouter, Request, UploadFile +from fastapi import APIRouter, Request, Response, UploadFile from typer import Typer from typing_extensions import Annotated @@ -19,6 +19,7 @@ CreateEnvironmentSuccess, Environment, EnvironmentInput, + PackageInput, WriteArtifactSuccess, ) @@ -96,11 +97,32 @@ async def upload_artifacts( # type: ignore[no-untyped-def] @staticmethod @router.post("/resend-pending-builds") - async def resend_pending_builds(): # type: ignore[no-untyped-def] + async def resend_pending_builds( # type: ignore[no-untyped-def] + response: Response, + ): """Resubmit any pending builds to the builder.""" + successes = 0 + failures = 0 for env in Environment.iter(): if env.state != State.queued: continue - Environment.submit_env_to_builder(env) - - return {"message": "Successfully triggered resend"} + result = Environment.submit_env_to_builder( + EnvironmentInput( + name=env.name, + path=env.path, + description=env.description, + packages=[PackageInput(**vars(p)) for p in env.packages], + ) + ) + if result is None: + successes += 1 + else: + failures += 1 + + if failures == 0: + message = "Successfully triggered resends" + else: + response.status_code = 500 + message = "Failed to trigger all resends" + + return {"message": message, "successes": successes, "failures": failures} diff --git a/tests/integration/test_resend_builds.py b/tests/integration/test_resend_builds.py index 056fcce..bfe76c5 100644 --- a/tests/integration/test_resend_builds.py +++ b/tests/integration/test_resend_builds.py @@ -23,6 +23,8 @@ def test_resend_pending_builds( httpx_post, testable_env_input: EnvironmentInput ): + Environment.delete("test_environment", "users/test_user") + Environment.delete("test_environment", "groups/test_group") ServiceAPI.register() client = TestClient(app.router) @@ -40,7 +42,18 @@ def test_resend_pending_builds( url="/resend-pending-builds", ) assert resp.status_code == 200 - assert resp.json().get("message") == "Successfully triggered resend" + assert resp.json().get("message") == "Successfully triggered resends" + assert resp.json().get("successes") == 1 + assert resp.json().get("failures") == 0 httpx_post.assert_called_once() builder_called_correctly(httpx_post, testable_env_input) + + httpx_post.side_effect = Exception('could not contact builder') + resp = client.post( + url="/resend-pending-builds", + ) + assert resp.status_code == 500 + assert resp.json().get("message") == "Failed to trigger all resends" + assert resp.json().get("successes") == 0 + assert resp.json().get("failures") == 1