mastra.schedules
新增於: @mastra/core@1.50.0
mastra.schedules 是持久化 cron 排程的 CRUD 服務。可用來建立、列出、更新、暫停、繼續、手動執行及刪除 Agent 或 Workflow 的排程。
使用模式與概念請參閱 Schedules。
使用範例「使用範例」的直接連結
建立 Agent 排程:
const schedule = await mastra.schedules.create({
agentId: 'pinger',
cron: '0 * * * *',
prompt: 'Give me a status update.',
})
建立 Workflow 排程:
const schedule = await mastra.schedules.create({
workflowId: 'daily-report',
cron: '0 9 * * *',
inputData: { reportType: 'summary' },
})
Schedule 需要實作 schedules domain 的 storage adapter。支援的 adapter 包括 @mastra/libsql、@mastra/pg、@mastra/mysql、@mastra/mongodb、@mastra/convex 與 @mastra/spanner。
方法「方法」的直接連結
建立排程「建立排程」的直接連結
create(input)「createinput」的直接連結
建立 Agent 或 Workflow 排程。傳入 agentId 可建立 Agent 排程;傳入 workflowId 則建立 Workflow 排程。
const schedule = await mastra.schedules.create({
agentId: 'pinger',
cron: '0 * * * *',
prompt: 'Give me a status update.',
})
Agent 排程輸入「Agent 排程輸入」的直接連結
id?:
agent_<slug>。省略時,Mastra 會產生 agent_<uuid> ID。agentId:
cron:
prompt:
name?:
timezone?:
America/New_York。threadId?:
agent.generate() 執行,不使用 thread。resourceId?:
threadId 時必須提供。signalType?:
notification。tagName?:
schedule。attributes?:
providerOptions?:
ifActive?:
threadId。ifIdle?:
threadId。metadata?:
status?:
active。Workflow 排程輸入「Workflow 排程輸入」的直接連結
id?:
schedule_<slug>。省略時,Mastra 會產生 schedule_<uuid> ID。workflowId:
cron:
timezone?:
inputData?:
initialState?:
requestContext?:
metadata?:
status?:
active。讀取排程「讀取排程」的直接連結
get(id)「getid」的直接連結
依 ID 取得排程。不含前綴的 Agent 排程 ID 也會解析為正規化的 agent_<slug> 形式。
const schedule = await mastra.schedules.get('pinger')
list(filter?)「listfilter」的直接連結
列出排程。未提供 filter 時,會傳回 Agent 與 Workflow 排程。
const schedules = await mastra.schedules.list({
agentId: 'pinger',
status: 'active',
})
filter?:
agentId?:
workflowId?:
threadId?:
resourceId?:
name?:
status?:
更新排程「更新排程」的直接連結
update(id, patch)「updateid-patch」的直接連結
更新排程。變更 cron 或 timezone 會重新計算下次觸發時間。將 status 從 paused 更新為 active 也會重新計算下次觸發時間。
const updated = await mastra.schedules.update('pinger', {
cron: '*/30 * * * *',
prompt: 'Give me a status update every 30 minutes.',
})
Agent 排程 patch 可更新 cron、timezone、prompt、name、signalType、tagName、attributes、providerOptions、ifActive、ifIdle、metadata 與 status。threadId 與 resourceId 無法透過 patch 更新。若需變更目標 thread,請建立新排程。
Workflow 排程 patch 可更新 cron、timezone、inputData、initialState、requestContext、metadata 與 status。在 Workflow 排程中使用 prompt、signalType 與 ifIdle 等 Agent 專用 patch 欄位會擲回錯誤。
生命週期「生命週期」的直接連結
pause(id)「pauseid」的直接連結
暫停排程。暫停操作具有持久性與等冪性。
const paused = await mastra.schedules.pause('pinger')
resume(id)「resumeid」的直接連結
繼續已暫停的排程,並從目前時間重新計算下次觸發時間。
const active = await mastra.schedules.resume('pinger')
run(id)「runid」的直接連結
立即觸發排程一次,不變更其 cron 執行週期。
const run = await mastra.schedules.run('pinger')
Agent 排程的 claimId 使用 manual_<scheduleId>_<timestamp>。Workflow 排程的 claimId 使用 sched_<scheduleId>_<timestamp>,並會重複用作 Workflow run ID。
delete(id)「deleteid」的直接連結
刪除排程。刪除不存在的排程不會執行任何操作。
await mastra.schedules.delete('pinger')
排程行為「排程行為」的直接連結
- Agent 排程 ID 使用
agent_前綴。透過mastra.schedules.create()建立的 Workflow 排程 ID 使用schedule_前綴。 - 設定
threadId時,使用 thread 的 Agent 排程必須提供resourceId。 signalType、ifActive、ifIdle與resourceId必須搭配threadId。- Workflow 排程不接受
prompt、signalType或ifIdle等 Agent 專用 patch 欄位。 run()會立即發布一次手動觸發,不會變更已儲存的 cron 執行週期。