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 的 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 运行 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 周期。