|
|
|
|
@ -14,8 +14,10 @@ app = FastAPI( |
|
|
|
|
|
|
|
|
|
# 全局变量 |
|
|
|
|
_simulator = None |
|
|
|
|
_last_result = None # 存储上次执行结果 |
|
|
|
|
_is_processing = False # 是否正在处理 |
|
|
|
|
_last_result = None |
|
|
|
|
_is_processing = False |
|
|
|
|
_token_expiry_time = 0 # token过期时间戳 |
|
|
|
|
_TOKEN_REFRESH_THRESHOLD = 1800 # 30分钟刷新阈值 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initialize_simulator(): |
|
|
|
|
@ -28,6 +30,27 @@ def initialize_simulator(): |
|
|
|
|
return _simulator |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_and_refresh_token(): |
|
|
|
|
"""检查并刷新token""" |
|
|
|
|
global _token_expiry_time, _simulator |
|
|
|
|
|
|
|
|
|
current_time = time.time() |
|
|
|
|
|
|
|
|
|
# 如果token即将过期(剩余时间少于30分钟),则刷新 |
|
|
|
|
if current_time >= _token_expiry_time - _TOKEN_REFRESH_THRESHOLD: |
|
|
|
|
print("token即将过期,重新登录...") |
|
|
|
|
_simulator.login() |
|
|
|
|
|
|
|
|
|
# 更新token过期时间(假设token有效期14400秒) |
|
|
|
|
_token_expiry_time = current_time + 14400 |
|
|
|
|
print(f"token已刷新,有效期至: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(_token_expiry_time))}") |
|
|
|
|
return True |
|
|
|
|
else: |
|
|
|
|
remaining = int(_token_expiry_time - current_time) |
|
|
|
|
print(f"token仍然有效,剩余时间: {remaining // 3600}小时{(remaining % 3600) // 60}分钟") |
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/") |
|
|
|
|
async def root(): |
|
|
|
|
"""根路径,返回服务信息""" |
|
|
|
|
@ -49,11 +72,11 @@ async def run_simulation(): |
|
|
|
|
""" |
|
|
|
|
触发一次模拟执行 |
|
|
|
|
执行顺序: |
|
|
|
|
1. 刷新token |
|
|
|
|
1. 检查token并刷新 |
|
|
|
|
2. 加载alpha列表 |
|
|
|
|
3. 执行批量模拟 |
|
|
|
|
""" |
|
|
|
|
global _is_processing, _last_result |
|
|
|
|
global _is_processing, _last_result, _simulator, _token_expiry_time |
|
|
|
|
|
|
|
|
|
# 检查是否正在处理 |
|
|
|
|
if _is_processing: |
|
|
|
|
@ -69,9 +92,16 @@ async def run_simulation(): |
|
|
|
|
# 初始化模拟器 |
|
|
|
|
simulator = initialize_simulator() |
|
|
|
|
|
|
|
|
|
# 1. 刷新token |
|
|
|
|
print("刷新token...") |
|
|
|
|
simulator.login() |
|
|
|
|
# 1. 检查并刷新token(只在需要时) |
|
|
|
|
print("检查token状态...") |
|
|
|
|
if _token_expiry_time == 0: |
|
|
|
|
# 第一次运行,需要登录 |
|
|
|
|
print("首次运行,进行登录...") |
|
|
|
|
simulator.login() |
|
|
|
|
_token_expiry_time = time.time() + 14400 |
|
|
|
|
print("登录成功") |
|
|
|
|
else: |
|
|
|
|
check_and_refresh_token() |
|
|
|
|
|
|
|
|
|
# 2. 加载alpha列表 |
|
|
|
|
print("加载alpha列表...") |
|
|
|
|
@ -139,10 +169,20 @@ async def get_last_result(): |
|
|
|
|
@app.get("/status") |
|
|
|
|
async def get_status(): |
|
|
|
|
"""获取服务状态""" |
|
|
|
|
global _token_expiry_time |
|
|
|
|
|
|
|
|
|
token_valid = False |
|
|
|
|
if _token_expiry_time > 0: |
|
|
|
|
remaining_time = _token_expiry_time - time.time() |
|
|
|
|
token_valid = remaining_time > 0 |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
"status": "running", |
|
|
|
|
"is_processing": _is_processing, |
|
|
|
|
"simulator_initialized": _simulator is not None, |
|
|
|
|
"has_last_result": _last_result is not None, |
|
|
|
|
"token_valid": token_valid, |
|
|
|
|
"token_expires_in": int(_token_expiry_time - time.time()) if _token_expiry_time > 0 else 0, |
|
|
|
|
"timestamp": time.time() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -153,7 +193,8 @@ async def health_check(): |
|
|
|
|
return { |
|
|
|
|
"status": "healthy", |
|
|
|
|
"timestamp": time.time(), |
|
|
|
|
"simulator_initialized": _simulator is not None |
|
|
|
|
"simulator_initialized": _simulator is not None, |
|
|
|
|
"token_valid": _token_expiry_time > time.time() if _token_expiry_time > 0 else False |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -168,6 +209,6 @@ if __name__ == "__main__": |
|
|
|
|
uvicorn.run( |
|
|
|
|
app, |
|
|
|
|
host="0.0.0.0", |
|
|
|
|
port=8000, |
|
|
|
|
port=8005, |
|
|
|
|
log_level="info" |
|
|
|
|
) |