Skip to content

Commit 8e22e11

Browse files
committed
settings
1 parent b425f76 commit 8e22e11

File tree

6 files changed

+68
-50
lines changed

6 files changed

+68
-50
lines changed

.env-devel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ DYNAMIC_SCHEDULER_UI_STORAGE_SECRET=adminadmin
133133

134134
FUNCTION_SERVICES_AUTHORS='{"UN": {"name": "Unknown", "email": "[email protected]", "affiliation": "unknown"}}'
135135

136-
136+
WEBSERVER_LICENSES={}
137+
LICENSES_ITIS_VIP_SYNCER_ENABLED=false
137138
ITIS_VIP_API_URL=https://itis.swiss/PD_DirectDownload/getDownloadableItems/{category}
138139
ITIS_VIP_CATEGORIES='{"HumanWholeBody": "Humans", "HumanBodyRegion": "Humans (Region)", "AnimalWholeBody": "Animal"}'
139140

@@ -375,7 +376,6 @@ WEBSERVER_GARBAGE_COLLECTOR=null
375376
WEBSERVER_GROUPS=1
376377
WEBSERVER_GUNICORN_CMD_ARGS=--timeout=180
377378
WEBSERVER_HOST=webserver
378-
WEBSERVER_LICENSES=true
379379
WEBSERVER_LOGIN={}
380380
WEBSERVER_LOGLEVEL=INFO
381381
WEBSERVER_META_MODELING=1

services/docker-compose.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ services:
705705
INVITATIONS_USERNAME: ${INVITATIONS_USERNAME}
706706

707707
WEBSERVER_LICENSES: ${WEBSERVER_LICENSES}
708+
LICENSES_ITIS_VIP_SYNCER_ENABLED : ${LICENSES_ITIS_VIP_SYNCER_ENABLED}
708709
ITIS_VIP_API_URL: ${ITIS_VIP_API_URL}
709710
ITIS_VIP_CATEGORIES: ${ITIS_VIP_CATEGORIES}
710711

@@ -901,7 +902,7 @@ services:
901902
WEBSERVER_GARBAGE_COLLECTOR: ${WB_DB_EL_GARBAGE_COLLECTOR}
902903
WEBSERVER_GROUPS: ${WB_DB_EL_GROUPS}
903904
WEBSERVER_INVITATIONS: ${WB_DB_EL_INVITATIONS}
904-
WEBSERVER_LICENSES: 0
905+
WEBSERVER_LICENSES: null
905906
WEBSERVER_LOGIN: ${WB_DB_EL_LOGIN}
906907
WEBSERVER_PAYMENTS: ${WB_DB_EL_PAYMENTS}
907908
WEBSERVER_META_MODELING: ${WB_DB_EL_META_MODELING}
@@ -1005,7 +1006,7 @@ services:
10051006
WEBSERVER_FRONTEND: ${WB_GC_FRONTEND}
10061007
WEBSERVER_GROUPS: ${WB_GC_GROUPS}
10071008
WEBSERVER_INVITATIONS: ${WB_GC_INVITATIONS}
1008-
WEBSERVER_LICENSES: 0
1009+
WEBSERVER_LICENSES: null
10091010
WEBSERVER_LOGIN: ${WB_GC_LOGIN}
10101011
WEBSERVER_META_MODELING: ${WB_GC_META_MODELING}
10111012
WEBSERVER_NOTIFICATIONS: ${WB_GC_NOTIFICATIONS}

services/web/server/src/simcore_service_webserver/application_settings.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from settings_library.tracing import TracingSettings
2727
from settings_library.utils_logging import MixinLoggingSettings
2828
from settings_library.utils_service import DEFAULT_AIOHTTP_PORT
29+
from simcore_service_webserver.licenses.settings import LicensesSettings
2930

3031
from ._constants import APP_SETTINGS_KEY
3132
from ._meta import API_VERSION, API_VTAG, APP_NAME
@@ -231,6 +232,13 @@ class ApplicationSettings(BaseApplicationSettings, MixinLoggingSettings):
231232
),
232233
]
233234

