agentic_huge_data_base / wiki
页面 Open WebUI · 20.1 定时自动化·DeepWiki 中文全文译文

20.1 · 定时自动化(Scheduled Automations)

多模型对话工作台与知识应用入口 · 本章是 Open WebUI DeepWiki 中文译文的独立章节页,保留原始链接、源码锚点、模块标签和章节层级。

项目Open WebUI 章节20.1 状态全文译文 模块界面与交互、接口与服务契约、系统架构、频道、笔记与协作
源码线索
  • backend/open_webui/migrations/versions/d4e5f6a7b8c9_add_automation_tables.py
  • backend/open_webui/models/automations.py
  • backend/open_webui/routers/automations.py
  • backend/open_webui/utils/automations.py
  • src/lib/apis/automations/index.ts
  • src/lib/components/AutomationModal.svelte
  • src/lib/components/automations/AutomationEditor.svelte
  • src/lib/components/automations/AutomationMenu.svelte
  • src/lib/components/layout/Sidebar/UserMenu.svelte
  • src/lib/components/workspace/Models/TerminalSelector.svelte
模块标签
  • 界面与交互
  • 接口与服务契约
  • 系统架构
  • 频道、笔记与协作
  • 测试、发布与运维

中文译文

定时自动化(中文译文)

原始 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.py
  • backend/open_webui/models/automations.py
  • backend/open_webui/routers/automations.py
  • backend/open_webui/utils/automations.py
  • src/lib/apis/automations/index.ts
  • src/lib/components/AutomationModal.svelte
  • src/lib/components/automations/AutomationEditor.svelte
  • src/lib/components/automations/AutomationMenu.svelte
  • src/lib/components/layout/Sidebar/UserMenu.svelte
  • src/lib/components/workspace/Models/TerminalSelector.svelte
  • src/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-199backend/open_webui/utils/automations.py:233-270backend/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
  • 时钟对齐:对于 MINUTELYHOURLY 频率,系统通过使用固定纪元 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

当自动化触发时,系统:

  1. 使用 _build_request 构建一个模拟的 Request 对象,以满足内部 chat_completion 管道的需求 backend/open_webui/utils/automations.py:206-224
  2. 使用指定的 model_id 调用提示词 backend/open_webui/utils/automations.py:245-255
  3. automation_run 表中创建新条目,存储结果和任何错误消息 backend/open_webui/models/automations.py:38-46
  4. 如果自动化数据中存在 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-51backend/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-40backend/open_webui/routers/automations.py:69-96backend/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_COUNTAUTOMATION_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-86src/lib/components/automations/AutomationEditor.svelte:188-199src/lib/components/AutomationModal.svelte:102-161