Skip to content

Commit

Permalink
Merge pull request #7 from ioxiocom/feature/fail_better
Browse files Browse the repository at this point in the history
Catching more exceptions, reducing verbosity in case of errors and focusing on the important information
  • Loading branch information
lietu authored Feb 5, 2024
2 parents 1799bac + 67c2582 commit fd64255
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "multi-start"
version = "1.0.1"
version = "1.1.0"
description = "Run multiple services inside a docker container"
license = "BSD-3-Clause"
readme = "README.md"
Expand Down
27 changes: 20 additions & 7 deletions src/starter.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,13 @@ async def start_service(svc: Service, extra_env: Optional[dict] = None) -> Proce
return await run(svc.cmd, cwd=svc.cwd, extra_env=extra_env)


async def wait_for_service(svc: Service, timeout: float):
async def wait_for_service(svc: Service, timeout: float, name: str):
"""
Wait until a service and up and listening for either a port or socket.
Port takes precedence over a socket if both defined.
:param svc: Service to wait for
:param timeout: How long to wait in seconds
:param name: How to describe the service in exceptions
"""
if not svc.socket and not svc.port:
raise RuntimeError("Either socket or port should be defined")
Expand All @@ -146,14 +147,20 @@ async def wait_for_service(svc: Service, timeout: float):
for _ in range(max_attempts):
try:
await client.get(url, timeout=timeout)
except httpx.ConnectError:
except (
httpx.NetworkError,
httpx.TimeoutException,
httpx.HTTPStatusError,
) as e:
logger.debug(f"Caught {type(e).__name__} from {url}")
await asyncio.sleep(wait_step)
else:
logger.debug(f"Connection established: {url}")
break
else:
logger.error("Service is not responding", svc)
raise TimeoutError("Service is not responding")
raise TimeoutError(
f"{name.capitalize()} service is not responding, aborting..."
)


class Runner:
Expand All @@ -179,19 +186,25 @@ async def start(

if backend:
procs.append(await start_service(backend))
prerequisites.append(wait_for_service(backend, backend.timeout))
prerequisites.append(wait_for_service(backend, backend.timeout, "backend"))

if frontend:
frontend_svc = await start_service(
frontend, extra_env={"PORT": str(frontend.port)}
)
procs.append(frontend_svc)
prerequisites.append(wait_for_service(frontend, frontend.timeout))
prerequisites.append(
wait_for_service(frontend, frontend.timeout, "frontend")
)

if nginx:
prerequisites.append(setup_nginx())

await asyncio.gather(*prerequisites)
try:
await asyncio.gather(*prerequisites)
except TimeoutError as e:
logger.error(str(e))
return

if nginx:
procs.append(await start_service(nginx))
Expand Down

0 comments on commit fd64255

Please sign in to comment.