Skip to content

Commit 0955166

Browse files
committed
refactor: rabbit appkeys
1 parent 9924fde commit 0955166

File tree

4 files changed

+18
-23
lines changed

4 files changed

+18
-23
lines changed

.github/prompts/aiohttp-appkey.prompt.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ Convert all string-based app key constants to use type-safe web.AppKey.
77

88
- Replace patterns like:
99
```python
10-
MY_APPKEY: Final[str] = f"{__name__}.my_key"
10+
CONSTNAME_APPKEY: Final[str] = f"{__name__}.my_key"
1111
```
1212
with:
1313
```python
1414
from aiohttp import web
15-
MY_APPKEY: Final = web.AppKey("MY_APPKEY", MySpecificType)
15+
CONSTNAME_APPKEY: Final = web.AppKey("CONSTNAME", ValueType)
1616
```
17-
(Replace MySpecificType with the actual type stored under this key.)
17+
(Replace ValueType with the actual type stored under this key.)
1818

1919
- Update all usages:
20-
- `app[MY_APPKEY] = value`
21-
- `data = app[MY_APPKEY]` or `data = request.app[MY_APPKEY]`
20+
- `app[CONSTNAME_APPKEY] = value`
21+
- `data = app[CONSTNAME_APPKEY]` or `data = request.app[CONSTNAME_APPKEY]`
2222

2323
- Key constant MUST be UPPERCASE
2424
- Key name MUST be suffixed `_APPKEY`

packages/service-library/src/servicelib/aiohttp/application_keys.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
"APP_FIRE_AND_FORGET_TASKS_KEY", set[asyncio.Task]
3434
)
3535

36-
APP_RABBITMQ_CLIENT_KEY: Final[str] = f"{__name__}.rabbit_client"
37-
APP_RABBITMQ_RPC_SERVER_KEY: Final[str] = f"{__name__}.rabbit_rpc_server"
3836

3937
#
4038
# web.Response keys, i.e. app[RSP_*_KEY]

services/web/server/src/simcore_service_webserver/products/_web_events.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import tempfile
33
from pathlib import Path
44
from pprint import pformat
5+
from typing import Final
56

67
from aiohttp import web
78
from models_library.products import ProductName

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import logging
22
from collections.abc import AsyncIterator
3-
from typing import Final, cast
3+
from typing import Final
44

55
from aiohttp import web
66
from models_library.errors import RABBITMQ_CLIENT_UNHEALTHY_MSG
7-
from servicelib.aiohttp.application_keys import (
8-
APP_RABBITMQ_CLIENT_KEY,
9-
APP_RABBITMQ_RPC_SERVER_KEY,
10-
)
117
from servicelib.logging_utils import log_context
128
from servicelib.rabbitmq import (
139
RabbitMQClient,
@@ -21,9 +17,9 @@
2117

2218
_logger = logging.getLogger(__name__)
2319

24-
_RABBITMQ_RPC_CLIENT_APPKEY: Final = web.AppKey(
25-
"_RABBITMQ_RPC_CLIENT_APPKEY", RabbitMQRPCClient
26-
)
20+
RABBITMQ_CLIENT_APPKEY: Final = web.AppKey("RABBITMQ_CLIENT", RabbitMQClient)
21+
RABBITMQ_RPC_SERVER_APPKEY: Final = web.AppKey("RABBITMQ_RPC_SERVER", RabbitMQRPCClient)
22+
RABBITMQ_RPC_CLIENT_APPKEY: Final = web.AppKey("RABBITMQ_RPC_CLIENT", RabbitMQRPCClient)
2723

2824

2925
async def _on_healthcheck_async_adapter(app: web.Application) -> None:
@@ -42,8 +38,8 @@ async def _rabbitmq_client_cleanup_ctx(app: web.Application) -> AsyncIterator[No
4238
with log_context(
4339
_logger, logging.INFO, msg=f"Connect RabbitMQ clients to {settings.dsn}"
4440
):
45-
app[APP_RABBITMQ_CLIENT_KEY] = RabbitMQClient("webserver", settings)
46-
app[APP_RABBITMQ_RPC_SERVER_KEY] = await RabbitMQRPCClient.create(
41+
app[RABBITMQ_CLIENT_APPKEY] = RabbitMQClient("webserver", settings)
42+
app[RABBITMQ_RPC_SERVER_APPKEY] = await RabbitMQRPCClient.create(
4743
client_name="webserver_rpc_server", settings=settings
4844
)
4945

@@ -55,8 +51,8 @@ async def _rabbitmq_client_cleanup_ctx(app: web.Application) -> AsyncIterator[No
5551

5652
# cleanup
5753
with log_context(_logger, logging.INFO, msg="Close RabbitMQ client"):
58-
await app[APP_RABBITMQ_CLIENT_KEY].close()
59-
await app[APP_RABBITMQ_RPC_SERVER_KEY].close()
54+
await app[RABBITMQ_CLIENT_APPKEY].close()
55+
await app[RABBITMQ_RPC_SERVER_APPKEY].close()
6056

6157

6258
async def _rabbitmq_rpc_client_lifespan(app: web.Application):
@@ -67,7 +63,7 @@ async def _rabbitmq_rpc_client_lifespan(app: web.Application):
6763

6864
assert rpc_client # nosec
6965

70-
app[_RABBITMQ_RPC_CLIENT_APPKEY] = rpc_client
66+
app[RABBITMQ_RPC_CLIENT_APPKEY] = rpc_client
7167

7268
yield
7369

@@ -87,12 +83,12 @@ def setup_rabbitmq(app: web.Application) -> None:
8783

8884

8985
def get_rabbitmq_rpc_client(app: web.Application) -> RabbitMQRPCClient:
90-
return cast(RabbitMQRPCClient, app[_RABBITMQ_RPC_CLIENT_APPKEY])
86+
return app[RABBITMQ_RPC_CLIENT_APPKEY]
9187

9288

9389
def get_rabbitmq_client(app: web.Application) -> RabbitMQClient:
94-
return cast(RabbitMQClient, app[APP_RABBITMQ_CLIENT_KEY])
90+
return app[RABBITMQ_CLIENT_APPKEY]
9591

9692

9793
def get_rabbitmq_rpc_server(app: web.Application) -> RabbitMQRPCClient:
98-
return cast(RabbitMQRPCClient, app[APP_RABBITMQ_RPC_SERVER_KEY])
94+
return app[RABBITMQ_RPC_SERVER_APPKEY]

0 commit comments

Comments
 (0)