文件
workbuddy-portal/workbuddy_portal/scheduler.py
T
wangchuanli df7db3582e feat(multi-user): 多用户化 + 凭证加密 + 自助注册与图形验证码
数据隔离
- settings / usage_records 主键改为 (user_id, key) / (user_id, request_id),
  索引一律以 user_id 打头;collect_runs / audit_log 增加 user_id
- query / collect / scheduler 全链路把 uid 作为 conn 之后的第一个位置参数且无默认值
  (漏传直接 TypeError,不会退化成「返回全量」)
- 配置三级回落 个人→实例→DEFAULTS;NO_FALLBACK_KEYS={cookie,user_agent} 不回落

凭证保密
- 新增 workbuddy_portal/crypto.py:手写 ChaCha20(RFC8439 §2.3) + HMAC-SHA256
  encrypt-then-MAC,零第三方依赖;主密钥 cookie_key 与 SECRET_KEY 分键位存放
- get_secret() 是取明文的唯一通道;get_settings() 把加密键置空;
  secret_state() 只回 {set,chars,tail,broken};升级时自动加密历史明文

注册与验证码
- 新增 /register 与 workbuddy_portal/captcha.py(手写 PNG + 点阵字模 + 干扰线)
- 验证码答案只存服务端表、不进 session,一次性、5 分钟过期、按 purpose 隔离
- allow_register / register_max_per_ip / captcha_policy / captcha_length 四个实例级开关
- 失败限速改为 IP + 用户名双维度;停用账号每请求回查、立即失效

页面
- 新增 /profile(个人中心)与注册页;登录页加验证码与自助注册入口
- /config 增加凭证状态、cookie_broken 告警、实例级设置区;/users 增加邮箱/状态与启停

修复
- base.html 顶层 {% set me %} 覆盖子模板同名变量,导致个人中心「注册于」渲染为空
- WB_COOKIE_SECURE 未写进 compose 的 environment,在 .env 里设了不生效
- 「修改登录密码」提示写「至少 6 位」,与实际策略(≥8 位 + 两类字符)不符
- 「用户管理」删除说明写「可勾选保留」,与页面实际行为不符
- 注册页与 flash 文案里的 **强调** Markdown 字面量

验证与文档
- smoke.py 99 → 165 项断言(多用户隔离 / 凭证保密 / 注册与验证码 / 3 条防回归)
- check_live.py 56 → 83 项断言(新增注册 / 验证码 / 安全响应头一节)
- demo_data.py 造两个账号;shots.py 自动过验证码、重出 11 张截图
- README / SECURITY / ARCHITECTURE / API / DEPLOYMENT / USER-GUIDE / FAQ / CHANGELOG / CONTRIBUTING 同步
2026-09-15 17:32:35 +08:00

202 行
7.5 KiB
Python
原始文件 Blame 文件历史

