forked from ITISFoundation/osparc-simcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into buy-credits
- Loading branch information
Showing
19 changed files
with
472 additions
and
51 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
8 changes: 8 additions & 0 deletions
8
packages/models-library/src/models_library/api_schemas_directorv2/notifications.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,8 @@ | ||
from models_library.projects_nodes_io import NodeID | ||
from models_library.wallets import WalletID | ||
from pydantic import BaseModel | ||
|
||
|
||
class ServiceNoMoreCredits(BaseModel): | ||
node_id: NodeID | ||
wallet_id: WalletID |
3 changes: 3 additions & 0 deletions
3
packages/models-library/src/models_library/api_schemas_directorv2/socketio.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,3 @@ | ||
from typing import Final | ||
|
||
SOCKET_IO_SERVICE_NO_MORE_CREDITS_EVENT: Final[str] = "serviceNoMoreCredits" |
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 |
---|---|---|
|
@@ -29,6 +29,7 @@ networkx | |
ordered-set | ||
orjson | ||
pydantic[dotenv] | ||
python-socketio | ||
redis | ||
rich | ||
tenacity |
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
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
59 changes: 59 additions & 0 deletions
59
services/director-v2/src/simcore_service_director_v2/modules/notifier.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,59 @@ | ||
import contextlib | ||
|
||
import socketio | ||
from fastapi import FastAPI | ||
from fastapi.encoders import jsonable_encoder | ||
from models_library.api_schemas_directorv2.notifications import ServiceNoMoreCredits | ||
from models_library.api_schemas_directorv2.socketio import ( | ||
SOCKET_IO_SERVICE_NO_MORE_CREDITS_EVENT, | ||
) | ||
from models_library.api_schemas_webserver.socketio import SocketIORoomStr | ||
from models_library.projects_nodes_io import NodeID | ||
from models_library.users import UserID | ||
from models_library.wallets import WalletID | ||
from servicelib.fastapi.app_state import SingletonInAppStateMixin | ||
|
||
|
||
class Notifier(SingletonInAppStateMixin): | ||
app_state_name: str = "notifier" | ||
|
||
def __init__(self, sio_manager: socketio.AsyncAioPikaManager): | ||
self._sio_manager = sio_manager | ||
|
||
async def notify_shutdown_no_more_credits( | ||
self, user_id: UserID, node_id: NodeID, wallet_id: WalletID | ||
) -> None: | ||
await self._sio_manager.emit( | ||
SOCKET_IO_SERVICE_NO_MORE_CREDITS_EVENT, | ||
data=jsonable_encoder( | ||
ServiceNoMoreCredits(node_id=node_id, wallet_id=wallet_id) | ||
), | ||
room=SocketIORoomStr.from_user_id(user_id), | ||
) | ||
|
||
|
||
async def publish_shutdown_no_more_credits( | ||
app: FastAPI, *, user_id: UserID, node_id: NodeID, wallet_id: WalletID | ||
) -> None: | ||
notifier: Notifier = Notifier.get_from_app_state(app) | ||
await notifier.notify_shutdown_no_more_credits( | ||
user_id=user_id, node_id=node_id, wallet_id=wallet_id | ||
) | ||
|
||
|
||
def setup(app: FastAPI): | ||
async def _on_startup() -> None: | ||
assert app.state.external_socketio # nosec | ||
|
||
notifier = Notifier( | ||
sio_manager=app.state.external_socketio, | ||
) | ||
notifier.set_to_app_state(app) | ||
assert Notifier.get_from_app_state(app) == notifier # nosec | ||
|
||
async def _on_shutdown() -> None: | ||
with contextlib.suppress(AttributeError): | ||
Notifier.pop_from_app_state(app) | ||
|
||
app.add_event_handler("startup", _on_startup) | ||
app.add_event_handler("shutdown", _on_shutdown) |
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
33 changes: 33 additions & 0 deletions
33
services/director-v2/src/simcore_service_director_v2/modules/socketio.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,33 @@ | ||
import logging | ||
|
||
import socketio | ||
from fastapi import FastAPI | ||
from servicelib.socketio_utils import cleanup_socketio_async_pubsub_manager | ||
|
||
from ..core.settings import AppSettings | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def setup(app: FastAPI): | ||
settings: AppSettings = app.state.settings | ||
|
||
async def _on_startup() -> None: | ||
assert app.state.rabbitmq_client # nosec | ||
|
||
# Connect to the as an external process in write-only mode | ||
# SEE https://python-socketio.readthedocs.io/en/stable/server.html#emitting-from-external-processes | ||
app.state.external_socketio = socketio.AsyncAioPikaManager( | ||
url=settings.DIRECTOR_V2_RABBITMQ.dsn, | ||
logger=_logger, | ||
write_only=True, | ||
) | ||
|
||
async def _on_shutdown() -> None: | ||
if external_socketio := getattr(app.state, "external_socketio"): # noqa: B009 | ||
await cleanup_socketio_async_pubsub_manager( | ||
server_manager=external_socketio | ||
) | ||
|
||
app.add_event_handler("startup", _on_startup) | ||
app.add_event_handler("shutdown", _on_shutdown) |
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
Oops, something went wrong.