ข้ามไปที่เนื้อหา

Task Scheduler

Task scheduler ให้คุณรัน query ของเอเจนต์อัตโนมัติตามตารางเวลา -- ครั้งเดียวใน อนาคต, ตามช่วงเวลาที่เกิดซ้ำ หรือผ่านนิพจน์ cron งานที่ตั้งเวลาไว้จะถูกบันทึกใน SQLite ทำให้รอดจากการ restart ของ process และการรันถูกจัดการโดย background daemon thread ที่ poll หางานที่ถึงกำหนดทุก 60 วินาที

ส่วนประกอบเสริม

Scheduler เป็นโมดูลแยกต่างหาก (openjarvis.scheduler) มันไม่ได้ต่อสายเข้ากับการเริ่มต้นของ Jarvis / JarvisSystem เริ่มต้น คุณต้องเปิดใช้งานเองผ่าน SystemBuilder หรือเริ่ม daemon ของ CLI ด้วย jarvis scheduler start


ประเภทตารางเวลา

schedule_type รูปแบบ schedule_value ตัวอย่าง ความหมาย
once ISO 8601 UTC datetime "2026-03-01T09:00:00Z" รันครั้งเดียวที่ timestamp นั้น
interval วินาทีในรูปสตริง "3600" รันทุกชั่วโมง เริ่มทันที
cron นิพจน์ cron มาตรฐาน 5 ฟิลด์ "0 9 * * 1-5" 09:00 UTC, จันทร์–ศุกร์

การรองรับ Cron

การรองรับนิพจน์ cron แบบเต็มต้องมี croniter (uv pip install croniter) หากไม่มี scheduler จะใช้ตัว parser ในตัวแบบขั้นต่ำ ที่รองรับเฉพาะรูปแบบ minute hour * * * แบบง่ายเท่านั้น


คำสั่ง CLI

กลุ่มคำสั่งย่อย jarvis scheduler จัดการงานและ daemon จากเทอร์มินัล

เริ่ม Daemon

jarvis scheduler start

เริ่ม background polling daemon daemon จะรันแบบ foreground จนกว่าจะถูกขัดจังหวะ (Ctrl+C) ในโปรดักชัน ให้รันภายใต้ systemd หรือ launchd (ดู Deployment)

สร้างงาน

# Run once at a specific time
jarvis scheduler create \
  --prompt "Generate the weekly summary report" \
  --type once \
  --value "2026-03-01T09:00:00Z"

# Run every hour
jarvis scheduler create \
  --prompt "Check for new emails and summarize" \
  --type interval \
  --value "3600" \
  --agent orchestrator \
  --tools retrieval,think

# Run on a cron schedule
jarvis scheduler create \
  --prompt "Summarize overnight logs" \
  --type cron \
  --value "0 8 * * 1-5"

ดูรายการงาน

# All tasks
jarvis scheduler list

# Active tasks only
jarvis scheduler list --status active

# Paused tasks
jarvis scheduler list --status paused

ตัวอย่างผลลัพธ์:

ID               AGENT     TYPE       VALUE          STATUS   NEXT RUN
a3f9b12c4d8e    simple    cron       0 8 * * 1-5    active   2026-02-26T08:00:00+00:00
b7c2e56f1a3d    orchestr  interval   3600           active   2026-02-25T14:05:00+00:00

หยุดชั่วคราวและดำเนินการต่อ

# Pause a running task
jarvis scheduler pause a3f9b12c4d8e

# Resume -- next_run is recomputed from the current time
jarvis scheduler resume a3f9b12c4d8e

ยกเลิกงาน

# Permanently cancel (status -> "cancelled", next_run cleared)
jarvis scheduler cancel a3f9b12c4d8e

ดู Log การรัน

# Last 10 executions for a task
jarvis scheduler logs a3f9b12c4d8e

ตัวอย่างผลลัพธ์:

Run 1: started=2026-02-25T08:00:01Z finished=2026-02-25T08:00:04Z success=True
  Result: Overnight logs contain 3 warnings and no errors.
Run 2: started=2026-02-24T08:00:00Z finished=2026-02-24T08:00:05Z success=True
  Result: Logs are clean.

Python API

scheduler_example.py
from openjarvis.scheduler.store import SchedulerStore
from openjarvis.scheduler.scheduler import TaskScheduler

# Set up storage
store = SchedulerStore(db_path="~/.openjarvis/scheduler.db")  # (1)!

# Wire in a JarvisSystem for task execution
from openjarvis import Jarvis
jarvis = Jarvis()

