Skip to content

Commit 70efe70

Browse files
authored
payments service: notification of payments to the front-end via socketio (#5057)
1 parent 482c544 commit 70efe70

File tree

27 files changed

+683
-917
lines changed

27 files changed

+683
-917
lines changed

.codeclimate.yml

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,30 @@ plugins:
5959
- .js
6060

6161
exclude_patterns:
62-
- "config/"
63-
- "db/"
64-
- "dist/"
65-
- "features/"
66-
- "**/node_modules/"
67-
- "script"
68-
- "**/spec/"
69-
- "**/test/"
70-
- "**/tests/"
71-
- "**/vendor/"
72-
- "**/*.d.ts"
73-
- "**/.venv/"
7462
- ".venv/"
75-
- "**/healthcheck.py"
63+
- "**/.venv/"
64+
- "**/*.d.ts"
65+
- "**/*.js"
7666
- "**/client-sdk/"
7767
- "**/generated_code/"
68+
- "**/healthcheck.py"
7869
- "**/migrations/"
79-
- "**/*.js"
80-
- "**/pytest-simcore/"
70+
- "**/node_modules/"
8171
- "**/pytest_plugin/"
72+
- "**/pytest-simcore/"
8273
- "**/sandbox/"
74+
- "**/spec/"
75+
- "**/test/"
76+
- "**/tests/"
77+
- "**/vendor/"
78+
- "config/"
79+
- "db/"
80+
- "dist/"
81+
- "features/"
82+
- "script"
83+
- "scripts/"
8384
- packages/models-library/src/models_library/utils/_original_fastapi_encoders.py
85+
- services/payments/scripts/example_payment_gateway.py
8486
- services/resource-usage-tracker/src/simcore_service_resource_usage_tracker/modules/db/repositories/resource_tracker.py
8587
- services/web/server/src/simcore_service_webserver/exporter/formatters/sds/xlsx/templates/code_description.py
8688
- services/web/server/src/simcore_service_webserver/projects/db.py
87-
- "scripts/"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from typing import Final
2+
3+
SOCKET_IO_PAYMENT_COMPLETED_EVENT: Final[str] = "paymentCompleted"
4+
SOCKET_IO_PAYMENT_METHOD_ACKED_EVENT: Final[str] = "paymentMethodAcknoledged"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from typing import Any, TypedDict
2+
3+
4+
class SocketMessageDict(TypedDict):
5+
event_type: str
6+
data: dict[str, Any]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
""" Common utilities for python-socketio library
2+
3+
4+
NOTE: we intentionally avoided importing socketio here to avoid adding an extra dependency at
5+
this level which would include python-socketio in all libraries
6+
"""
7+
8+
import asyncio
9+
10+
11+
async def cleanup_socketio_async_pubsub_manager(server_manager):
12+
13+
# NOTE: this is ugly. It seems though that python-socketio does not
14+
# cleanup its background tasks properly.
15+
# https://github.com/miguelgrinberg/python-socketio/discussions/1092
16+
cancelled_tasks = []
17+
18+
if hasattr(server_manager, "thread"):
19+
server_thread = server_manager.thread
20+
assert isinstance(server_thread, asyncio.Task) # nosec
21+
server_thread.cancel()
22+
cancelled_tasks.append(server_thread)
23+
24+
if server_manager.publisher_channel:
25+
await server_manager.publisher_channel.close()
26+
27+
if server_manager.publisher_connection:
28+
await server_manager.publisher_connection.close()
29+
30+
current_tasks = asyncio.tasks.all_tasks()
31+
for task in current_tasks:
32+
coro = task.get_coro()
33+
if any(
34+
coro_name in coro.__qualname__ # type: ignore
35+
for coro_name in [
36+
"AsyncServer._service_task",
37+
"AsyncSocket.schedule_ping",
38+
"AsyncPubSubManager._thread",
39+
]
40+
):
41+
task.cancel()
42+
cancelled_tasks.append(task)
43+
await asyncio.gather(*cancelled_tasks, return_exceptions=True)

packages/service-library/src/servicelib/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ def fire_and_forget_task(
7575
task = asyncio.create_task(obj, name=f"fire_and_forget_task_{task_suffix_name}")
7676
fire_and_forget_tasks_collection.add(task)
7777

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

86-
task.add_done_callback(log_exception_callback)
86+
task.add_done_callback(_log_exception_callback)
8787
task.add_done_callback(fire_and_forget_tasks_collection.discard)
8888
return task
8989

services/payments/gateway/Makefile

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ include ../../../scripts/common.Makefile
22

33

44

5-
.PHONY: run-devel
6-
run-devel: ## runs example_payment_gateway server
7-
# SEE http://127.0.0.1:8000/docs
8-
set -o allexport; source .env-secret; set +o allexport; \
9-
uvicorn example_payment_gateway:the_app --reload
10-
115
.PHONY: openapi.json
126
openapi.json: ## creates OAS
13-
@set -o allexport; source .env-secret; set +o allexport; \
14-
python example_payment_gateway.py openapi > $@
7+
example_payment_gateway.py openapi > $@

0 commit comments

Comments
 (0)