同步本地更改

This commit is contained in:
2025-12-15 10:48:58 +08:00
parent dab29347bd
commit a619e96e73
3 changed files with 139 additions and 13 deletions

View File

@@ -188,6 +188,9 @@ def toggle_schedule_api(schedule_id):
def run_schedule_now_api(schedule_id):
"""立即执行定时任务"""
import json
import threading
import time as time_mod
import uuid
schedule = database.get_schedule_by_id(schedule_id)
if not schedule:
@@ -210,27 +213,104 @@ def run_schedule_now_api(schedule_id):
if not safe_get_user_accounts_snapshot(user_id):
load_user_accounts(user_id)
started = []
from services.state import safe_create_batch, safe_finalize_batch_after_dispatch
from services.task_batches import _send_batch_task_email_if_configured
execution_start_time = time_mod.time()
log_id = database.create_schedule_execution_log(
schedule_id=schedule_id,
user_id=user_id,
schedule_name=schedule.get("name", "未命名任务"),
)
batch_id = f"batch_{uuid.uuid4().hex[:12]}"
now_ts = time_mod.time()
safe_create_batch(
batch_id,
{
"user_id": user_id,
"browse_type": browse_type,
"schedule_name": schedule.get("name", "未命名任务"),
"screenshots": [],
"total_accounts": 0,
"completed": 0,
"created_at": now_ts,
"updated_at": now_ts,
},
)
started_count = 0
skipped_count = 0
completion_lock = threading.Lock()
remaining = {"count": 0, "done": False}
def on_browse_done():
with completion_lock:
remaining["count"] -= 1
if remaining["done"] or remaining["count"] > 0:
return
remaining["done"] = True
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",
)
for account_id in account_ids:
account = safe_get_account(user_id, account_id)
if not account:
skipped_count += 1
continue
if account.is_running:
skipped_count += 1
continue
task_source = f"user_scheduled:{batch_id}"
with completion_lock:
remaining["count"] += 1
ok, msg = submit_account_task(
user_id=user_id,
account_id=account_id,
browse_type=browse_type,
enable_screenshot=enable_screenshot,
source="user_scheduled",
source=task_source,
done_callback=on_browse_done,
)
if ok:
started.append(account_id)
started_count += 1
else:
with completion_lock:
remaining["count"] -= 1
skipped_count += 1
batch_info = safe_finalize_batch_after_dispatch(batch_id, started_count, now_ts=time_mod.time())
if batch_info:
_send_batch_task_email_if_configured(batch_info)
database.update_schedule_last_run(schedule_id)
return jsonify({"success": True, "started_count": len(started), "message": f"已启动 {len(started)} 个账号"})
if started_count <= 0:
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",
)
return jsonify(
{
"success": True,
"started_count": started_count,
"skipped_count": skipped_count,
"message": f"已启动 {started_count} 个账号",
}
)
@api_schedules_bp.route("/api/schedules/<int:schedule_id>/logs", methods=["GET"])