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
|
|
|
|
|
2024-12-13 16:45:21 +03:00
|
|
|
from .repos.in_memory_storage import set_setting, get_setting as ims_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
|
|
|
|
2024-10-23 11:54:32 +03:00
|
|
|
def get_setting(name: str) -> decimal.Decimal:
|
|
|
|
return ims_get_setting(name)
|
|
|
|
|
2024-12-13 16:45:21 +03:00
|
|
|
async def start_thread(rmq_connect_func: Callable[[], Awaitable[aio_pika.abc.AbstractRobustConnection]], *args):
|
|
|
|
conn = await rmq_connect_func()
|
|
|
|
async with conn:
|
|
|
|
chan = await conn.channel()
|
|
|
|
await consume_setting_updates(set_setting, chan)
|
|
|
|
|
2024-10-23 11:54:32 +03:00
|
|
|
|
2024-12-13 16:45:21 +03:00
|
|
|
def launch_consumer(rmq_connect_func: Callable[[], Awaitable[aio_pika.abc.AbstractRobustConnection]]):
|
|
|
|
t = threading.Thread(target=asyncio.run, args=(start_thread(rmq_connect_func),))
|
2024-10-23 11:54:32 +03:00
|
|
|
t.start()
|