refactor: optimize structure, stability and runtime performance
This commit is contained in:
292
db/schedules.py
292
db/schedules.py
@@ -2,12 +2,93 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
import json
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import db_pool
|
||||
from services.schedule_utils import compute_next_run_at, format_cst
|
||||
from services.time_utils import get_beijing_now
|
||||
|
||||
_SCHEDULE_DEFAULT_TIME = "08:00"
|
||||
_SCHEDULE_DEFAULT_WEEKDAYS = "1,2,3,4,5"
|
||||
|
||||
_ALLOWED_SCHEDULE_UPDATE_FIELDS = (
|
||||
"name",
|
||||
"enabled",
|
||||
"schedule_time",
|
||||
"weekdays",
|
||||
"browse_type",
|
||||
"enable_screenshot",
|
||||
"random_delay",
|
||||
"account_ids",
|
||||
)
|
||||
|
||||
_ALLOWED_EXEC_LOG_UPDATE_FIELDS = (
|
||||
"total_accounts",
|
||||
"success_accounts",
|
||||
"failed_accounts",
|
||||
"total_items",
|
||||
"total_attachments",
|
||||
"total_screenshots",
|
||||
"duration_seconds",
|
||||
"status",
|
||||
"error_message",
|
||||
)
|
||||
|
||||
|
||||
def _normalize_limit(limit, default: int, *, minimum: int = 1) -> int:
|
||||
try:
|
||||
parsed = int(limit)
|
||||
except Exception:
|
||||
parsed = default
|
||||
if parsed < minimum:
|
||||
return minimum
|
||||
return parsed
|
||||
|
||||
|
||||
def _to_int(value, default: int = 0) -> int:
|
||||
try:
|
||||
return int(value)
|
||||
except Exception:
|
||||
return default
|
||||
|
||||
|
||||
def _format_optional_datetime(dt: datetime | None) -> str | None:
|
||||
if dt is None:
|
||||
return None
|
||||
return format_cst(dt)
|
||||
|
||||
|
||||
def _serialize_account_ids(account_ids) -> str:
|
||||
return json.dumps(account_ids) if account_ids else "[]"
|
||||
|
||||
|
||||
def _compute_schedule_next_run_str(
|
||||
*,
|
||||
now_dt,
|
||||
schedule_time,
|
||||
weekdays,
|
||||
random_delay,
|
||||
last_run_at,
|
||||
) -> str:
|
||||
next_dt = compute_next_run_at(
|
||||
now=now_dt,
|
||||
schedule_time=str(schedule_time or _SCHEDULE_DEFAULT_TIME),
|
||||
weekdays=str(weekdays or _SCHEDULE_DEFAULT_WEEKDAYS),
|
||||
random_delay=_to_int(random_delay, 0),
|
||||
last_run_at=str(last_run_at or "") if last_run_at else None,
|
||||
)
|
||||
return format_cst(next_dt)
|
||||
|
||||
|
||||
def _map_schedule_log_row(row) -> dict:
|
||||
log = dict(row)
|
||||
log["created_at"] = log.get("execute_time")
|
||||
log["success_count"] = log.get("success_accounts", 0)
|
||||
log["failed_count"] = log.get("failed_accounts", 0)
|
||||
log["duration"] = log.get("duration_seconds", 0)
|
||||
return log
|
||||
|
||||
|
||||
def get_user_schedules(user_id):
|
||||
"""获取用户的所有定时任务"""
|
||||
@@ -44,14 +125,10 @@ def create_user_schedule(
|
||||
account_ids=None,
|
||||
):
|
||||
"""创建用户定时任务"""
|
||||
import json
|
||||
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
cst_time = format_cst(get_beijing_now())
|
||||
|
||||
account_ids_str = json.dumps(account_ids) if account_ids else "[]"
|
||||
|
||||
cursor.execute(
|
||||
"""
|
||||
INSERT INTO user_schedules (
|
||||
@@ -66,8 +143,8 @@ def create_user_schedule(
|
||||
weekdays,
|
||||
browse_type,
|
||||
enable_screenshot,
|
||||
int(random_delay or 0),
|
||||
account_ids_str,
|
||||
_to_int(random_delay, 0),
|
||||
_serialize_account_ids(account_ids),
|
||||
cst_time,
|
||||
cst_time,
|
||||
),
|
||||
@@ -79,28 +156,11 @@ def create_user_schedule(
|
||||
|
||||
def update_user_schedule(schedule_id, **kwargs):
|
||||
"""更新用户定时任务"""
|
||||
import json
|
||||
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
now_dt = get_beijing_now()
|
||||
now_str = format_cst(now_dt)
|
||||
|
||||
updates = []
|
||||
params = []
|
||||
|
||||
allowed_fields = [
|
||||
"name",
|
||||
"enabled",
|
||||
"schedule_time",
|
||||
"weekdays",
|
||||
"browse_type",
|
||||
"enable_screenshot",
|
||||
"random_delay",
|
||||
"account_ids",
|
||||
]
|
||||
|
||||
# 读取旧值,用于决定是否需要重算 next_run_at
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT enabled, schedule_time, weekdays, random_delay, last_run_at
|
||||
@@ -112,10 +172,11 @@ def update_user_schedule(schedule_id, **kwargs):
|
||||
current = cursor.fetchone()
|
||||
if not current:
|
||||
return False
|
||||
current_enabled = int(current[0] or 0)
|
||||
|
||||
current_enabled = _to_int(current[0], 0)
|
||||
current_time = current[1]
|
||||
current_weekdays = current[2]
|
||||
current_random_delay = int(current[3] or 0)
|
||||
current_random_delay = _to_int(current[3], 0)
|
||||
current_last_run_at = current[4]
|
||||
|
||||
will_enabled = current_enabled
|
||||
@@ -123,21 +184,28 @@ def update_user_schedule(schedule_id, **kwargs):
|
||||
next_weekdays = current_weekdays
|
||||
next_random_delay = current_random_delay
|
||||
|
||||
for field in allowed_fields:
|
||||
if field in kwargs:
|
||||
value = kwargs[field]
|
||||
if field == "account_ids" and isinstance(value, list):
|
||||
value = json.dumps(value)
|
||||
if field == "enabled":
|
||||
will_enabled = 1 if value else 0
|
||||
if field == "schedule_time":
|
||||
next_time = value
|
||||
if field == "weekdays":
|
||||
next_weekdays = value
|
||||
if field == "random_delay":
|
||||
next_random_delay = int(value or 0)
|
||||
updates.append(f"{field} = ?")
|
||||
params.append(value)
|
||||
updates = []
|
||||
params = []
|
||||
|
||||
for field in _ALLOWED_SCHEDULE_UPDATE_FIELDS:
|
||||
if field not in kwargs:
|
||||
continue
|
||||
|
||||
value = kwargs[field]
|
||||
if field == "account_ids" and isinstance(value, list):
|
||||
value = json.dumps(value)
|
||||
|
||||
if field == "enabled":
|
||||
will_enabled = 1 if value else 0
|
||||
if field == "schedule_time":
|
||||
next_time = value
|
||||
if field == "weekdays":
|
||||
next_weekdays = value
|
||||
if field == "random_delay":
|
||||
next_random_delay = int(value or 0)
|
||||
|
||||
updates.append(f"{field} = ?")
|
||||
params.append(value)
|
||||
|
||||
if not updates:
|
||||
return False
|
||||
@@ -145,30 +213,26 @@ def update_user_schedule(schedule_id, **kwargs):
|
||||
updates.append("updated_at = ?")
|
||||
params.append(now_str)
|
||||
|
||||
# 关键字段变更后重算 next_run_at,确保索引驱动不会跑偏
|
||||
#
|
||||
# 需求:当用户修改“执行时间/执行日期/随机±15分钟”后,即使今天已经执行过,也允许按新配置在今天再次触发。
|
||||
# 做法:这些关键字段发生变更时,重算 next_run_at 时忽略 last_run_at 的“同日仅一次”限制。
|
||||
config_changed = any(key in kwargs for key in ["schedule_time", "weekdays", "random_delay"])
|
||||
config_changed = any(key in kwargs for key in ("schedule_time", "weekdays", "random_delay"))
|
||||
enabled_toggled = "enabled" in kwargs
|
||||
should_recompute_next = config_changed or (enabled_toggled and will_enabled == 1)
|
||||
|
||||
if should_recompute_next:
|
||||
next_dt = compute_next_run_at(
|
||||
now=now_dt,
|
||||
schedule_time=str(next_time or "08:00"),
|
||||
weekdays=str(next_weekdays or "1,2,3,4,5"),
|
||||
random_delay=int(next_random_delay or 0),
|
||||
last_run_at=None if config_changed else (str(current_last_run_at or "") if current_last_run_at else None),
|
||||
next_run_at = _compute_schedule_next_run_str(
|
||||
now_dt=now_dt,
|
||||
schedule_time=next_time,
|
||||
weekdays=next_weekdays,
|
||||
random_delay=next_random_delay,
|
||||
last_run_at=None if config_changed else current_last_run_at,
|
||||
)
|
||||
updates.append("next_run_at = ?")
|
||||
params.append(format_cst(next_dt))
|
||||
params.append(next_run_at)
|
||||
|
||||
# 若本次显式禁用任务,则 next_run_at 清空(与 toggle 行为保持一致)
|
||||
if enabled_toggled and will_enabled == 0:
|
||||
updates.append("next_run_at = ?")
|
||||
params.append(None)
|
||||
params.append(schedule_id)
|
||||
|
||||
params.append(schedule_id)
|
||||
sql = f"UPDATE user_schedules SET {', '.join(updates)} WHERE id = ?"
|
||||
cursor.execute(sql, params)
|
||||
conn.commit()
|
||||
@@ -203,28 +267,19 @@ def toggle_user_schedule(schedule_id, enabled):
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
if row:
|
||||
schedule_time, weekdays, random_delay, last_run_at, existing_next_run_at = (
|
||||
row[0],
|
||||
row[1],
|
||||
row[2],
|
||||
row[3],
|
||||
row[4],
|
||||
)
|
||||
|
||||
schedule_time, weekdays, random_delay, last_run_at, existing_next_run_at = row
|
||||
existing_next_run_at = str(existing_next_run_at or "").strip() or None
|
||||
# 若 next_run_at 已经被“修改配置”逻辑预先计算好且仍在未来,则优先沿用,
|
||||
# 避免 last_run_at 的“同日仅一次”限制阻塞用户把任务调整到今天再次触发。
|
||||
|
||||
if existing_next_run_at and existing_next_run_at > now_str:
|
||||
next_run_at = existing_next_run_at
|
||||
else:
|
||||
next_dt = compute_next_run_at(
|
||||
now=now_dt,
|
||||
schedule_time=str(schedule_time or "08:00"),
|
||||
weekdays=str(weekdays or "1,2,3,4,5"),
|
||||
random_delay=int(random_delay or 0),
|
||||
last_run_at=str(last_run_at or "") if last_run_at else None,
|
||||
next_run_at = _compute_schedule_next_run_str(
|
||||
now_dt=now_dt,
|
||||
schedule_time=schedule_time,
|
||||
weekdays=weekdays,
|
||||
random_delay=random_delay,
|
||||
last_run_at=last_run_at,
|
||||
)
|
||||
next_run_at = format_cst(next_dt)
|
||||
|
||||
cursor.execute(
|
||||
"""
|
||||
@@ -272,16 +327,15 @@ def update_schedule_last_run(schedule_id):
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
return False
|
||||
schedule_time, weekdays, random_delay = row[0], row[1], row[2]
|
||||
|
||||
next_dt = compute_next_run_at(
|
||||
now=now_dt,
|
||||
schedule_time=str(schedule_time or "08:00"),
|
||||
weekdays=str(weekdays or "1,2,3,4,5"),
|
||||
random_delay=int(random_delay or 0),
|
||||
schedule_time, weekdays, random_delay = row
|
||||
next_run_at = _compute_schedule_next_run_str(
|
||||
now_dt=now_dt,
|
||||
schedule_time=schedule_time,
|
||||
weekdays=weekdays,
|
||||
random_delay=random_delay,
|
||||
last_run_at=now_str,
|
||||
)
|
||||
next_run_at = format_cst(next_dt)
|
||||
|
||||
cursor.execute(
|
||||
"""
|
||||
@@ -305,7 +359,11 @@ def update_schedule_next_run(schedule_id: int, next_run_at: str) -> bool:
|
||||
SET next_run_at = ?, updated_at = ?
|
||||
WHERE id = ?
|
||||
""",
|
||||
(str(next_run_at or "").strip() or None, format_cst(get_beijing_now()), int(schedule_id)),
|
||||
(
|
||||
str(next_run_at or "").strip() or None,
|
||||
format_cst(get_beijing_now()),
|
||||
int(schedule_id),
|
||||
),
|
||||
)
|
||||
conn.commit()
|
||||
return cursor.rowcount > 0
|
||||
@@ -328,15 +386,15 @@ def recompute_schedule_next_run(schedule_id: int, *, now_dt=None) -> bool:
|
||||
if not row:
|
||||
return False
|
||||
|
||||
schedule_time, weekdays, random_delay, last_run_at = row[0], row[1], row[2], row[3]
|
||||
next_dt = compute_next_run_at(
|
||||
now=now_dt,
|
||||
schedule_time=str(schedule_time or "08:00"),
|
||||
weekdays=str(weekdays or "1,2,3,4,5"),
|
||||
random_delay=int(random_delay or 0),
|
||||
last_run_at=str(last_run_at or "") if last_run_at else None,
|
||||
schedule_time, weekdays, random_delay, last_run_at = row
|
||||
next_run_at = _compute_schedule_next_run_str(
|
||||
now_dt=now_dt,
|
||||
schedule_time=schedule_time,
|
||||
weekdays=weekdays,
|
||||
random_delay=random_delay,
|
||||
last_run_at=last_run_at,
|
||||
)
|
||||
return update_schedule_next_run(int(schedule_id), format_cst(next_dt))
|
||||
return update_schedule_next_run(int(schedule_id), next_run_at)
|
||||
|
||||
|
||||
def get_due_user_schedules(now_cst: str, limit: int = 50):
|
||||
@@ -345,6 +403,8 @@ def get_due_user_schedules(now_cst: str, limit: int = 50):
|
||||
if not now_cst:
|
||||
now_cst = format_cst(get_beijing_now())
|
||||
|
||||
safe_limit = _normalize_limit(limit, 50, minimum=1)
|
||||
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
@@ -358,7 +418,7 @@ def get_due_user_schedules(now_cst: str, limit: int = 50):
|
||||
ORDER BY us.next_run_at ASC
|
||||
LIMIT ?
|
||||
""",
|
||||
(now_cst, int(limit)),
|
||||
(now_cst, safe_limit),
|
||||
)
|
||||
return [dict(row) for row in cursor.fetchall()]
|
||||
|
||||
@@ -370,15 +430,13 @@ def create_schedule_execution_log(schedule_id, user_id, schedule_name):
|
||||
"""创建定时任务执行日志"""
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
execute_time = format_cst(get_beijing_now())
|
||||
|
||||
cursor.execute(
|
||||
"""
|
||||
INSERT INTO schedule_execution_logs (
|
||||
schedule_id, user_id, schedule_name, execute_time, status
|
||||
) VALUES (?, ?, ?, ?, 'running')
|
||||
""",
|
||||
(schedule_id, user_id, schedule_name, execute_time),
|
||||
(schedule_id, user_id, schedule_name, format_cst(get_beijing_now())),
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
@@ -393,22 +451,11 @@ def update_schedule_execution_log(log_id, **kwargs):
|
||||
updates = []
|
||||
params = []
|
||||
|
||||
allowed_fields = [
|
||||
"total_accounts",
|
||||
"success_accounts",
|
||||
"failed_accounts",
|
||||
"total_items",
|
||||
"total_attachments",
|
||||
"total_screenshots",
|
||||
"duration_seconds",
|
||||
"status",
|
||||
"error_message",
|
||||
]
|
||||
|
||||
for field in allowed_fields:
|
||||
if field in kwargs:
|
||||
updates.append(f"{field} = ?")
|
||||
params.append(kwargs[field])
|
||||
for field in _ALLOWED_EXEC_LOG_UPDATE_FIELDS:
|
||||
if field not in kwargs:
|
||||
continue
|
||||
updates.append(f"{field} = ?")
|
||||
params.append(kwargs[field])
|
||||
|
||||
if not updates:
|
||||
return False
|
||||
@@ -424,6 +471,7 @@ def update_schedule_execution_log(log_id, **kwargs):
|
||||
def get_schedule_execution_logs(schedule_id, limit=10):
|
||||
"""获取定时任务执行日志"""
|
||||
try:
|
||||
safe_limit = _normalize_limit(limit, 10, minimum=1)
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
@@ -433,24 +481,16 @@ def get_schedule_execution_logs(schedule_id, limit=10):
|
||||
ORDER BY execute_time DESC
|
||||
LIMIT ?
|
||||
""",
|
||||
(schedule_id, limit),
|
||||
(schedule_id, safe_limit),
|
||||
)
|
||||
|
||||
logs = []
|
||||
rows = cursor.fetchall()
|
||||
|
||||
for row in rows:
|
||||
for row in cursor.fetchall():
|
||||
try:
|
||||
log = dict(row)
|
||||
log["created_at"] = log.get("execute_time")
|
||||
log["success_count"] = log.get("success_accounts", 0)
|
||||
log["failed_count"] = log.get("failed_accounts", 0)
|
||||
log["duration"] = log.get("duration_seconds", 0)
|
||||
logs.append(log)
|
||||
logs.append(_map_schedule_log_row(row))
|
||||
except Exception as e:
|
||||
print(f"[数据库] 处理日志行时出错: {e}")
|
||||
continue
|
||||
|
||||
return logs
|
||||
except Exception as e:
|
||||
print(f"[数据库] 查询定时任务日志时出错: {e}")
|
||||
@@ -462,6 +502,7 @@ def get_schedule_execution_logs(schedule_id, limit=10):
|
||||
|
||||
def get_user_all_schedule_logs(user_id, limit=50):
|
||||
"""获取用户所有定时任务的执行日志"""
|
||||
safe_limit = _normalize_limit(limit, 50, minimum=1)
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
@@ -471,7 +512,7 @@ def get_user_all_schedule_logs(user_id, limit=50):
|
||||
ORDER BY execute_time DESC
|
||||
LIMIT ?
|
||||
""",
|
||||
(user_id, limit),
|
||||
(user_id, safe_limit),
|
||||
)
|
||||
return [dict(row) for row in cursor.fetchall()]
|
||||
|
||||
@@ -493,14 +534,21 @@ def delete_schedule_logs(schedule_id, user_id):
|
||||
|
||||
def clean_old_schedule_logs(days=30):
|
||||
"""清理指定天数前的定时任务执行日志"""
|
||||
safe_days = _to_int(days, 30)
|
||||
if safe_days < 0:
|
||||
safe_days = 0
|
||||
|
||||
cutoff_dt = get_beijing_now() - timedelta(days=safe_days)
|
||||
cutoff_str = format_cst(cutoff_dt)
|
||||
|
||||
with db_pool.get_db() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"""
|
||||
DELETE FROM schedule_execution_logs
|
||||
WHERE execute_time < datetime('now', 'localtime', '-' || ? || ' days')
|
||||
WHERE execute_time < ?
|
||||
""",
|
||||
(days,),
|
||||
(cutoff_str,),
|
||||
)
|
||||
conn.commit()
|
||||
return cursor.rowcount
|
||||
|
||||
Reference in New Issue
Block a user