235+
WEBSERVER_LICENSES: Annotated[
236+
LicensesSettings | None,
237+
Field(
238+
json_schema_extra={"auto_default_from_env": True},
239+
),
240+
]
241+
234242
WEBSERVER_LOGIN: Annotated[
235243
LoginSettings | None,
236244
Field(
@@ -349,7 +357,6 @@ class ApplicationSettings(BaseApplicationSettings, MixinLoggingSettings):
349357
WEBSERVER_DB_LISTENER: bool = True
350358
WEBSERVER_FOLDERS: bool = True
351359
WEBSERVER_GROUPS: bool = True
352-
WEBSERVER_LICENSES: bool = False
353360
WEBSERVER_META_MODELING: bool = True
354361
WEBSERVER_NOTIFICATIONS: bool = True
355362
WEBSERVER_PRODUCTS: bool = True
@@ -520,7 +527,6 @@ def to_client_statics(self) -> dict[str, Any]:
520527
"SIMCORE_VCS_RELEASE_TAG": True,
521528
"SIMCORE_VCS_RELEASE_URL": True,
522529
"SWARM_STACK_NAME": True,
523-
"WEBSERVER_LICENSES": True,
524530
"WEBSERVER_LOGIN": {
525531
"LOGIN_ACCOUNT_DELETION_RETENTION_DAYS",
526532
"LOGIN_2FA_REQUIRED",

services/web/server/src/simcore_service_webserver/licenses/_itis_vip_syncer_service.py

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import asyncio
2+
import datetime
23
import logging
34
from datetime import timedelta
45

56
from aiohttp import web
67
from httpx import AsyncClient
78
from models_library.licenses import LicensedResourceType
8-
from pydantic import ValidationError
99
from servicelib.async_utils import cancel_wait_task
1010
from servicelib.background_task_utils import exclusive_periodic
1111
from servicelib.logging_utils import log_catch, log_context
@@ -89,46 +89,42 @@ async def sync_resources_with_licensed_items(
8989
_BACKGROUND_TASK_NAME = f"{__name__}.itis_vip_syncer_cleanup_ctx._periodic_sync"
9090

9191

92-
def setup_itis_vip_syncer(app: web.Application):
93-
categories = []
92+
def setup_itis_vip_syncer(
93+
app: web.Application, settings: ItisVipSettings, resync_after: datetime.timedelta
94+
):
95+
categories = settings.to_categories()
96+
if not categories:
97+
_logger.warning(
98+
"Skipping setup_itis_vip_syncer. %s did not provide any category", settings
99+
)
100+
return
94101

95-
try:
96-
settings = ItisVipSettings.create_from_envs()
97-
categories = settings.to_categories()
102+
setup_redis(app)
98103

99-
except ValidationError as err:
100-
_logger.warning("IT'IS VIP syncer disabled. Skipping. %s", err)
101-
return
104+
async def _lifespan(app_: web.Application):
105+
with (
106+
log_context(
107+
_logger,
108+
logging.INFO,
109+
f"IT'IS VIP syncing {len(categories)} categories",
110+
),
111+
log_catch(_logger, reraise=False),
112+
):
102113

103-
if categories:
104-
105-
async def _cleanup_ctx(app_: web.Application):
106-
with (
107-
log_context(
108-
_logger,
109-
logging.INFO,
110-
f"IT'IS VIP syncing {len(categories)} categories",
111-
),
112-
log_catch(_logger, reraise=False),
113-
):
114-
115-
@exclusive_periodic(
116-
get_redis_lock_manager_client_sdk(app_),
117-
task_interval=timedelta(minutes=1),
118-
retry_after=timedelta(minutes=2),
119-
)
120-
async def _periodic_sync() -> None:
121-
await sync_resources_with_licensed_items(
122-
app_, categories=categories
123-
)
114+
@exclusive_periodic(
115+
get_redis_lock_manager_client_sdk(app_),
116+
task_interval=resync_after,
117+
retry_after=timedelta(minutes=2),
118+
)
119+
async def _periodic_sync() -> None:
120+
await sync_resources_with_licensed_items(app_, categories=categories)
124121

125-
background_task = asyncio.create_task(
126-
_periodic_sync(), name=_BACKGROUND_TASK_NAME
127-
)
122+
background_task = asyncio.create_task(
123+
_periodic_sync(), name=_BACKGROUND_TASK_NAME
124+
)
128125

129-
yield
126+
yield
130127

131-
await cancel_wait_task(background_task)
128+
await cancel_wait_task(background_task)
132129

133-
setup_redis(app)
134-
app.cleanup_ctx.append(_cleanup_ctx)
130+
app.cleanup_ctx.append(_lifespan)

services/web/server/src/simcore_service_webserver/licenses/plugin.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
_licensed_items_rest,
1717
_rpc,
1818
)
19+
from .settings import LicensesSettings, get_plugin_settings
1920

2021
_logger = logging.getLogger(__name__)
2122

@@ -27,7 +28,7 @@
2728
logger=_logger,
2829
)
2930
def setup_licenses(app: web.Application):
30-
assert app[APP_SETTINGS_KEY].WEBSERVER_LICENSES # nosec
31+
settings: LicensesSettings = get_plugin_settings(app)
3132

3233
# routes
3334
setup_rest(app)
@@ -39,5 +40,9 @@ def setup_licenses(app: web.Application):
3940
if app[APP_SETTINGS_KEY].WEBSERVER_RABBITMQ:
4041
app.on_startup.append(_rpc.register_rpc_routes_on_startup)
4142

42-
# TODO: this is temporary
43-
_itis_vip_syncer_service.setup_itis_vip_syncer(app)
43+
if settings.LICENSES_ITIS_VIP_SYNCER_ENABLED and settings.LICENSES_ITIS_VIP:
44+
_itis_vip_syncer_service.setup_itis_vip_syncer(
45+
app,
46+
settings=settings.LICENSES_ITIS_VIP,
47+
resync_after=settings.LICENSES_ITIS_VIP_SYNCER_PERIODICITY,
48+
)
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
import datetime
22
from typing import Annotated
33

4+
from aiohttp import web
45
from pydantic import Field
6+
from servicelib.aiohttp.application_keys import APP_SETTINGS_KEY
57
from settings_library.base import BaseCustomSettings
68

79
from ._itis_vip_settings import ItisVipSettings
810

911

1012
class LicensesSettings(BaseCustomSettings):
11-
LICENSES_SYNCER_ENABLED: bool
12-
LICENSES_SYNCER_PERIODICITY: datetime.timedelta
13-
14-
# Registered licensed resources:
13+
# ITIS - VIP
1514
LICENSES_ITIS_VIP: Annotated[
1615
ItisVipSettings | None, Field(description="Settings for VIP license models")
1716
]
17+
LICENSES_ITIS_VIP_SYNCER_ENABLED: bool = False
18+
LICENSES_ITIS_VIP_SYNCER_PERIODICITY: datetime.timedelta = datetime.timedelta(
19+
days=1
20+
)
1821

1922
# other licensed resources come here ...
23+
24+
25+
def get_plugin_settings(app: web.Application) -> LicensesSettings:
26+
settings = app[APP_SETTINGS_KEY].WEBSERVER_LICENSES
27+
assert settings, "setup_settings not called?" # nosec
28+
assert isinstance(settings, LicensesSettings) # nosec
29+
return settings

0 commit comments

Comments
 (0)