修改平差时找不到平差按钮就返回。
This commit is contained in:
35
scheduler.py
35
scheduler.py
@@ -152,23 +152,28 @@ def get_combined_tasks():
|
||||
|
||||
def run_task(address, target_time, username):
|
||||
"""
|
||||
单个执行线程:锁定状态 -> 等待 -> 执行 -> 完成
|
||||
单个执行线程:检查时间 -> 锁定状态 -> 执行 -> 完成
|
||||
"""
|
||||
# 1. 尝试将状态从 ok 改为 running (锁定任务)
|
||||
# 如果此时文件状态已被其他逻辑修改,则放弃执行,防止重复
|
||||
if not update_file_status(username, "ok", "running"):
|
||||
return f"⏭️ {username} 状态已变更,跳过执行。"
|
||||
|
||||
print(f"🚀 [任务锁定] 设备: {address} | 用户: {username} | 计划时间: {target_time}")
|
||||
print(f"📅 [任务检查] 设备: {address} | 用户: {username} | 计划时间: {target_time}")
|
||||
|
||||
try:
|
||||
# 2. 计算并执行等待逻辑
|
||||
# 1. 检查当前时间是否到达计划时间
|
||||
target_dt = datetime.strptime(target_time, "%Y-%m-%d %H:%M:%S")
|
||||
wait_secs = (target_dt - datetime.now()).total_seconds()
|
||||
current_dt = datetime.now()
|
||||
|
||||
if wait_secs > 0:
|
||||
print(f"⏳ {username} 距离执行还有 {int(wait_secs)} 秒...")
|
||||
time.sleep(wait_secs)
|
||||
# 如果当前时间还未到达计划时间,直接返回,等待下次轮询
|
||||
if current_dt < target_dt:
|
||||
time_diff = int((target_dt - current_dt).total_seconds())
|
||||
print(f"⏰ {username} 计划时间未到,距离执行还有 {time_diff} 秒,等待下次轮询")
|
||||
return f"⏰ {username} 计划时间未到,等待下次轮询"
|
||||
|
||||
# 2. 开始执行前,尝试将状态从 ok 改为 running (锁定任务)
|
||||
# 如果此时文件状态已被其他逻辑修改,则放弃执行,防止重复
|
||||
print(f"🔒 [准备锁定] 尝试锁定任务状态: {username}")
|
||||
if not update_file_status(username, "ok", "running"):
|
||||
return f"⏭️ {username} 状态已变更,跳过执行。"
|
||||
|
||||
print(f"🚀 [任务锁定] 设备: {address} | 用户: {username} | 计划时间: {target_time}")
|
||||
|
||||
# 3. 调用 main.py 中的自动化逻辑
|
||||
print(f"▶️ [正在执行] {username} 开始自动化操作...")
|
||||
@@ -181,7 +186,11 @@ def run_task(address, target_time, username):
|
||||
|
||||
except Exception as e:
|
||||
# 如果中间报错,将状态改为 error 方便排查
|
||||
update_file_status(username, "running", "error")
|
||||
# 只有在状态已经改为 running 的情况下才需要改为 error
|
||||
try:
|
||||
update_file_status(username, "running", "error")
|
||||
except:
|
||||
pass
|
||||
return f"❌ {username} 执行异常: {str(e)}"
|
||||
|
||||
def monitor_center():
|
||||
|
||||
Reference in New Issue
Block a user