Queue worker, processes tasks in the request queue
()
| 19 | |
| 20 | |
| 21 | async def queue_worker() -> None: |
| 22 | """Queue worker, processes tasks in the request queue""" |
| 23 | # Delayed imports to avoid circularity |
| 24 | from api_utils.server_state import state |
| 25 | from config import RESPONSE_COMPLETION_TIMEOUT |
| 26 | |
| 27 | logger = state.logger |
| 28 | request_queue = state.request_queue |
| 29 | processing_lock = state.processing_lock |
| 30 | model_switching_lock = state.model_switching_lock |
| 31 | params_cache_lock = state.params_cache_lock |
| 32 | from browser_utils.auth_rotation import perform_auth_rotation |
| 33 | from browser_utils.page_controller import PageController |
| 34 | from config.global_state import GlobalState |
| 35 | |
| 36 | from .error_utils import ( |
| 37 | client_cancelled, |
| 38 | client_disconnected, |
| 39 | server_error, |
| 40 | ) |
| 41 | |
| 42 | # Internal imports for queue worker logic |
| 43 | from .request_processor import ( |
| 44 | ClientDisconnectedError, |
| 45 | _process_request_refactored, |
| 46 | _test_client_connection, |
| 47 | save_error_snapshot, |
| 48 | ) |
| 49 | from .utils_ext.stream import clear_stream_queue |
| 50 | |
| 51 | logger.info("--- Queue Worker Started ---") |
| 52 | |
| 53 | # Validate that required globals are initialized |
| 54 | if request_queue is None: |
| 55 | logger.critical("FATAL: request_queue is None! Initialization failed.") |
| 56 | raise RuntimeError("request_queue not initialized") |
| 57 | |
| 58 | if processing_lock is None: |
| 59 | logger.critical("FATAL: processing_lock is None! Initialization failed.") |
| 60 | raise RuntimeError("processing_lock not initialized") |
| 61 | |
| 62 | if model_switching_lock is None: |
| 63 | logger.critical("FATAL: model_switching_lock is None! Initialization failed.") |
| 64 | raise RuntimeError("model_switching_lock not initialized") |
| 65 | |
| 66 | if params_cache_lock is None: |
| 67 | logger.critical("FATAL: params_cache_lock is None! Initialization failed.") |
| 68 | raise RuntimeError("params_cache_lock not initialized") |
| 69 | |
| 70 | logger.debug( |
| 71 | f"Queue worker initialized with queue={request_queue}, lock={processing_lock}" |
| 72 | ) |
| 73 | |
| 74 | was_last_request_streaming = False |
| 75 | last_request_completion_time = 0.0 |
| 76 | shutdown_check_interval = 0.1 |
| 77 | |
| 78 | while True: |
no test coverage detected