-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
services/api-server/src/simcore_service_api_server/services/rabbitmq.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import logging | ||
from typing import cast | ||
|
||
from fastapi import FastAPI | ||
from models_library.rabbitmq_messages import RabbitMessageBase | ||
from servicelib.rabbitmq import RabbitMQClient, wait_till_rabbitmq_responsive | ||
from settings_library.rabbit import RabbitSettings | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def setup_rabbitmq(app: FastAPI) -> None: | ||
settings: RabbitSettings = app.state.settings.PAYMENTS_RABBITMQ | ||
app.state.rabbitmq_client = None | ||
app.state.rabbitmq_rpc_server = None | ||
|
||
async def _on_startup() -> None: | ||
await wait_till_rabbitmq_responsive(settings.dsn) | ||
|
||
app.state.rabbitmq_client = RabbitMQClient( | ||
client_name="api_server", settings=settings | ||
) | ||
|
||
async def _on_shutdown() -> None: | ||
if app.state.rabbitmq_client: | ||
await app.state.rabbitmq_client.close() | ||
|
||
app.add_event_handler("startup", _on_startup) | ||
app.add_event_handler("shutdown", _on_shutdown) | ||
|
||
|
||
def get_rabbitmq_client(app: FastAPI) -> RabbitMQClient: | ||
assert app.state.rabbitmq_client # nosec | ||
return cast(RabbitMQClient, app.state.rabbitmq_client) | ||
|
||
|
||
async def post_message(app: FastAPI, message: RabbitMessageBase) -> None: | ||
await get_rabbitmq_client(app).publish(message.channel_name, message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# pylint: disable=protected-access | ||
# pylint: disable=redefined-outer-name | ||
# pylint: disable=too-many-arguments | ||
# pylint: disable=unused-argument | ||
# pylint: disable=unused-variable | ||
|
||
|
||
from fastapi import FastAPI | ||
from simcore_service_api_server.services.rabbitmq import ( | ||
get_rabbitmq_client, | ||
setup_rabbitmq, | ||
) | ||
|
||
pytest_simcore_core_services_selection = [ | ||
"rabbit", | ||
] | ||
pytest_simcore_ops_services_selection = [] | ||
|
||
|
||
def test_it(app: FastAPI): | ||
|
||
setup_rabbitmq(app) | ||
|
||
rabbit_client = get_rabbitmq_client(app) |