Skip to content

Commit

Permalink
Merge branch 'is1988/upgrading-director-to-fastapi' of github.com:pcr…
Browse files Browse the repository at this point in the history
…espov/osparc-simcore into is1988/upgrading-director-to-fastapi
  • Loading branch information
pcrespov committed Nov 4, 2024
2 parents 1ac2876 + 95446b9 commit b378463
Show file tree
Hide file tree
Showing 36 changed files with 31 additions and 4,013 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Final

from fastapi import FastAPI
from servicelib.client_session import persistent_client_session
from servicelib.fastapi.tracing import setup_tracing

from .. import registry_cache_task
Expand All @@ -13,6 +14,8 @@
APP_STARTED_BANNER_MSG,
)
from ..api.rest.routes import setup_api_routes
from ..monitoring import setup_app_monitoring
from ..registry_proxy import setup as setup_registry
from .settings import ApplicationSettings

_LOG_LEVEL_STEP = logging.CRITICAL - logging.ERROR
Expand Down Expand Up @@ -50,7 +53,11 @@ def create_app(settings: ApplicationSettings) -> FastAPI:
if app.state.settings.DIRECTOR_TRACING:
setup_tracing(app, app.state.settings.DIRECTOR_TRACING, APP_NAME)

# replace by httpx client
app.cleanup_ctx.append(persistent_client_session)
setup_registry(app)
registry_cache_task.setup(app)
setup_app_monitoring(app, "simcore_service_director")

# ERROR HANDLERS

Expand Down
14 changes: 6 additions & 8 deletions services/director/src/simcore_service_director/monitoring.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import prometheus_client
from aiohttp import web
from fastapi import FastAPI
from prometheus_client import CONTENT_TYPE_LATEST
from prometheus_client.registry import CollectorRegistry


from servicelib.monitor_services import ( # pylint: disable=no-name-in-module
from servicelib.monitor_services import (
add_instrumentation as add_services_instrumentation,
)

from . import config
from simcore_service_director.core.settings import ApplicationSettings

kCOLLECTOR_REGISTRY = f"{__name__}.collector_registry"

Expand All @@ -21,8 +18,9 @@ async def metrics_handler(request: web.Request):
return resp


def setup_app_monitoring(app: web.Application, app_name: str) -> None:
if not config.MONITORING_ENABLED:
def setup_app_monitoring(app: FastAPI, app_name: str) -> None:
app_settings: ApplicationSettings = app.state.settings
if not app_settings.DIRECTOR_MONITORING_ENABLED:
return
# app-scope registry
app[kCOLLECTOR_REGISTRY] = reg = CollectorRegistry(auto_describe=True)
Expand Down
25 changes: 18 additions & 7 deletions services/director/src/simcore_service_director/registry_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
from http import HTTPStatus
from pprint import pformat
from typing import Any, AsyncIterator
from typing import Any

from aiohttp import BasicAuth, ClientSession, client_exceptions
from aiohttp.client import ClientTimeout
Expand Down Expand Up @@ -43,7 +43,8 @@ async def _basic_auth_registry_request(
app: FastAPI, path: str, method: str, **session_kwargs
) -> tuple[dict, dict]:
if not config.REGISTRY_URL:
raise exceptions.DirectorException("URL to registry is not defined")
msg = "URL to registry is not defined"
raise exceptions.DirectorException(msg)

url = URL(
f"{'https' if config.REGISTRY_SSL else 'http'}://{config.REGISTRY_URL}{path}"
Expand Down Expand Up @@ -177,7 +178,7 @@ async def registry_request(
)


async def is_registry_responsive(app: FastAPI) -> bool:
async def _is_registry_responsive(app: FastAPI) -> bool:
path = "/v2/"
try:
await registry_request(
Expand All @@ -189,21 +190,31 @@ async def is_registry_responsive(app: FastAPI) -> bool:
return False


async def setup_registry(app: FastAPI) -> AsyncIterator[None]:
async def _setup_registry(app: FastAPI) -> None:
logger.debug("pinging registry...")

@retry(
wait=wait_fixed(2),
before_sleep=before_sleep_log(logger, logging.WARNING),
retry=retry_if_result(lambda result: result == False),
retry=retry_if_result(lambda result: result is False),
reraise=True,
)
async def wait_until_registry_responsive(app: FastAPI) -> bool:
return await is_registry_responsive(app)
return await _is_registry_responsive(app)

await wait_until_registry_responsive(app)
logger.info("Connected to docker registry")
yield


def setup(app: FastAPI) -> None:
async def on_startup() -> None:
await _setup_registry(app)

async def on_shutdown() -> None:
...

app.add_event_handler("startup", on_startup)
app.add_event_handler("shutdown", on_shutdown)


async def _list_repositories(app: FastAPI) -> list[str]:
Expand Down

This file was deleted.

Empty file.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit b378463

Please sign in to comment.