Spaces:
Running
Running
Commit ·
2bf23f6
1
Parent(s): 4d2e3e5
feat: 增强用户数据库同步可靠性
Browse files- backend/app/database_user.py +51 -25
backend/app/database_user.py
CHANGED
|
@@ -11,6 +11,7 @@ import secrets
|
|
| 11 |
import hashlib
|
| 12 |
import hmac
|
| 13 |
import logging
|
|
|
|
| 14 |
from dotenv import load_dotenv
|
| 15 |
|
| 16 |
# 加载 .env 环境变量
|
|
@@ -170,20 +171,30 @@ def sync_user_db_from_hf() -> None:
|
|
| 170 |
logger.info("HF_TOKEN or DATASET_REPO_ID not set, skip user db download")
|
| 171 |
return
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
|
| 189 |
def sync_qr_codes_from_hf() -> None:
|
|
@@ -221,17 +232,32 @@ def upload_user_db_to_hf() -> None:
|
|
| 221 |
logger.warning("User db file not found, skip upload")
|
| 222 |
return
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
|
| 237 |
def sync_user_db_after_update(func: Callable[..., Any]) -> Callable[..., Any]:
|
|
|
|
| 11 |
import hashlib
|
| 12 |
import hmac
|
| 13 |
import logging
|
| 14 |
+
import time
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
|
| 17 |
# 加载 .env 环境变量
|
|
|
|
| 171 |
logger.info("HF_TOKEN or DATASET_REPO_ID not set, skip user db download")
|
| 172 |
return
|
| 173 |
|
| 174 |
+
max_retries = 5
|
| 175 |
+
retry_delay = 2 # seconds
|
| 176 |
+
for attempt in range(max_retries):
|
| 177 |
+
try:
|
| 178 |
+
downloaded = hf_hub_download(
|
| 179 |
+
repo_id=DATASET_REPO_ID,
|
| 180 |
+
filename=USER_DB_FILENAME,
|
| 181 |
+
repo_type="dataset",
|
| 182 |
+
token=HF_TOKEN,
|
| 183 |
+
)
|
| 184 |
+
src = Path(downloaded)
|
| 185 |
+
dst = Path(USER_DB_PATH)
|
| 186 |
+
if src.exists() and src.resolve() != dst.resolve():
|
| 187 |
+
dst.write_bytes(src.read_bytes())
|
| 188 |
+
logger.info(f"User db synced from HF: {USER_DB_FILENAME}")
|
| 189 |
+
return # Success, exit the loop
|
| 190 |
+
except Exception as e:
|
| 191 |
+
logger.warning(f"Attempt {attempt + 1} failed to sync user db from HF: {e}")
|
| 192 |
+
if attempt < max_retries - 1:
|
| 193 |
+
logger.info(f"Retrying in {retry_delay} seconds...")
|
| 194 |
+
time.sleep(retry_delay)
|
| 195 |
+
else:
|
| 196 |
+
logger.error(f"All {max_retries} attempts failed to sync user db from HF. Application will not start properly.")
|
| 197 |
+
raise # Re-raise the exception to prevent application startup
|
| 198 |
|
| 199 |
|
| 200 |
def sync_qr_codes_from_hf() -> None:
|
|
|
|
| 232 |
logger.warning("User db file not found, skip upload")
|
| 233 |
return
|
| 234 |
|
| 235 |
+
# Check if the database file is too small (likely empty), prevent overwriting
|
| 236 |
+
file_size = db_path.stat().st_size
|
| 237 |
+
if file_size < 1024: # Less than 1KB, likely empty
|
| 238 |
+
logger.warning(f"User db file is too small ({file_size} bytes), skip upload to prevent overwriting remote data")
|
| 239 |
+
return
|
| 240 |
+
|
| 241 |
+
max_retries = 3
|
| 242 |
+
retry_delay = 2 # seconds
|
| 243 |
+
for attempt in range(max_retries):
|
| 244 |
+
try:
|
| 245 |
+
upload_file(
|
| 246 |
+
path_or_fileobj=str(db_path),
|
| 247 |
+
path_in_repo=USER_DB_FILENAME,
|
| 248 |
+
repo_id=DATASET_REPO_ID,
|
| 249 |
+
repo_type="dataset",
|
| 250 |
+
token=HF_TOKEN,
|
| 251 |
+
)
|
| 252 |
+
logger.info(f"User db uploaded to HF: {USER_DB_FILENAME}")
|
| 253 |
+
return # Success, exit the loop
|
| 254 |
+
except Exception as e:
|
| 255 |
+
logger.error(f"Attempt {attempt + 1} failed to upload user db to HF: {e}")
|
| 256 |
+
if attempt < max_retries - 1:
|
| 257 |
+
logger.info(f"Retrying in {retry_delay} seconds...")
|
| 258 |
+
time.sleep(retry_delay)
|
| 259 |
+
else:
|
| 260 |
+
logger.error(f"All {max_retries} attempts failed to upload user db to HF")
|
| 261 |
|
| 262 |
|
| 263 |
def sync_user_db_after_update(func: Callable[..., Any]) -> Callable[..., Any]:
|