代码优化

This commit is contained in:
liujianjiang 2026-01-09 15:46:23 +08:00
parent 3330f60ffc
commit 5a5ba0da43
2 changed files with 39 additions and 28 deletions

View File

@ -70,12 +70,9 @@ class DealAccount:
async def deal_backup_task(self, data: Dict[str, Any]): async def deal_backup_task(self, data: Dict[str, Any]):
# 先查询该账户是否备份过,备份过就修改数据库状态, # 先查询该账户是否备份过,备份过就修改数据库状态,
await self.db_pool.initialize() await self.db_pool.initialize()
set_param = {"status": 1} sql_str = f"""select account_id from shoppe_device_record where account_id='{data['account_id']}'"""
params = (data["account_id"],) result = await self.db_pool.fetch_all(sql_str, )
where_conditions = "account_id = %s " if result:
result = await self.update_device_status(set_param, where_conditions, params)
print(result)
if not result:
print(f"该账号:{data['account_id']} 已经备份过,不需要在备份") print(f"该账号:{data['account_id']} 已经备份过,不需要在备份")
return True return True
else: else:
@ -126,7 +123,8 @@ class DealAccount:
return True return True
else: else:
print(f"云机:{data['pad_code']} 300环境还原失败需要认为干预") print(f"云机:{data['pad_code']} 300环境还原失败需要认为干预")
else:
print("未查询到可用还原数据")
return False return False

53
main.py
View File

@ -7,7 +7,8 @@ import asyncio
import uvicorn import uvicorn
from pathlib import Path from pathlib import Path
from typing import Dict, Any, Optional, List from typing import Dict, Any, Optional, List
from fastapi import FastAPI, HTTPException, Depends, Header from fastapi import FastAPI, HTTPException, Depends, Header, BackgroundTasks
from public_function.auth import verify_tk_token from public_function.auth import verify_tk_token
from task_management.all_task_management import AllTask from task_management.all_task_management import AllTask
from account_management.deal_account import DealAccount from account_management.deal_account import DealAccount
@ -213,32 +214,44 @@ async def reset_task(task_data: ResetTask, task_manager: Any = Depends(get_task_
raise HTTPException(status_code=404, detail=f"任务:{params.get("task_id")} 重启失败") raise HTTPException(status_code=404, detail=f"任务:{params.get("task_id")} 重启失败")
async def deal_shopee_task(param: Dict[str, Any], account_manager):
print(param)
result = await account_manager.deal_backup_task(param)
print(f"pad_code{param.get('pad_code')} 备份结果为:{result}")
if param.get("is_restore", False):
time.sleep(random.randint(1, 10))
print(param)
restore_result = await account_manager.deal_restore_system(param)
print(f"pad_code{param.get('pad_code')} 还原结果为:{restore_result}")
# if restore_result and result:
# return {"code": 200, "message": f"任务:{param.get("pad_code")} 备份还原成功"}
# raise HTTPException(status_code=404, detail=f"云机:{param.get("pad_code")} 备份还原失败")
# else:
# if result:
# return {"code": 200, "message": f"任务:{param.get("pad_code")} 备份成功", }
# else:
# raise HTTPException(status_code=404, detail=f"云机:{param.get("pad_code")} 备份失败")
# # try:
# # pass
# # except Exception as e:
# # raise HTTPException(status_code=404, detail=f"云机:{param.get("pad_code")} 备份失败,失败原因:{e}")
@app.post("/shop_backup", summary="云机备份与还原") @app.post("/shop_backup", summary="云机备份与还原")
async def shop_backup(backup_data: BackupItem, account_manager: Any = Depends(get_account_manager)): async def shop_backup(background_tasks: BackgroundTasks, backup_data: BackupItem, account_manager: Any = Depends(get_account_manager)):
""" """
获取指定应用的可用账号 获取指定应用的可用账号
- **app_name**: 应用名称 - **app_name**: 应用名称
pad_code pad_code
""" """
param = backup_data.model_dump() param = backup_data.model_dump()
print(param) task_id = f"task_{int(asyncio.get_event_loop().time())}"
result = await account_manager.deal_backup_task(param) param["task_id"] = task_id # 后续需要对任务状态进行记录使用
if param.get("is_restore", False): background_tasks.add_task(deal_shopee_task, param, account_manager)
time.sleep(random.randint(1, 10)) return {
print(param) "message": "请求已接收,任务在后台执行",
restore_result = await account_manager.deal_restore_system(param) "task_id": task_id
if restore_result and result: }
return {"code": 200, "message": f"任务:{param.get("pad_code")} 备份还原成功"}
raise HTTPException(status_code=404, detail=f"云机:{param.get("pad_code")} 备份还原失败")
else:
if result:
return {"code": 200, "message": f"任务:{param.get("pad_code")} 备份成功", }
else:
raise HTTPException(status_code=404, detail=f"云机:{param.get("pad_code")} 备份失败")
# try:
# pass
# except Exception as e:
# raise HTTPException(status_code=404, detail=f"云机:{param.get("pad_code")} 备份失败,失败原因:{e}")
if __name__ == '__main__': if __name__ == '__main__':