此文件含有模棱两可的 Unicode 字符
此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: MIT
# Copyright (c) 2026 Wang Chuanli
"""进程内调度器(手写,不依赖 APScheduler)。
为什么不引 APScheduler:
* 需求只是「每天固定几个时刻跑一次」,一个线程 + 20 秒轮询足够
* 需要「启动补跑」(程序没开的时候错过了时刻,开机后要补上)
* 需要和 CLI 共享同一把文件锁,避免两处同时采集
多用户
------
调度配置(开关 / 时刻 / 补跑 / slot:* 簿记)都是**个人级**设置,
所以 tick 会遍历所有启用状态的账号,各自判断有没有到期槽位。
好处是「A 想 9 点采、B 想 21 点采」互不影响;代价是串行执行 ——
这是刻意的,SQLite 单写者不允许并发采集。
单实例保证:
* Flask 的 reloader 会 fork 两个进程 → 只在 WERKZEUG_RUN_MAIN 里启动
* 多进程部署时用环境变量 WB_DISABLE_SCHEDULER=1 关掉除一个之外的所有实例
* 真正防重复采集靠 collect._Lock(文件锁),即使两个调度线程同时触发也只会跑一个
"""
import logging
import os
import threading
from datetime import datetime, timedelta
from . import collect, db
log = logging.getLogger("wb.scheduler")
SLOT_PREFIX = "slot:" # settings 键:slot:09:00 -> 最近执行的日期(按 user_id 存)
def parse_times(raw):
"""把 '09:00,17:00' 解析成 ['09:00','17:00'];非法片段直接丢弃。
对外公开(config.normalize_setting 用它做写入校验),所以不要改名。
"""
out = []
for part in str(raw or "").replace(";", ",").replace(",", ",").split(","):
p = part.strip()
if not p:
continue
bits = p.split(":")
try:
hh = int(bits[0])
mm = int(bits[1]) if len(bits) > 1 else 0
except ValueError:
continue
if 0 <= hh <= 23 and 0 <= mm <= 59:
out.append("%02d:%02d" % (hh, mm))
return sorted(set(out))
# 兼容旧名(早期版本用 _parse_times)
_parse_times = parse_times
def slots(conn, uid=0):
return parse_times(db.get_setting(conn, "schedule_times", uid=uid))
def last_run_of_slot(conn, uid, slot):
return db.get_setting(conn, SLOT_PREFIX + slot, "", uid)
def mark_slot(conn, uid, slot, day):
db.set_setting(conn, SLOT_PREFIX + slot, day, uid)
def next_run_at(conn, uid=0, now=None):
"""下一次计划执行时间(仅按配置推算,不含补跑)。"""
if not db.get_bool(conn, "schedule_enabled", True, uid):
return None
now = now or datetime.now()
best = None
for s in slots(conn, uid):
hh, mm = map(int, s.split(":"))
cand = now.replace(hour=hh, minute=mm, second=0, microsecond=0)
if cand <= now:
cand += timedelta(days=1)
if best is None or cand < best:
best = cand
return best
def due_slots(conn, uid=0, now=None):
"""返回此刻应当执行的槽位列表(含启动补跑)。"""
if not db.get_bool(conn, "schedule_enabled", True, uid):
return []
now = now or datetime.now()
today = now.strftime("%Y-%m-%d")
# 用 get_int 兜底:catch_up_grace_hours 在后台是自由文本框,
# 历史上填成 "12h" 会让这里 int() 抛 ValueError,把 /tasks 打成 500。
grace_hours = db.get_int(conn, "catch_up_grace_hours", 12, uid)
grace = timedelta(hours=max(1, grace_hours))
catch_up = db.get_bool(conn, "catch_up", True, uid)
out = []
for s in slots(conn, uid):
hh, mm = map(int, s.split(":"))
when = now.replace(hour=hh, minute=mm, second=0, microsecond=0)
if when > now:
continue # 还没到点
if last_run_of_slot(conn, uid, s) == today:
continue # 今天这个槽位已跑过
if when < now - grace and catch_up:
continue # 错过太久,不补(避免开机狂刷)
if when < now - timedelta(seconds=90) and not catch_up:
continue # 未开启补跑,只认刚到的点
out.append(s)
return out
class Scheduler:
def __init__(self, interval=20):
self.interval = interval
self._stop = threading.Event()
self._thread = None
# ---- 生命周期 ----
def start(self):
if self._thread and self._thread.is_alive():
return False
self._stop.clear()
self._thread = threading.Thread(target=self._loop, name="wb-scheduler", daemon=True)
self._thread.start()
log.info("调度器已启动,轮询间隔 %ss", self.interval)
return True
def stop(self):
self._stop.set()
if self._thread:
self._thread.join(timeout=5)
@property
def running(self):
return bool(self._thread and self._thread.is_alive())
# ---- 主循环 ----
def _loop(self):
while not self._stop.is_set():
try:
self.tick()
except Exception as e: # 任何异常都不能让线程死掉
log.exception("调度 tick 出错:%s", e)
self._stop.wait(self.interval)
def tick(self, now=None):
conn = db.thread_conn()
now = now or datetime.now()
today = now.strftime("%Y-%m-%d")
for u in db.active_users(conn):
uid = u["id"]
try:
pending = due_slots(conn, uid, now)
except Exception as e: # 单个账号配置坏了不能拖垮其他人
log.error("账号 #%s(%s) 读取调度配置失败:%s", uid, u["username"], e)
continue
for slot in pending:
scheduled = now.replace(hour=int(slot[:2]), minute=int(slot[3:]),
second=0, microsecond=0)
trigger = "startup" if now - scheduled > timedelta(minutes=5) else "schedule"
log.info("触发采集:账号 %s 槽位 %s(%s)", u["username"], slot, trigger)
# 先占位,避免采集失败被无限重试打爆云端
mark_slot(conn, uid, slot, today)
if not db.secret_state(conn, "cookie", uid)["set"]:
log.info("跳过:账号 %s 还没配置自己的 Cookie", u["username"])
continue
try:
r = collect.run_sync(trigger=trigger, uid=uid)
log.info("采集完成:%s → %s", u["username"], r["message"])
except collect.Busy as e:
log.warning("跳过(%s)", e)
except db.SecretUnreadable as e:
log.error("账号 %s 的 Cookie 解不开:%s", u["username"], e)
except Exception as e:
log.error("账号 %s 采集失败:%s", u["username"], e)
return True
_scheduler = None
def get_scheduler(interval=20):
global _scheduler
if _scheduler is None:
_scheduler = Scheduler(interval=interval)
return _scheduler
def start_from_app(app):
"""由 create_app 调用。遵守 reloader 与显式禁用开关。"""
if os.environ.get("WB_DISABLE_SCHEDULER") == "1":
app.logger.info("WB_DISABLE_SCHEDULER=1,调度器未启动")
return None
if app.debug and os.environ.get("WERKZEUG_RUN_MAIN") != "true":
return None # reloader 的父进程不启动,避免跑两份
sch = get_scheduler()
sch.start()
return sch