Skip to content

Commit

Permalink
Merge pull request #119 from chrisburr/settings-lifetime-func
Browse files Browse the repository at this point in the history
Add lifetime_function to ServiceSettingsBase
  • Loading branch information
chaen authored Oct 3, 2023
2 parents 248ae87 + 1d6f108 commit 4c5744b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/diracx/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"ServiceSettingsBase",
)

import contextlib
from pathlib import Path
from typing import TYPE_CHECKING, Any, Self, TypeVar
from typing import TYPE_CHECKING, Any, AsyncIterator, Self, TypeVar

from authlib.jose import JsonWebKey
from pydantic import AnyUrl, BaseSettings, SecretStr, parse_obj_as
Expand Down Expand Up @@ -58,3 +59,8 @@ class ServiceSettingsBase(BaseSettings, allow_mutation=False):
@classmethod
def create(cls) -> Self:
raise NotImplementedError("This should never be called")

@contextlib.asynccontextmanager
async def lifetime_function(self) -> AsyncIterator[None]:
"""A context manager that can be used to run code at startup and shutdown."""
yield
1 change: 1 addition & 0 deletions src/diracx/routers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def create_app_inner(
cls = type(service_settings)
assert cls not in available_settings_classes
available_settings_classes.add(cls)
app.lifetime_functions.append(service_settings.lifetime_function)
app.dependency_overrides[cls.create] = partial(lambda x: x, service_settings)

# Override the configuration source
Expand Down

0 comments on commit 4c5744b

Please sign in to comment.