fix: alternate extractor between macro and extraction queues (1:2 ratio) to prevent starvation

This commit is contained in:
Celes Renata
2026-04-16 17:45:25 +00:00
parent 2440cddd37
commit f83577480f
+19 -4
View File
@@ -250,15 +250,30 @@ async def main() -> None:
# Pre-load company ID map (refreshed periodically)
company_id_map = await _build_company_id_map(pool)
refresh_counter = 0
# Alternate between queues to prevent starvation: process 1 macro then 2 extractions
macro_turn_counter = 0
try:
while True:
# Check macro classification queue first (priority)
raw = await redis_client.lpop(macro_queue)
is_macro_job = raw is not None
# Alternate: every 3rd job from macro queue, rest from extraction
# This prevents macro events from starving regular extractions
raw = None
is_macro_job = False
if raw is None:
if macro_turn_counter % 3 == 0:
# Try macro first
raw = await redis_client.lpop(macro_queue)
is_macro_job = raw is not None
if raw is None:
raw = await redis_client.lpop(queue)
else:
# Try extraction first
raw = await redis_client.lpop(queue)
if raw is None:
raw = await redis_client.lpop(macro_queue)
is_macro_job = raw is not None
macro_turn_counter += 1
if raw is None:
await asyncio.sleep(1)