hzruo commited on
Commit
ff8a422
·
verified ·
1 Parent(s): b18468d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -27
app.py CHANGED
@@ -38,8 +38,15 @@ class Settings(BaseSettings):
38
  TOKEN_CACHE_TIME: int = int(os.getenv("TOKEN_CACHE_TIME", 604800)) # 默认缓存7天 (7*24*60*60=604800秒)
39
  FINGERPRINT_PREFIX: str = os.getenv("FINGERPRINT_PREFIX", "anon_")
40
 
 
 
 
 
 
 
41
  class Config:
42
  env_file = ".env"
 
43
 
44
  settings = Settings()
45
  # =====================================
@@ -551,36 +558,42 @@ class SambaAuthAsync:
551
  finally:
552
  await self.client.aclose()
553
 
554
- @app.on_event("startup")
555
- async def startup_event():
556
- """应用启动时预获取令牌"""
557
- print("\n" + "="*50)
558
- print("[启动] SambaNova OpenAI 代理服务启动")
559
- print("="*50)
560
-
561
- # 检查环境变量
562
- print(f"[环境] SAMBA_EMAIL: {'已设置' if settings.SAMBA_EMAIL else '未设置'}")
563
- print(f"[环境] SAMBA_PASSWORD: {'已设置' if settings.SAMBA_PASSWORD else '未设置'}")
564
- print(f"[环境] LOCAL_API_KEY: {'已设置' if settings.LOCAL_API_KEY else '未设置'}")
565
 
566
- # 尝试直接登录
567
- print("[登录] 开始尝试登录...")
568
- try:
569
- auth = SambaAuthAsync(settings.SAMBA_EMAIL, settings.SAMBA_PASSWORD)
570
- token = await auth.login()
 
 
 
 
 
 
571
 
572
- if token:
573
- global access_token, token_expiry
574
- access_token = token
575
- token_expiry = time.time() + settings.TOKEN_CACHE_TIME
576
- print(f"[登录] 登录成功! 令牌: {token}")
577
- print(f"[登录] 令牌将在 {settings.TOKEN_CACHE_TIME} 秒后过期")
578
  else:
579
- print("[登录] 登录失败,未获取到令牌")
580
- except Exception as e:
581
- print(f"[登录] 登录过程发生异常: {str(e)}")
582
-
583
- print("="*50 + "\n")
 
 
 
 
 
 
 
 
 
 
584
 
585
  if __name__ == "__main__":
586
  import uvicorn
 
38
  TOKEN_CACHE_TIME: int = int(os.getenv("TOKEN_CACHE_TIME", 604800)) # 默认缓存7天 (7*24*60*60=604800秒)
39
  FINGERPRINT_PREFIX: str = os.getenv("FINGERPRINT_PREFIX", "anon_")
40
 
41
+ # 添加缺少的字段
42
+ domain: str = os.getenv("domain", "")
43
+ temp_mail: str = os.getenv("temp_mail", "")
44
+ temp_mail_ext: str = os.getenv("temp_mail_ext", "")
45
+ rate_limit_wait: int = int(os.getenv("rate_limit_wait", 3600))
46
+
47
  class Config:
48
  env_file = ".env"
49
+ extra = "ignore" # 忽略额外的字段,避免类似错误
50
 
51
  settings = Settings()
52
  # =====================================
 
558
  finally:
559
  await self.client.aclose()
560
 
561
+ # 定义后台任务刷新令牌
562
+ async def token_refresh_task():
563
+ global token_expiry, access_token
 
 
 
 
 
 
 
 
564
 
565
+ while True:
566
+ # 如果没有令牌或过期时间未设置,先获取一次
567
+ if not access_token or token_expiry == 0:
568
+ await get_samba_token()
569
+ if not access_token:
570
+ # 如果获取失败,等待一段时间后重试
571
+ await asyncio.sleep(60)
572
+ continue
573
+
574
+ # 计算距离过期还有多少时间(秒)
575
+ remaining_time = token_expiry - time.time()
576
 
577
+ if remaining_time <= 0:
578
+ # 如果已过期,立即刷新
579
+ print("[后台任务] 令牌已过期,立即刷新")
580
+ await get_samba_token()
 
 
581
  else:
582
+ # 设置为过期前3分钟刷新
583
+ refresh_before = min(remaining_time - 180, 3600) # 提前3分钟,但最长等待1小时
584
+ refresh_before = max(refresh_before, 0) # 确保不会是负数
585
+
586
+ print(f"[后台任务] 令牌将在 {int(remaining_time)} 秒后过期,计划在 {int(refresh_before)} 秒后刷新")
587
+ await asyncio.sleep(refresh_before)
588
+
589
+ # 刷新令牌
590
+ print("[后台任务] 开始自动刷新令牌")
591
+ await get_samba_token()
592
+
593
+ # 在应用启动时启动后台任务
594
+ @app.on_event("startup")
595
+ async def startup_event():
596
+ asyncio.create_task(token_refresh_task())
597
 
598
  if __name__ == "__main__":
599
  import uvicorn