定时自动化(中文译文)
原始 DeepWiki 页面:https://deepwiki.com/open-webui/open-webui/20.1-scheduled-automations
翻译时间:2026-06-09T16:12:46.409Z
翻译模型:deepseek-chat
原文字符数:8641
项目:Open WebUI (open-webui)
---
定时自动化
相关源文件
以下文件为本 wiki 页面的生成提供了上下文:
backend/open_webui/migrations/versions/d4e5f6a7b8c9_add_automation_tables.pybackend/open_webui/models/automations.pybackend/open_webui/routers/automations.pybackend/open_webui/utils/automations.pysrc/lib/apis/automations/index.tssrc/lib/components/AutomationModal.sveltesrc/lib/components/automations/AutomationEditor.sveltesrc/lib/components/automations/AutomationMenu.sveltesrc/lib/components/layout/Sidebar/UserMenu.sveltesrc/lib/components/workspace/Models/TerminalSelector.sveltesrc/routes/(app)/automations/+page.svelte/automations/+page.svelte)- [src/routes/(app)/automations/[id]/+page.svelte](src/routes/(app)/automations/[id]/+page.svelte)
定时自动化引擎允许用户使用标准 iCalendar RRULE 语法自动化重复性 AI 任务 backend/open_webui/utils/automations.py:26-26。该系统实现了"设置后即遗忘"的工作流,模型可按特定计划被触发以生成内容、处理数据或执行终端操作 backend/open_webui/models/automations.py:64-68。
系统架构
自动化系统分为前端管理界面、FastAPI REST API 和随主应用运行的后台调度工作器三部分。
数据流:自动化执行
下图展示了从后台工作器到 AI 响应的定时任务生命周期。
自动化执行周期
sequenceDiagram
participant SW as ["scheduler_worker_loop"]
participant DB as ["SQLAlchemy_AutomationTable"]
participant EA as ["execute_automation"]
participant CC as ["chat_completion"]
participant AR as ["AutomationRuns_Table"]
loop "每 SCHEDULER_POLL_INTERVAL"
SW->>DB: "Automations.claim_due(now)"
DB-->>SW: "List[AutomationModel]"
loop "对每个自动化"
SW->>EA: "asyncio.create_task(execute_automation)"
EA->>CC: "生成 AI 响应"
CC-->>EA: "聊天响应对象"
EA->>AR: "AutomationRuns.insert(success/error)"
EA->>DB: "Automations.update_next_run(rrule)"
end
end
来源:backend/open_webui/utils/automations.py:162-199、backend/open_webui/utils/automations.py:233-270、backend/open_webui/models/automations.py:120-144
核心组件
1. 调度工作器
scheduler_worker_loop 是自动化引擎的核心 backend/open_webui/utils/automations.py:162-162。它以可配置的间隔运行(默认通过 SCHEDULER_POLL_INTERVAL 设为 10 秒)backend/open_webui/utils/automations.py:39-39。
- 领取任务:使用
Automations.claim_due原子性地获取并锁定next_run_at时间戳已过去的任务backend/open_webui/utils/automations.py:179-179。 - 并发性:每个自动化在非阻塞的
asyncio.task中执行,确保单个慢速 AI 响应不会阻塞整个调度backend/open_webui/utils/automations.py:183-183。 - 抖动:循环包含 0-2 秒的随机抖动,防止多个服务器实例运行时出现"惊群"问题
backend/open_webui/utils/automations.py:198-198。 - 统一逻辑:除自动化外,如果启用了
ENABLE_CALENDAR,工作器还处理日历事件提醒backend/open_webui/utils/automations.py:188-192。
2. RRULE 引擎
Open WebUI 使用 dateutil.rrule 库处理复杂的重复模式 backend/open_webui/utils/automations.py:26-26。
- 时区感知:系统根据用户配置文件解析时区,确保调度引用用户的本地时间
backend/open_webui/utils/automations.py:48-61。 - 时钟对齐:对于
MINUTELY和HOURLY频率,系统通过使用固定纪元DTSTART将执行时间对齐到时钟边界(例如,每 5 分钟执行一次的结果为 :00、:05、:10)backend/open_webui/utils/automations.py:64-77。 - 验证:
validate_rrule函数确保提供的字符串语法正确且尚未达到结束日期backend/open_webui/utils/automations.py:80-95。
3. 执行逻辑(execute_automation)
当自动化触发时,系统:
- 使用
_build_request构建一个模拟的Request对象,以满足内部chat_completion管道的需求backend/open_webui/utils/automations.py:206-224。 - 使用指定的
model_id调用提示词backend/open_webui/utils/automations.py:245-255。 - 在
automation_run表中创建新条目,存储结果和任何错误消息backend/open_webui/models/automations.py:38-46。 - 如果自动化数据中存在
terminal配置,则支持终端执行backend/open_webui/models/automations.py:59-68。
数据库模式
自动化系统使用两个主要表:automation 用于定义,automation_run 用于执行历史。
实体关系图
erDiagram
"Automation_Model" {
string id PK
string user_id
string name
json data "prompt, model_id, rrule"
boolean is_active
bigint next_run_at
bigint last_run_at
}
"AutomationRun_Model" {
string id PK
string automation_id FK
string chat_id
string status "success | error"
text error
bigint created_at
}
"Automation_Model" ||--o{ "AutomationRun_Model" : "tracks_history_in"
来源:backend/open_webui/models/automations.py:20-51、backend/open_webui/migrations/versions/d4e5f6a7b8c9_add_automation_tables.py:19-50
配置与限制
管理员可以通过环境变量和全局配置控制自动化引擎。
| 变量 | 描述 | 默认值 |
|---|---|---|
ENABLE_AUTOMATIONS | 自动化功能的全局开关。 | False |
AUTOMATION_MAX_COUNT | 非管理员用户可创建的最大自动化数量。 | None |
AUTOMATION_MIN_INTERVAL | 运行之间的最小允许秒数(防止滥用)。 | None |
SCHEDULER_POLL_INTERVAL | 后台工作器检查到期任务的频率(秒)。 | 10 |
来源:backend/open_webui/utils/automations.py:39-40、backend/open_webui/routers/automations.py:69-96、backend/open_webui/routers/automations.py:41-54
权限强制
check_automations_permission 辅助函数确保只有管理员或具有 features.automations 权限的用户才能访问端点 backend/open_webui/routers/automations.py:41-54。此外,check_automation_limits 在创建和更新期间强制实施 AUTOMATION_MAX_COUNT 和 AUTOMATION_MIN_INTERVAL 约束 backend/open_webui/routers/automations.py:69-96。
前端实现
前端提供管理仪表板(/automations)和详细编辑器。
- 自动化列表:显示所有已配置的任务及其状态(活动/暂停)、通过
formatRRule格式化的调度摘要以及上次运行状态src/routes/(app)/automations/+page.svelte:121-157。 - 自动化编辑器:允许用户修改提示词、通过
ModelDropdown选择模型,以及使用ScheduleDropdown组件配置调度src/lib/components/automations/AutomationEditor.svelte:29-30。 - 运行历史:使用
getAutomationRuns获取并显示特定自动化的分页历史执行记录src/lib/apis/automations/index.ts:266-300。 - 手动触发:用户可以使用
runAutomationById立即触发自动化,绕过调度器src/lib/apis/automations/index.ts:210-236。
来源:src/routes/(app)/automations/+page.svelte:65-86、src/lib/components/automations/AutomationEditor.svelte:188-199、src/lib/components/AutomationModal.svelte:102-161