From 5a8a402fdcdba5190657cab10f40bbc15bd3b669 Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Fri, 28 Mar 2025 15:04:35 +0800 Subject: [PATCH] Enhance LoraRoutes and templates for improved cache initialization handling - Updated LoraRoutes to better check cache initialization status and handle loading states. - Added logging for successful cache loading and error handling for cache retrieval failures. - Enhanced base.html and loras.html templates to display a loading spinner and initialization notice during cache setup. - Improved user experience by ensuring the loading notice is displayed appropriately based on initialization state. --- py/routes/lora_routes.py | 41 +++++++++++++++------- py/services/lora_scanner.py | 4 ++- templates/base.html | 68 ++++++++++++++++++++++++++++++------- templates/loras.html | 4 +++ 4 files changed, 91 insertions(+), 26 deletions(-) diff --git a/py/routes/lora_routes.py b/py/routes/lora_routes.py index d61f3500..40ff8e98 100644 --- a/py/routes/lora_routes.py +++ b/py/routes/lora_routes.py @@ -58,11 +58,13 @@ class LoraRoutes: async def handle_loras_page(self, request: web.Request) -> web.Response: """Handle GET /loras request""" try: - # 不等待缓存数据,直接检查缓存状态 + # 检查缓存初始化状态,增强判断条件 is_initializing = ( - self.scanner._cache is None and + self.scanner._cache is None or (self.scanner._initialization_task is not None and - not self.scanner._initialization_task.done()) + not self.scanner._initialization_task.done()) or + (self.scanner._cache is not None and len(self.scanner._cache.raw_data) == 0 and + self.scanner._initialization_task is not None) ) if is_initializing: @@ -74,16 +76,31 @@ class LoraRoutes: settings=settings, # Pass settings to template request=request # Pass the request object to the template ) + + logger.info("Loras page is initializing, returning loading page") else: - # 正常流程 - cache = await self.scanner.get_cached_data() - template = self.template_env.get_template('loras.html') - rendered = template.render( - folders=cache.folders, - is_initializing=False, - settings=settings, # Pass settings to template - request=request # Pass the request object to the template - ) + # 正常流程 - 但不要等待缓存刷新 + try: + cache = await self.scanner.get_cached_data(force_refresh=False) + template = self.template_env.get_template('loras.html') + rendered = template.render( + folders=cache.folders, + is_initializing=False, + settings=settings, # Pass settings to template + request=request # Pass the request object to the template + ) + logger.info(f"Loras page loaded successfully with {len(cache.raw_data)} items") + except Exception as cache_error: + logger.error(f"Error loading cache data: {cache_error}") + # 如果获取缓存失败,也显示初始化页面 + template = self.template_env.get_template('loras.html') + rendered = template.render( + folders=[], + is_initializing=True, + settings=settings, + request=request + ) + logger.info("Cache error, returning initialization page") return web.Response( text=rendered, diff --git a/py/services/lora_scanner.py b/py/services/lora_scanner.py index bf58029c..18cd45df 100644 --- a/py/services/lora_scanner.py +++ b/py/services/lora_scanner.py @@ -3,6 +3,7 @@ import os import logging import asyncio import shutil +import time from typing import List, Dict, Optional from dataclasses import dataclass from operator import itemgetter @@ -91,6 +92,7 @@ class LoraScanner: async def _initialize_cache(self) -> None: """Initialize or refresh the cache""" try: + start_time = time.time() # Clear existing hash index self._hash_index.clear() @@ -122,7 +124,7 @@ class LoraScanner: await self._cache.resort() self._initialization_task = None - logger.info("LoRA Manager: Cache initialization completed") + logger.info(f"LoRA Manager: Cache initialization completed in {time.time() - start_time:.2f} seconds, found {len(raw_data)} loras") except Exception as e: logger.error(f"LoRA Manager: Error initializing cache: {e}") self._cache = LoraCache( diff --git a/templates/base.html b/templates/base.html index 4815232a..e9c283aa 100644 --- a/templates/base.html +++ b/templates/base.html @@ -37,6 +37,49 @@ + + {% if is_initializing %} + + {% endif %} + {% endif %} {% block additional_scripts %}{% endblock %} - \ No newline at end of file + diff --git a/templates/loras.html b/templates/loras.html index 6e1ee336..83a8411c 100644 --- a/templates/loras.html +++ b/templates/loras.html @@ -4,7 +4,9 @@ {% block page_id %}loras{% endblock %} {% block preload %} +{% if not is_initializing %} +{% endif %} {% endblock %} {% block init_title %}Initializing LoRA Manager{% endblock %} @@ -29,5 +31,7 @@ {% endblock %} {% block main_script %} +{% if not is_initializing %} +{% endif %} {% endblock %} \ No newline at end of file