From e07544784caa8fb5d24352f85ca0d51b839097b2 Mon Sep 17 00:00:00 2001 From: yuyx <237899745@qq.com> Date: Wed, 10 Dec 2025 22:09:30 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E4=BB=BB=E5=8A=A1=E6=89=A7=E8=A1=8C=E8=80=97=E6=97=B6?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E4=B8=BA0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 启动监控线程等待所有任务完成后再更新日志 - 通过参数传递避免闭包问题 - 耗时现在正确反映实际任务执行时间 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- app.py | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/app.py b/app.py index 5923cc3..f845395 100755 --- a/app.py +++ b/app.py @@ -2995,6 +2995,7 @@ def scheduled_task_worker(): started_count = 0 skipped_count = 0 + task_threads = [] # 收集所有启动的任务线程 for account_id in account_ids: if user_id not in user_accounts: @@ -3019,6 +3020,7 @@ def scheduled_task_worker(): ) thread.start() active_tasks[account_id] = thread + task_threads.append(thread) started_count += 1 socketio.emit('account_update', account.to_dict(), room=f'user_{user_id}') @@ -3026,18 +3028,40 @@ def scheduled_task_worker(): # 更新最后执行时间 database.update_schedule_last_run(schedule_id) - # 更新执行日志 - execution_duration = int(time_mod.time() - execution_start_time) - database.update_schedule_execution_log( - log_id, - total_accounts=len(account_ids), - success_accounts=started_count, - failed_accounts=len(account_ids) - started_count, - duration_seconds=execution_duration, - status='completed' - ) - print(f"[用户定时任务] 已启动 {started_count} 个账号,跳过 {skipped_count} 个账号") + + # 启动监控线程,等待所有任务完成后更新日志 + def wait_and_update_log(threads, start_time, lid, total, success, sid): + for t in threads: + t.join() # 等待每个任务完成 + execution_duration = int(time_mod.time() - start_time) + database.update_schedule_execution_log( + lid, + total_accounts=total, + success_accounts=success, + failed_accounts=total - success, + duration_seconds=execution_duration, + status='completed' + ) + print(f"[用户定时任务] 任务#{sid}执行完成,耗时{execution_duration}秒") + + if task_threads: + monitor_thread = threading.Thread( + target=wait_and_update_log, + args=(task_threads, execution_start_time, log_id, len(account_ids), started_count, schedule_id), + daemon=True + ) + monitor_thread.start() + else: + # 没有启动任何任务,直接更新日志 + database.update_schedule_execution_log( + log_id, + total_accounts=len(account_ids), + success_accounts=0, + failed_accounts=len(account_ids), + duration_seconds=0, + status='completed' + ) if started_count == 0 and len(account_ids) > 0: print(f"[用户定时任务] ⚠️ 警告:所有账号都被跳过了!请检查user_accounts状态")