Skip to content

Commit 40490ad

Browse files
committed
🎨 Refactor long-running tasks context key to use LONG_RUNNING_TASKS_CONTEXT_REQKEY for consistency
1 parent e246233 commit 40490ad

File tree

7 files changed

+15
-17
lines changed

7 files changed

+15
-17
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from . import server
2-
from ._request import RQT_LONG_RUNNING_TASKS_CONTEXT_APPKEY
2+
from ._request import LONG_RUNNING_TASKS_CONTEXT_REQKEY
33

4-
__all__ = ("server", "RQT_LONG_RUNNING_TASKS_CONTEXT_APPKEY")
4+
__all__ = ("server", "LONG_RUNNING_TASKS_CONTEXT_REQKEY")

packages/service-library/src/servicelib/aiohttp/long_running_tasks/_request.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
from aiohttp import web
44

5-
RQT_LONG_RUNNING_TASKS_CONTEXT_APPKEY: Final = web.AppKey(
6-
"RQT_LONG_RUNNING_TASKS_CONTEXT", dict[str, Any]
7-
)
5+
LONG_RUNNING_TASKS_CONTEXT_REQKEY: Final = f"{__name__}.LONG_RUNNING_TASKS_CONTEXT"
86

97

108
def get_task_context(request: web.Request) -> dict[str, Any]:
11-
return request[RQT_LONG_RUNNING_TASKS_CONTEXT_APPKEY]
9+
ctx: dict[str, Any] = request[LONG_RUNNING_TASKS_CONTEXT_REQKEY]
10+
return ctx

packages/service-library/src/servicelib/aiohttp/long_running_tasks/_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
get_long_running_manager,
3737
)
3838
from ._request import (
39-
RQT_LONG_RUNNING_TASKS_CONTEXT_APPKEY,
39+
LONG_RUNNING_TASKS_CONTEXT_REQKEY,
4040
)
4141

4242

@@ -47,7 +47,7 @@ def _no_ops_decorator(handler: Handler):
4747
def _no_task_context_decorator(handler: Handler):
4848
@wraps(handler)
4949
async def _wrap(request: web.Request):
50-
request[RQT_LONG_RUNNING_TASKS_CONTEXT_APPKEY] = {}
50+
request[LONG_RUNNING_TASKS_CONTEXT_REQKEY] = {}
5151
return await handler(request)
5252

5353
return _wrap

packages/service-library/tests/aiohttp/long_running_tasks/test_long_running_tasks_with_task_context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from pytest_simcore.helpers.assert_checks import assert_status
2121
from servicelib.aiohttp import long_running_tasks, status
2222
from servicelib.aiohttp.long_running_tasks._request import (
23-
RQT_LONG_RUNNING_TASKS_CONTEXT_APPKEY,
23+
LONG_RUNNING_TASKS_CONTEXT_REQKEY,
2424
)
2525
from servicelib.aiohttp.requests_validation import parse_request_query_parameters_as
2626
from servicelib.aiohttp.rest_middlewares import append_rest_middlewares
@@ -56,7 +56,7 @@ async def _test_task_context_decorator(
5656
) -> web.StreamResponse:
5757
"""this task context callback tries to get the user_id from the query if available"""
5858
query_param = parse_request_query_parameters_as(query_model, request)
59-
request[RQT_LONG_RUNNING_TASKS_CONTEXT_APPKEY] = query_param.model_dump()
59+
request[LONG_RUNNING_TASKS_CONTEXT_REQKEY] = query_param.model_dump()
6060
return await handler(request)
6161

6262
return _test_task_context_decorator

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
from functools import wraps
33

44
from aiohttp import web
5-
from models_library.utils.fastapi_encoders import jsonable_encoder
65
from servicelib.aiohttp.long_running_tasks import (
7-
RQT_LONG_RUNNING_TASKS_CONTEXT_APPKEY,
6+
LONG_RUNNING_TASKS_CONTEXT_REQKEY,
87
)
98
from servicelib.aiohttp.long_running_tasks.server import setup
109
from servicelib.aiohttp.typing_extension import Handler
@@ -31,7 +30,7 @@ async def _test_task_context_decorator(
3130
) -> web.StreamResponse:
3231
"""this task context callback tries to get the user_id from the query if available"""
3332
req_ctx = AuthenticatedRequestContext.model_validate(request)
34-
request[RQT_LONG_RUNNING_TASKS_CONTEXT_APPKEY] = jsonable_encoder(req_ctx)
33+
request[LONG_RUNNING_TASKS_CONTEXT_REQKEY] = req_ctx.model_dump(mode="json")
3534
return await handler(request)
3635

3736
return _test_task_context_decorator

services/web/server/src/simcore_service_webserver/projects/_permalink_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import asyncio
22
import logging
3-
from typing import Final, Protocol, cast
3+
from typing import Final, Protocol
44

55
from aiohttp import web
66
from models_library.api_schemas_webserver.permalinks import ProjectPermalink
@@ -37,7 +37,7 @@ def register_factory(app: web.Application, factory_coro: CreateLinkCoroutine):
3737

3838
def _get_factory(app: web.Application) -> CreateLinkCoroutine:
3939
if _create := app.get(_PROJECT_PERMALINK_FACTORY_APPKEY):
40-
return cast(CreateLinkCoroutine, _create)
40+
return _create
4141

4242
msg = "Undefined permalink factory. Check plugin initialization."
4343
raise PermalinkFactoryError(msg)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from ..rabbitmq_settings import get_plugin_settings as get_rabbitmq_settings
1111
from . import _handlers
1212
from ._utils import (
13-
APP_CLIENT_SOCKET_SERVER_KEY,
13+
APP_CLIENT_SOCKET_SERVER_APPKEY,
1414
get_socket_server,
1515
register_socketio_handlers,
1616
)
@@ -30,7 +30,7 @@ async def _socketio_server_cleanup_ctx(app: web.Application) -> AsyncIterator[No
3030
)
3131
sio_server.attach(app)
3232

33-
app[APP_CLIENT_SOCKET_SERVER_KEY] = sio_server
33+
app[APP_CLIENT_SOCKET_SERVER_APPKEY] = sio_server
3434

3535
register_socketio_handlers(app, _handlers)
3636

0 commit comments

Comments
 (0)