xiaoh2018 commited on
Commit
ef100b2
·
verified ·
1 Parent(s): 826af92

Upload database.py

Browse files
Files changed (1) hide show
  1. src/core/database.py +60 -47
src/core/database.py CHANGED
@@ -935,23 +935,32 @@ class Database:
935
 
936
  async def get_generation_config(self) -> Optional[GenerationConfig]:
937
  """Get generation configuration"""
938
- async with aiosqlite.connect(self.db_path) as db:
939
- db.row_factory = aiosqlite.Row
940
- cursor = await db.execute("SELECT * FROM generation_config WHERE id = 1")
941
- row = await cursor.fetchone()
942
- if row:
943
- return GenerationConfig(**dict(row))
944
- return None
 
 
 
 
 
945
 
946
  async def update_generation_config(self, image_timeout: int, video_timeout: int):
947
  """Update generation configuration"""
948
- async with aiosqlite.connect(self.db_path) as db:
949
- await db.execute("""
950
- UPDATE generation_config
951
- SET image_timeout = ?, video_timeout = ?, updated_at = CURRENT_TIMESTAMP
952
- WHERE id = 1
953
- """, (image_timeout, video_timeout))
954
- await db.commit()
 
 
 
 
955
 
956
  # Request log operations
957
  async def add_request_log(self, log: RequestLog):
@@ -1046,39 +1055,43 @@ class Database:
1046
  - Generation config (image_timeout, video_timeout)
1047
  - Proxy config will be handled by ProxyManager
1048
  """
1049
- from .config import config
1050
-
1051
- # Reload admin config
1052
- admin_config = await self.get_admin_config()
1053
- if admin_config:
1054
- config.set_admin_username_from_db(admin_config.username)
1055
- config.set_admin_password_from_db(admin_config.password)
1056
- config.api_key = admin_config.api_key
1057
-
1058
- # Reload cache config
1059
- cache_config = await self.get_cache_config()
1060
- if cache_config:
1061
- config.set_cache_enabled(cache_config.cache_enabled)
1062
- config.set_cache_timeout(cache_config.cache_timeout)
1063
- config.set_cache_base_url(cache_config.cache_base_url or "")
1064
-
1065
- # Reload generation config
1066
- generation_config = await self.get_generation_config()
1067
- if generation_config:
1068
- config.set_image_timeout(generation_config.image_timeout)
1069
- config.set_video_timeout(generation_config.video_timeout)
1070
-
1071
- # Reload debug config
1072
- debug_config = await self.get_debug_config()
1073
- if debug_config:
1074
- config.set_debug_enabled(debug_config.enabled)
1075
-
1076
- # Reload captcha config
1077
- captcha_config = await self.get_captcha_config()
1078
- if captcha_config:
1079
- config.set_captcha_method(captcha_config.captcha_method)
1080
- config.set_yescaptcha_api_key(captcha_config.yescaptcha_api_key)
1081
- config.set_yescaptcha_base_url(captcha_config.yescaptcha_base_url)
 
 
 
 
1082
 
1083
  # Cache config operations
1084
  async def get_cache_config(self) -> CacheConfig:
 
935
 
936
  async def get_generation_config(self) -> Optional[GenerationConfig]:
937
  """Get generation configuration"""
938
+ try:
939
+ async with aiosqlite.connect(self.db_path) as db:
940
+ db.row_factory = aiosqlite.Row
941
+ cursor = await db.execute("SELECT * FROM generation_config WHERE id = 1")
942
+ row = await cursor.fetchone()
943
+ if row:
944
+ return GenerationConfig(**dict(row))
945
+ return None
946
+ except Exception as e:
947
+ print(f"Error getting generation config: {e}")
948
+ # 返回默认配置,避免异常传播
949
+ return GenerationConfig(image_timeout=300, video_timeout=1500)
950
 
951
  async def update_generation_config(self, image_timeout: int, video_timeout: int):
952
  """Update generation configuration"""
953
+ try:
954
+ async with aiosqlite.connect(self.db_path) as db:
955
+ await db.execute("""
956
+ UPDATE generation_config
957
+ SET image_timeout = ?, video_timeout = ?, updated_at = CURRENT_TIMESTAMP
958
+ WHERE id = 1
959
+ """, (image_timeout, video_timeout))
960
+ await db.commit()
961
+ except Exception as e:
962
+ print(f"Error updating generation config: {e}")
963
+ raise
964
 
965
  # Request log operations
966
  async def add_request_log(self, log: RequestLog):
 
1055
  - Generation config (image_timeout, video_timeout)
1056
  - Proxy config will be handled by ProxyManager
1057
  """
1058
+ try:
1059
+ from .config import config
1060
+
1061
+ # Reload admin config
1062
+ admin_config = await self.get_admin_config()
1063
+ if admin_config:
1064
+ config.set_admin_username_from_db(admin_config.username)
1065
+ config.set_admin_password_from_db(admin_config.password)
1066
+ config.api_key = admin_config.api_key
1067
+
1068
+ # Reload cache config
1069
+ cache_config = await self.get_cache_config()
1070
+ if cache_config:
1071
+ config.set_cache_enabled(cache_config.cache_enabled)
1072
+ config.set_cache_timeout(cache_config.cache_timeout)
1073
+ config.set_cache_base_url(cache_config.cache_base_url or "")
1074
+
1075
+ # Reload generation config
1076
+ generation_config = await self.get_generation_config()
1077
+ if generation_config:
1078
+ config.set_image_timeout(generation_config.image_timeout)
1079
+ config.set_video_timeout(generation_config.video_timeout)
1080
+
1081
+ # Reload debug config
1082
+ debug_config = await self.get_debug_config()
1083
+ if debug_config:
1084
+ config.set_debug_enabled(debug_config.enabled)
1085
+
1086
+ # Reload captcha config
1087
+ captcha_config = await self.get_captcha_config()
1088
+ if captcha_config:
1089
+ config.set_captcha_method(captcha_config.captcha_method)
1090
+ config.set_yescaptcha_api_key(captcha_config.yescaptcha_api_key)
1091
+ config.set_yescaptcha_base_url(captcha_config.yescaptcha_base_url)
1092
+ except Exception as e:
1093
+ print(f"Error reloading config to memory: {e}")
1094
+ # 不要让异常传播,避免返回 500 错误
1095
 
1096
  # Cache config operations
1097
  async def get_cache_config(self) -> CacheConfig: