Skip to content

Commit

Permalink
@GitHK review: using attrs instead of keys
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Jun 8, 2021
1 parent 7412233 commit 3979902
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/service-library/src/servicelib/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def shutdown_info(app: web.Application):
def create_safe_application(config: Optional[Dict] = None) -> web.Application:
app = web.Application()

# Enxures config entry
# Ensures config entry
app[APP_CONFIG_KEY] = config or {}

app.on_startup.append(startup_info)
Expand Down
2 changes: 1 addition & 1 deletion packages/service-library/src/servicelib/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


def setup_tracing(
app: web.Application, app_name: str, host: str, port: str, config: Dict
app: web.Application, app_name: str, host: str, port: int, config: Dict
) -> bool:
zipkin_address = f"{config['zipkin_endpoint']}/api/v2/spans"
endpoint = az.create_endpoint(app_name, ipv4=host, port=port)
Expand Down
11 changes: 6 additions & 5 deletions services/storage/src/simcore_service_storage/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from typing import Optional

from aiohttp import web
from pydantic import BaseSettings
from servicelib.application import create_safe_application
from servicelib.application import APP_CONFIG_KEY, create_safe_application
from servicelib.monitoring import setup_monitoring
from servicelib.tracing import setup_tracing

Expand All @@ -16,18 +15,20 @@
from .meta import WELCOME_MSG
from .rest import setup_rest
from .s3 import setup_s3
from .settings import Settings

log = logging.getLogger(__name__)


def create(settings: BaseSettings) -> web.Application:
def create(settings: Settings) -> web.Application:
log.debug(
"Initializing app with settings:\n%s",
settings.json(indent=2, sort_keys=True),
)

# TODO: tmp using dict() until webserver is also pydantic-compatible
app = create_safe_application(settings.dict())
app = create_safe_application(None)
app[APP_CONFIG_KEY] = settings

if settings.STORAGE_TRACING.enabled:
setup_tracing(
Expand All @@ -48,7 +49,7 @@ def create(settings: BaseSettings) -> web.Application:
return app


def run(settings: BaseSettings, app: Optional[web.Application] = None):
def run(settings: Settings, app: Optional[web.Application] = None):
log.debug("Serving application ")
if not app:
app = create(settings)
Expand Down
23 changes: 13 additions & 10 deletions services/storage/src/simcore_service_storage/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,35 @@

from .constants import APP_CONFIG_KEY, APP_DB_ENGINE_KEY
from .models import metadata
from .settings import PostgresSettings

log = logging.getLogger(__name__)


async def pg_engine(app: web.Application):
pg_cfg = app[APP_CONFIG_KEY]["STORAGE_POSTGRES"]
engine = None

pg_cfg: PostgresSettings = app[APP_CONFIG_KEY].STORAGE_POSTGRES
dsn = DataSourceName(
application_name=f"{__name__}_{id(app)}",
database=pg_cfg["db"],
user=pg_cfg["user"],
password=pg_cfg["password"].get_secret_value(),
host=pg_cfg["host"],
port=pg_cfg["port"],
)
database=pg_cfg.db,
user=pg_cfg.user,
password=pg_cfg.password.get_secret_value(),
host=pg_cfg.host,
port=pg_cfg.port,
) # type: ignore

log.info("Creating pg engine for %s", dsn)
async for attempt in AsyncRetrying(
**PostgresRetryPolicyUponInitialization(log).kwargs
):
with attempt:
engine = await create_pg_engine(
dsn, minsize=pg_cfg["minsize"], maxsize=pg_cfg["maxsize"]
dsn, minsize=pg_cfg.minsize, maxsize=pg_cfg.maxsize
)
await raise_if_not_responsive(engine)

if app[APP_CONFIG_KEY]["STORAGE_TESTING"]:
if app[APP_CONFIG_KEY].STORAGE_TESTING:
log.info("Initializing tables for %s", dsn)
init_pg_tables(dsn, schema=metadata)

Expand Down Expand Up @@ -78,7 +81,7 @@ def get_engine_state(app: web.Application) -> Dict[str, Any]:


def setup_db(app: web.Application):
if "postgres" in app[APP_CONFIG_KEY]["STORAGE_DISABLE_SERVICES"]:
if "postgres" in app[APP_CONFIG_KEY].STORAGE_DISABLE_SERVICES:
app[APP_DB_ENGINE_KEY] = None
log.warning("Service '%s' explicitly disabled in config", "postgres")
return
Expand Down
4 changes: 2 additions & 2 deletions services/storage/src/simcore_service_storage/db_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ async def get_api_token_and_secret(app: web.Application, userid) -> Tuple[str, s
engine = app.get(APP_DB_ENGINE_KEY, None)

# defaults from config if any, othewise None
api_token = app[APP_CONFIG_KEY]["BF_API_KEY"]
api_secret = app[APP_CONFIG_KEY]["BF_API_SECRET"]
api_token = app[APP_CONFIG_KEY].BF_API_KEY
api_secret = app[APP_CONFIG_KEY].BF_API_SECRET

if engine:
try:
Expand Down
4 changes: 2 additions & 2 deletions services/storage/src/simcore_service_storage/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def setup_s3(app: web.Application):
"""minio/s3 service setup"""

log.debug("Setting up %s ...", __name__)
STORAGE_DISABLE_SERVICES = app[APP_CONFIG_KEY]["STORAGE_DISABLE_SERVICES"]
STORAGE_DISABLE_SERVICES = app[APP_CONFIG_KEY].STORAGE_DISABLE_SERVICES

if "s3" in STORAGE_DISABLE_SERVICES:
log.warning("Service '%s' explicitly disabled in config", "s3")
Expand All @@ -79,5 +79,5 @@ def setup_s3(app: web.Application):


def get_config_s3(app: web.Application) -> Dict:
cfg = app[APP_CONFIG_KEY]["STORAGE_S3"]
cfg = app[APP_CONFIG_KEY].STORAGE_S3
return cfg
2 changes: 1 addition & 1 deletion services/storage/tests/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def client(
settings = Settings.create_from_env()
print(settings.json(indent=2))

app[APP_CONFIG_KEY] = settings.dict()
app[APP_CONFIG_KEY] = settings

setup_db(app)
setup_rest(app)
Expand Down

0 comments on commit 3979902

Please sign in to comment.