代码优化
This commit is contained in:
parent
c7d1c77480
commit
a8405be6e9
|
|
@ -1,5 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import os
|
import os
|
||||||
|
from datetime import datetime
|
||||||
from typing import Optional, Dict, Any, List
|
from typing import Optional, Dict, Any, List
|
||||||
from public_function.asyn_mysql import AsyncMySQL
|
from public_function.asyn_mysql import AsyncMySQL
|
||||||
from public_function.deal_all_task import DealAllTask
|
from public_function.deal_all_task import DealAllTask
|
||||||
|
|
@ -73,18 +74,23 @@ class DealAccount:
|
||||||
where_conditions = "account_id = %s "
|
where_conditions = "account_id = %s "
|
||||||
result = await self.update_device_status(set_param, where_conditions, params)
|
result = await self.update_device_status(set_param, where_conditions, params)
|
||||||
if result:
|
if result:
|
||||||
print(f"该账号:{params['account_id']} 已经备份过,不需要在备份")
|
print(f"该账号:{data['account_id']} 已经备份过,不需要在备份")
|
||||||
else:
|
else:
|
||||||
tasks = self.task_all.backup_system([pad_code], pad_code.lower())
|
tasks = self.task_all.backup_system([data["pad_code"]], data["pad_code"].lower())
|
||||||
|
if tasks:
|
||||||
backup_name = tasks[0]["backupId"]
|
backup_name = tasks[0]["backupId"]
|
||||||
payload = {"account_id": params["account_id"], "backup_name": backup_name, "script_name": params["script_name"]}
|
payload = {"account_id": data["account_id"], "backup_name": backup_name, "script_name": data["script_name"]}
|
||||||
# 将数据插入mysql 的表 shoppe_device_record 中
|
# 将数据插入mysql 的表 shoppe_device_record 中
|
||||||
await self.db_pool.insert_many(table="shoppe_device_record", data=[payload])
|
await self.db_pool.insert_many(table="shoppe_device_record", data=[payload])
|
||||||
result = self.task_all.check_phone_status(pad_code=params["pad_code"], file_name="test_abc.js")
|
result = self.task_all.check_phone_status(pad_code=data["pad_code"], file_name="test_abc.js")
|
||||||
if result:
|
if result:
|
||||||
print(f"云机:{params['pad_code']}备份完成")
|
return True
|
||||||
|
print(f"云机:{data['pad_code']}备份完成")
|
||||||
else:
|
else:
|
||||||
print(f"云机:{params['pad_code']} 300秒备份失败,需要认为干预")
|
print(f"云机:{data['pad_code']} 300秒备份失败,需要认为干预")
|
||||||
|
else:
|
||||||
|
print(f"实例:{data['pad_code']} 不存在")
|
||||||
|
return False
|
||||||
|
|
||||||
async def deal_restore_system(self, data: Dict[str, Any]):
|
async def deal_restore_system(self, data: Dict[str, Any]):
|
||||||
await self.db_pool.initialize()
|
await self.db_pool.initialize()
|
||||||
|
|
@ -102,14 +108,17 @@ class DealAccount:
|
||||||
self.task_all.restore_system(pad_code=data["pad_code"], backup_name=res_dict["backup_name"])
|
self.task_all.restore_system(pad_code=data["pad_code"], backup_name=res_dict["backup_name"])
|
||||||
result_status = self.task_all.check_phone_status(pad_code=data["pad_code"], file_name="test_abc.js")
|
result_status = self.task_all.check_phone_status(pad_code=data["pad_code"], file_name="test_abc.js")
|
||||||
if result_status:
|
if result_status:
|
||||||
print(f"云机:{params['pad_code']} 环境还原成功")
|
print(f"云机:{data['pad_code']} 环境还原成功")
|
||||||
# 下载执行脚本
|
# 下载执行脚本
|
||||||
self.task_all.upload_file_to_phone([data["pad_code"]], file_name=res_dict["script_name"])
|
self.task_all.upload_file_to_phone([data["pad_code"]], file_name=res_dict["script_name"])
|
||||||
# 启动对应脚本
|
# 启动对应脚本
|
||||||
self.task_all.async_execute_adb_command(pad_code=data["pad_code"], file_name=res_dict["script_name"])
|
self.task_all.async_execute_adb_command(pad_code=data["pad_code"], file_name=res_dict["script_name"])
|
||||||
# 需要新增函数
|
# 需要新增函数
|
||||||
|
return True
|
||||||
else:
|
else:
|
||||||
print(f"云机:{params['pad_code']} 300环境还原失败,需要认为干预")
|
print(f"云机:{data['pad_code']} 300环境还原失败,需要认为干预")
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
22
main.py
22
main.py
|
|
@ -212,22 +212,28 @@ async def reset_task(task_data: ResetTask, task_manager: Any = Depends(get_task_
|
||||||
|
|
||||||
|
|
||||||
@app.post("/shop_backup", summary="云机备份与还原")
|
@app.post("/shop_backup", summary="云机备份与还原")
|
||||||
async def shop_backup(backup_data: AccountObtain, account_manager: Any = Depends(get_account_manager)):
|
async def shop_backup(backup_data: BackupItem, account_manager: Any = Depends(get_account_manager)):
|
||||||
"""
|
"""
|
||||||
获取指定应用的可用账号
|
获取指定应用的可用账号
|
||||||
- **app_name**: 应用名称
|
- **app_name**: 应用名称
|
||||||
pad_code
|
pad_code
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
param = backup_data.model_dump()
|
param = backup_data.model_dump()
|
||||||
await account_manager.deal_backup_task(param)
|
try:
|
||||||
if params.get("is_restore", False):
|
result = await account_manager.deal_backup_task(param)
|
||||||
|
if param.get("is_restore", False):
|
||||||
time.sleep(random.randint(1, 10))
|
time.sleep(random.randint(1, 10))
|
||||||
await account_manager.deal_restore_system(param)
|
restore_result = await account_manager.deal_restore_system(param)
|
||||||
return {"code": 200, "message": f"任务:{params.get("pad_code")} 备份还原成功" }
|
if restore_result and result:
|
||||||
return {"code": 200, "message": f"任务:{params.get("pad_code")} 备份成功", }
|
return {"code": 200, "message": f"任务:{params.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")} 备份失败")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=404, detail=f"任务:{params.get("task_id")} 重启失败")
|
raise HTTPException(status_code=404, detail=f"云机:{param.get("pad_code")} 备份失败,失败原因:{e}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue