diff --git a/src/core/database.py b/src/core/database.py index 1b746b6..1a3f298 100644 --- a/src/core/database.py +++ b/src/core/database.py @@ -1010,9 +1010,12 @@ class Database: async with aiosqlite.connect(self.db_path) as db: await db.execute(""" UPDATE admin_config - SET admin_username = ?, admin_password = ?, api_key = ?, error_ban_threshold = ?, updated_at = CURRENT_TIMESTAMP + SET admin_username = ?, admin_password = ?, api_key = ?, error_ban_threshold = ?, + task_retry_enabled = ?, task_max_retries = ?, auto_disable_on_401 = ?, + updated_at = CURRENT_TIMESTAMP WHERE id = 1 - """, (config.admin_username, config.admin_password, config.api_key, config.error_ban_threshold)) + """, (config.admin_username, config.admin_password, config.api_key, config.error_ban_threshold, + config.task_retry_enabled, config.task_max_retries, config.auto_disable_on_401)) await db.commit() # Proxy config operations diff --git a/src/services/generation_handler.py b/src/services/generation_handler.py index 6218f5f..ae91bc9 100644 --- a/src/services/generation_handler.py +++ b/src/services/generation_handler.py @@ -521,6 +521,7 @@ class GenerationHandler: task_id = None is_first_chunk = True # Track if this is the first chunk log_id = None # Initialize log_id + log_updated = False # Track if log has been updated try: # Create initial log entry BEFORE submitting task to upstream @@ -680,6 +681,7 @@ class GenerationHandler: status_code=200, duration=duration ) + log_updated = True # Mark log as updated except Exception as e: # Release lock for image generation on error @@ -727,6 +729,7 @@ class GenerationHandler: status_code=status_code, duration=duration ) + log_updated = True # Mark log as updated else: # Generic error await self.db.update_request_log( @@ -735,12 +738,35 @@ class GenerationHandler: status_code=500, duration=duration ) + log_updated = True # Mark log as updated # Wrap exception with token_id information if token_obj: raise GenerationError(str(e), token_id=token_obj.id) else: raise e + finally: + # Ensure log is updated even if exception handling fails + # This prevents logs from being stuck at status_code = -1 + if log_id and not log_updated: + try: + # Log was not updated in try or except blocks, update it now + duration = time.time() - start_time + await self.db.update_request_log( + log_id, + response_body=json.dumps({"error": "Task failed or interrupted during processing"}), + status_code=500, + duration=duration + ) + debug_logger.log_info(f"Updated stuck log entry {log_id} from status -1 to 500 in finally block") + except Exception as finally_error: + # Don't let finally block errors break the flow + debug_logger.log_error( + error_message=f"Failed to update log in finally block: {str(finally_error)}", + status_code=500, + response_text=str(finally_error) + ) + async def handle_generation_with_retry(self, model: str, prompt: str, image: Optional[str] = None, video: Optional[str] = None,