mastra.schedules
新增於: @mastra/core@1.50.0
mastra.schedules 是用於持久保存 cron 排程的 CRUD 服務。你可以用它為 Agent 或 Workflow 建立、列出、更新、暫停、恢復、手動執行及刪除排程。
有關使用模式和概念,請參閱排程。
使用範例使用範例 的直接連結
建立 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' },
})
排程需要實作 schedules domain 的儲存適配器。支援的適配器包括 @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() 執行。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 的直接連結
列出排程。如未指定篩選條件,會傳回 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 執行 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 執行週期。