Simplify create_daily_room.py

This commit is contained in:
James Hush
2025-10-20 17:02:05 +08:00
parent 013f869259
commit d37e63d972

View File

@@ -90,6 +90,11 @@ async def create_daily_room(
if name: if name:
body["name"] = name body["name"] = name
headers = {
"Authorization": f"Bearer {os.getenv('DAILY_API_KEY')}",
"Content-Type": "application/json",
}
try: try:
# Use tenacity's AsyncRetrying for automatic retry with exponential backoff # Use tenacity's AsyncRetrying for automatic retry with exponential backoff
async for attempt in AsyncRetrying( async for attempt in AsyncRetrying(
@@ -99,21 +104,17 @@ async def create_daily_room(
reraise=True, reraise=True,
): ):
with attempt: with attempt:
async with AsyncClient() as client: async with AsyncClient(timeout=30) as client:
response = await client.post( response = await client.post(
url="https://api.daily.co/v1/rooms", url="https://api.daily.co/v1/rooms",
headers={ headers=headers,
"Authorization": f"Bearer {os.getenv('DAILY_API_KEY')}",
"Content-Type": "application/json",
},
json=body, json=body,
timeout=30,
) )
response.raise_for_status() response.raise_for_status()
return response.json()
room_data = response.json() # This line should never be reached due to reraise=True, but satisfies type checker
return room_data return None
except RetryError as e: except RetryError as e:
# All retries exhausted # All retries exhausted
@@ -157,18 +158,6 @@ async def create_room_with_progress(
else: else:
progress_dict["failed"] += 1 progress_dict["failed"] += 1
# Log progress periodically (every 10% or every 100 rooms, whichever is smaller)
total_processed = progress_dict["completed"] + progress_dict["failed"]
log_interval = min(100, max(1, total // 10))
if total_processed % log_interval == 0 or total_processed == total:
logger.info(
f"Progress: {total_processed}/{total} "
f"({(total_processed / total) * 100:.1f}%) - "
f"{progress_dict['completed']} succeeded, "
f"{progress_dict['failed']} failed"
)
return result return result
@@ -195,10 +184,16 @@ async def test_create_rooms(
# Shared progress tracking dictionary # Shared progress tracking dictionary
progress_dict = {"completed": 0, "failed": 0} progress_dict = {"completed": 0, "failed": 0}
# Create tasks for concurrent room creation with progress tracking # Start background progress logger
tasks = [] stop_event = asyncio.Event()
for i in range(num_rooms): progress_task = asyncio.create_task(
task = create_room_with_progress( periodic_progress_logger(progress_dict, num_rooms, progress_interval, stop_event)
)
# Create and execute all tasks concurrently
logger.info(f"Executing {num_rooms} concurrent room creation requests...")
tasks = [
create_room_with_progress(
index=i, index=i,
total=num_rooms, total=num_rooms,
progress_dict=progress_dict, progress_dict=progress_dict,
@@ -207,16 +202,8 @@ async def test_create_rooms(
exp_minutes=10, exp_minutes=10,
max_retries=5, max_retries=5,
) )
tasks.append(task) for i in range(num_rooms)
]
# Execute all tasks concurrently with periodic progress logging
logger.info(f"Executing {num_rooms} concurrent room creation requests...")
# Start background progress logger
stop_event = asyncio.Event()
progress_task = asyncio.create_task(
periodic_progress_logger(progress_dict, num_rooms, progress_interval, stop_event)
)
try: try:
results = await asyncio.gather(*tasks, return_exceptions=True) results = await asyncio.gather(*tasks, return_exceptions=True)
@@ -226,17 +213,8 @@ async def test_create_rooms(
await progress_task await progress_task
# Count successes and failures # Count successes and failures
success_count = 0 success_count = sum(1 for r in results if r is not None and not isinstance(r, Exception))
failed_count = 0 failed_count = num_rooms - success_count
for i, result in enumerate(results):
if isinstance(result, Exception):
logger.error(f"Room {i + 1} failed with exception: {result}")
failed_count += 1
elif result is None:
failed_count += 1
else:
success_count += 1
elapsed_time = time.time() - start_time elapsed_time = time.time() - start_time