scheduler = TaskScheduler(
    store=store,
    system=jarvis,         # (2)!
    poll_interval=60,      # (3)!
)

# Create tasks
daily_summary = scheduler.create_task(
    prompt="Summarize the latest news headlines",
    schedule_type="cron",
    schedule_value="0 8 * * *",
    agent="simple",
)
print(f"Created task {daily_summary.id}, next run: {daily_summary.next_run}")

# List active tasks
for task in scheduler.list_tasks(status="active"):
    print(f"  {task.id}: {task.prompt} @ {task.next_run}")

# Manage task state
scheduler.pause_task(daily_summary.id)
scheduler.resume_task(daily_summary.id)   # next_run recomputed from now
scheduler.cancel_task(daily_summary.id)  # permanent

# Start the background thread
scheduler.start()   # (4)!

# ... application runs ...

scheduler.stop()
jarvis.close()
  1. ฐานข้อมูล SQLite ที่เก็บ state ของงานทั้งหมดและ log การรัน
  2. Scheduler เรียก system.ask(task.prompt, agent=task.agent, tools=...) เมื่อถึงกำหนดงาน ส่ง system=None สำหรับโหมด dry-run ที่บันทึกว่าจะรันอะไรโดยไม่เรียกเอเจนต์จริง
  3. จำนวนวินาทีระหว่างรอบ polling ค่าที่ต่ำกว่าเพิ่มความไวในการตอบสนอง แลกกับการอ่าน SQLite ที่มากขึ้น
  4. เริ่ม daemon thread ชื่อ "jarvis-scheduler" daemon thread จะออกโดยอัตโนมัติเมื่อ process หลักออก

ฟิลด์ของ ScheduledTask

แต่ละงานแทนด้วย dataclass ScheduledTask

ฟิลด์ ชนิด ค่าเริ่มต้น คำอธิบาย
id str auto (16 hex) Identifier ของงานแบบไม่ซ้ำกัน
prompt str -- คำถามที่ส่งให้เอเจนต์ตอนรัน
schedule_type str -- "cron", "interval" หรือ "once"
schedule_value str -- นิพจน์ cron, วินาทีของ interval หรือ ISO datetime
context_mode str "isolated" โหมด context การรัน
status str "active" "active", "paused", "completed", "cancelled"
next_run str หรือ None คำนวณอัตโนมัติ ISO 8601 UTC datetime ของการรันครั้งถัดไป
last_run str หรือ None None ISO 8601 UTC datetime ของการรันครั้งล่าสุด
agent str "simple" คีย์ agent registry ที่ใช้ตอนรัน
tools str "" ชื่อ tool คั่นด้วยคอมมาสำหรับเอเจนต์
metadata dict {} Metadata ใดๆ สำหรับงาน

ใช้ Scheduler Tools ร่วมกับเอเจนต์

Scheduler MCP tool ทั้งห้าตัว (schedule_task, list_scheduled_tasks, pause_scheduled_task, resume_scheduled_task, cancel_scheduled_task) ส่งให้ ToolUsingAgent ใดๆ ได้ ทำให้เอเจนต์ตั้งเวลางานติดตามผลได้เองโดยอิสระ

# Let the orchestrator schedule its own follow-up
jarvis ask --agent orchestrator \
  --tools schedule_task,list_scheduled_tasks \
  "Research transformer architectures and schedule a daily summary at 8am"
from openjarvis import Jarvis

j = Jarvis()
response = j.ask(
    "Schedule a weekly digest of research papers every Monday at 9am",
    agent="orchestrator",
    tools=["schedule_task"],
)
print(response)

ดู Scheduler Tools สำหรับเอกสารอ้างอิงพารามิเตอร์แบบเต็ม


Configuration

การตั้งค่า Scheduler อยู่ในหมวด [scheduler] ของ ~/.openjarvis/config.toml

~/.openjarvis/config.toml
[scheduler]
enabled = false
db_path = "~/.openjarvis/scheduler.db"
poll_interval = 60
default_agent = "simple"
คีย์ ชนิด ค่าเริ่มต้น คำอธิบาย
enabled bool false เริ่ม scheduler daemon โดยอัตโนมัติ
db_path str ~/.openjarvis/scheduler.db Path ของฐานข้อมูล SQLite
poll_interval int 60 จำนวนวินาทีระหว่างรอบ polling
default_agent str "simple" เอเจนต์เริ่มต้นสำหรับงานที่ไม่ระบุ agent

ดูเพิ่มเติม