fix(fetch_active_tenants): propper async API call
This commit is contained in:
parent
abde776cd1
commit
f723bcb9b1
@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
from typing import Set
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from aio_pika import connect_robust, ExchangeType, Message
|
from aio_pika import connect_robust, ExchangeType, Message
|
||||||
from aio_pika.abc import AbstractIncomingMessage
|
from aio_pika.abc import AbstractIncomingMessage
|
||||||
@ -23,6 +24,7 @@ class RabbitMQHandler:
|
|||||||
self.service_request_exchange_name = "service_request_exchange" # INPUT
|
self.service_request_exchange_name = "service_request_exchange" # INPUT
|
||||||
self.service_response_exchange_name = "service_response_exchange" # OUTPUT
|
self.service_response_exchange_name = "service_response_exchange" # OUTPUT
|
||||||
self.service_dead_letter_queue_name = "service_dlq"
|
self.service_dead_letter_queue_name = "service_dlq"
|
||||||
|
self.queue_expiration_time = 300000
|
||||||
self.connection = None
|
self.connection = None
|
||||||
self.channel = None
|
self.channel = None
|
||||||
self.tenant_exchange = None
|
self.tenant_exchange = None
|
||||||
@ -112,29 +114,22 @@ class RabbitMQHandler:
|
|||||||
tenant_id = message.routing_key
|
tenant_id = message.routing_key
|
||||||
await self.output_exchange.publish(Message(body=processed_content.encode()), routing_key=tenant_id)
|
await self.output_exchange.publish(Message(body=processed_content.encode()), routing_key=tenant_id)
|
||||||
|
|
||||||
# FIXME: coroutine error
|
async def fetch_active_tenants(self) -> Set[str]:
|
||||||
async def fetch_active_tenants(self):
|
try:
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(self.tenant_service_url) as response:
|
async with session.get(self.tenant_service_url) as response:
|
||||||
if response.status == 200 and response.headers["content-type"].lower() == "application/json":
|
if response.status == 200:
|
||||||
tenants = {await tenant["tenantId"] for tenant in response.json()}
|
data = await response.json()
|
||||||
return await tenants
|
return {tenant["tenantId"] for tenant in data}
|
||||||
else:
|
else:
|
||||||
print(f"Failed to fetch active tenants. Status: {response.status}")
|
logger.error(f"Failed to fetch active tenants. Status: {response.status}")
|
||||||
return set()
|
return set()
|
||||||
|
except aiohttp.ClientError as e:
|
||||||
# TODO: remove after fetch_active_tenants is fixed
|
logger.error(f"Error fetching active tenants: {e}")
|
||||||
def get_initial_tenant_ids(self) -> set:
|
return set()
|
||||||
response = requests.get(self.tenant_service_url, timeout=10)
|
|
||||||
response.raise_for_status() # Raise an HTTPError for bad responses
|
|
||||||
|
|
||||||
if response.headers["content-type"].lower() == "application/json":
|
|
||||||
tenants = {tenant["tenantId"] for tenant in response.json()}
|
|
||||||
return tenants
|
|
||||||
return set()
|
|
||||||
|
|
||||||
async def initialize_tenant_queues(self):
|
async def initialize_tenant_queues(self):
|
||||||
active_tenants = self.get_initial_tenant_ids()
|
active_tenants = await self.fetch_active_tenants()
|
||||||
for tenant_id in active_tenants:
|
for tenant_id in active_tenants:
|
||||||
await self.create_tenant_queues(tenant_id)
|
await self.create_tenant_queues(tenant_id)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user