Factorize get build by name

This commit is contained in:
Stéphane Bidoul 2021-10-31 17:01:58 +01:00
parent f5d232c3af
commit 851916b087
No known key found for this signature in database
GPG key ID: BCAB2555446B5B92
2 changed files with 33 additions and 19 deletions

View file

@ -117,8 +117,8 @@ async def trigger_pull(org: str, repo: str, pr: int):
)
def _build_by_name(name: str) -> models.Build:
build = controller.db.get(name)
async def _build_by_name(name: str) -> models.Build:
build = await controller.get_build(name)
if build is None:
raise HTTPException(status.HTTP_404_NOT_FOUND)
return build
@ -126,7 +126,7 @@ def _build_by_name(name: str) -> models.Build:
@router.get("/builds/{name}", response_model=Build)
async def build(name: str):
return _build_by_name(name)
return await _build_by_name(name)
@router.get(
@ -135,7 +135,7 @@ async def build(name: str):
responses={200: {"content": {"text/plain": {}}}},
)
async def init_log(name: str):
# build = _build_by_name(name)
# build = await _build_by_name(name)
...
@ -152,19 +152,19 @@ async def log(name: str):
@router.post("/builds/{name}/start")
async def start(name: str):
"""Start the deployment."""
build = _build_by_name(name)
build = await _build_by_name(name)
await build.start()
@router.post("/builds/{name}/stop")
async def stop(name: str):
"""Stop the deployment."""
build = _build_by_name(name)
build = await _build_by_name(name)
await build.stop()
@router.delete("/builds/{name}", dependencies=[Depends(authenticated)])
async def delete(name: str):
"""Delete the deployment and drop the database."""
build = _build_by_name(name)
build = await _build_by_name(name)
await build.undeploy()

View file

@ -94,6 +94,18 @@ class Controller:
with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(self._wakeup_event.wait(), 10)
async def get_build(self, build_name: str, db_only: bool = True) -> Build | None:
build = self.db.get(build_name)
if build is not None:
return build
if not db_only:
build = await Build.from_name(build_name)
if build is not None:
if self.db.add(build):
self._wakeup()
return build
return None
async def deployment_watcher(self) -> None:
self.reset() # empty the local db each time we start watching
async for event_type, deployment in k8s.watch_deployments():
@ -119,19 +131,21 @@ class Controller:
if job_kind not in ("initialize", "cleanup"):
continue
if event_type in ("ADDED", "MODIFIED"):
build = self.db.get(build_name)
_logger.debug(
"job %s for %s status %s", job_kind, build_name, job.status
)
# Look for build in local db and also in k8s api.
# This is necessary because job events may come before build events
# have arrived.
build = await self.get_build(build_name, db_only=False)
if build is None:
# Not found in db, look in k8s api, in case the controller
# is starting and the db has not been populated yet.
build = await Build.from_name(build_name)
if build is None:
_logger.warning(
f"Received job event for {build_name} "
f"but the corresponding deployment is gone. "
f"Deleting all build resources."
)
await k8s.delete_resources(build_name)
continue
_logger.warning(
f"Received job event for {build_name} "
f"but the corresponding deployment is gone. "
f"Deleting all build resources."
)
await k8s.delete_resources(build_name)
continue
if job_kind == "initialize":
if job.status.active:
await build.on_initialize_started()