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

View file

@ -94,6 +94,18 @@ class Controller:
with contextlib.suppress(asyncio.TimeoutError): with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(self._wakeup_event.wait(), 10) 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: async def deployment_watcher(self) -> None:
self.reset() # empty the local db each time we start watching self.reset() # empty the local db each time we start watching
async for event_type, deployment in k8s.watch_deployments(): async for event_type, deployment in k8s.watch_deployments():
@ -119,11 +131,13 @@ class Controller:
if job_kind not in ("initialize", "cleanup"): if job_kind not in ("initialize", "cleanup"):
continue continue
if event_type in ("ADDED", "MODIFIED"): if event_type in ("ADDED", "MODIFIED"):
build = self.db.get(build_name) _logger.debug(
if build is None: "job %s for %s status %s", job_kind, build_name, job.status
# Not found in db, look in k8s api, in case the controller )
# is starting and the db has not been populated yet. # Look for build in local db and also in k8s api.
build = await Build.from_name(build_name) # 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: if build is None:
_logger.warning( _logger.warning(
f"Received job event for {build_name} " f"Received job event for {build_name} "