2024-10-23 11:54:32 +03:00
|
|
|
import decimal
|
|
|
|
import threading
|
2024-12-13 16:45:21 +03:00
|
|
|
import asyncio
|
|
|
|
from collections.abc import Callable, Awaitable
|
2024-10-23 11:54:32 +03:00
|
|
|
import aio_pika
|
2025-05-11 10:49:39 +03:00
|
|
|
import asyncpg
|
2024-10-23 11:54:32 +03:00
|
|
|
|
2025-05-11 10:49:39 +03:00
|
|
|
from .repos.pg import set_setting, get_setting as pg_get_setting
|
2024-10-23 11:54:32 +03:00
|
|
|
from .repos.rmq import consume_setting_updates
|
|
|
|
|
|
|
|
|
2024-12-13 16:45:21 +03:00
|
|
|
|
2025-05-11 10:49:39 +03:00
|
|
|
def get_setting(pg: asyncpg.Connection, name: str) -> decimal.Decimal:
|
|
|
|
return pg_get_setting(pg, name)
|
2024-10-23 11:54:32 +03:00
|
|
|
|
2024-12-13 16:45:21 +03:00
|
|
|
|
2025-05-11 10:49:39 +03:00
|
|
|
async def start_thread(connect_pg: Callable[[], Awaitable[asyncpg.Pool]], rmq_connect_func: Callable[[], Awaitable[aio_pika.abc.AbstractRobustConnection]], *args):
|
|
|
|
pg_pool = await connect_pg()
|
|
|
|
conn = await rmq_connect_func()
|
|
|
|
try:
|
|
|
|
async with conn:
|
|
|
|
chan = await conn.channel()
|
|
|
|
await consume_setting_updates(pg_pool, set_setting, chan)
|
|
|
|
finally:
|
|
|
|
await pg_pool.close()
|
|
|
|
|
2024-10-23 11:54:32 +03:00
|
|
|
|
2025-05-11 10:49:39 +03:00
|
|
|
def launch_consumer(connect_pg: Callable[[], Awaitable[asyncpg.Pool]], rmq_connect_func: Callable[[], Awaitable[aio_pika.abc.AbstractRobustConnection]]):
|
|
|
|
t = threading.Thread(target=asyncio.run, args=(start_thread(connect_pg, rmq_connect_func),))
|
2024-10-23 11:54:32 +03:00
|
|
|
t.start()
|