Skip to content

payments service: notification of payments to the front-end via socketio #5057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,30 @@ plugins:
- .js

exclude_patterns:
- "config/"
- "db/"
- "dist/"
- "features/"
- "**/node_modules/"
- "script"
- "**/spec/"
- "**/test/"
- "**/tests/"
- "**/vendor/"
- "**/*.d.ts"
- "**/.venv/"
- ".venv/"
- "**/healthcheck.py"
- "**/.venv/"
- "**/*.d.ts"
- "**/*.js"
- "**/client-sdk/"
- "**/generated_code/"
- "**/healthcheck.py"
- "**/migrations/"
- "**/*.js"
- "**/pytest-simcore/"
- "**/node_modules/"
- "**/pytest_plugin/"
- "**/pytest-simcore/"
- "**/sandbox/"
- "**/spec/"
- "**/test/"
- "**/tests/"
- "**/vendor/"
- "config/"
- "db/"
- "dist/"
- "features/"
- "script"
- "scripts/"
- packages/models-library/src/models_library/utils/_original_fastapi_encoders.py
- services/payments/scripts/example_payment_gateway.py
- services/resource-usage-tracker/src/simcore_service_resource_usage_tracker/modules/db/repositories/resource_tracker.py
- services/web/server/src/simcore_service_webserver/exporter/formatters/sds/xlsx/templates/code_description.py
- services/web/server/src/simcore_service_webserver/projects/db.py
- "scripts/"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from typing import Final

SOCKET_IO_PAYMENT_COMPLETED_EVENT: Final[str] = "paymentCompleted"
SOCKET_IO_PAYMENT_METHOD_ACKED_EVENT: Final[str] = "paymentMethodAcknoledged"
6 changes: 6 additions & 0 deletions packages/models-library/src/models_library/socketio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing import Any, TypedDict


class SocketMessageDict(TypedDict):
event_type: str
data: dict[str, Any]
43 changes: 43 additions & 0 deletions packages/service-library/src/servicelib/socketio_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
""" Common utilities for python-socketio library


NOTE: we intentionally avoided importing socketio here to avoid adding an extra dependency at
this level which would include python-socketio in all libraries
"""

import asyncio


async def cleanup_socketio_async_pubsub_manager(server_manager):

# NOTE: this is ugly. It seems though that python-socketio does not
# cleanup its background tasks properly.
# https://github.com/miguelgrinberg/python-socketio/discussions/1092
cancelled_tasks = []

if hasattr(server_manager, "thread"):
server_thread = server_manager.thread
assert isinstance(server_thread, asyncio.Task) # nosec
server_thread.cancel()
cancelled_tasks.append(server_thread)

if server_manager.publisher_channel:
await server_manager.publisher_channel.close()

if server_manager.publisher_connection:
await server_manager.publisher_connection.close()

current_tasks = asyncio.tasks.all_tasks()
for task in current_tasks:
coro = task.get_coro()
if any(
coro_name in coro.__qualname__ # type: ignore
for coro_name in [
"AsyncServer._service_task",
"AsyncSocket.schedule_ping",
"AsyncPubSubManager._thread",
]
):
task.cancel()
cancelled_tasks.append(task)
await asyncio.gather(*cancelled_tasks, return_exceptions=True)
4 changes: 2 additions & 2 deletions packages/service-library/src/servicelib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ def fire_and_forget_task(
task = asyncio.create_task(obj, name=f"fire_and_forget_task_{task_suffix_name}")
fire_and_forget_tasks_collection.add(task)

def log_exception_callback(fut: asyncio.Future):
def _log_exception_callback(fut: asyncio.Future):
try:
fut.result()
except asyncio.CancelledError:
_logger.warning("%s spawned as fire&forget was cancelled", fut)
except Exception: # pylint: disable=broad-except
_logger.exception("Error occurred while running task %s!", task.get_name())

task.add_done_callback(log_exception_callback)
task.add_done_callback(_log_exception_callback)
task.add_done_callback(fire_and_forget_tasks_collection.discard)
return task

Expand Down
9 changes: 1 addition & 8 deletions services/payments/gateway/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@ include ../../../scripts/common.Makefile



.PHONY: run-devel
run-devel: ## runs example_payment_gateway server
# SEE http://127.0.0.1:8000/docs
set -o allexport; source .env-secret; set +o allexport; \
uvicorn example_payment_gateway:the_app --reload

.PHONY: openapi.json
openapi.json: ## creates OAS
@set -o allexport; source .env-secret; set +o allexport; \
python example_payment_gateway.py openapi > $@
example_payment_gateway.py openapi > $@
Loading