mastra.schedules
Added in: @mastra/core@1.50.0
mastra.schedules is the CRUD service for persisted cron schedules. Use it to create, list, update, pause, resume, manually run, and delete schedules for agents or workflows.
For usage patterns and concepts, see Schedules.
Usage exampleUsage example的直接链接
Create an agent schedule:
const schedule = await mastra.schedules.create({
agentId: 'pinger',
cron: '0 * * * *',
prompt: 'Give me a status update.',
})
Create a workflow schedule:
const schedule = await mastra.schedules.create({
workflowId: 'daily-report',
cron: '0 9 * * *',
inputData: { reportType: 'summary' },
})
Schedules require a storage adapter that implements the schedules domain. Supported adapters include @mastra/libsql, @mastra/pg, @mastra/mysql, @mastra/mongodb, @mastra/convex, and @mastra/spanner.
MethodsMethods的直接链接
Create schedulesCreate schedules的直接链接
create(input)createinput的直接链接
Creates an agent or workflow schedule. Pass agentId to create an agent schedule, or workflowId to create a workflow schedule.
const schedule = await mastra.schedules.create({
agentId: 'pinger',
cron: '0 * * * *',
prompt: 'Give me a status update.',
})
Agent schedule inputAgent schedule input的直接链接
id?:
agent_<slug>. When omitted, Mastra generates an agent_<uuid> ID.agentId:
cron:
prompt:
name?:
timezone?:
America/New_York.threadId?:
agent.generate().resourceId?:
threadId is set.signalType?:
notification.tagName?:
schedule.attributes?:
providerOptions?:
ifActive?:
threadId.ifIdle?:
threadId.metadata?:
status?:
active.Workflow schedule inputWorkflow schedule input的直接链接
id?:
schedule_<slug>. When omitted, Mastra generates a schedule_<uuid> ID.workflowId:
cron:
timezone?:
inputData?:
initialState?:
requestContext?:
metadata?:
status?:
active.Read schedulesRead schedules的直接链接
get(id)getid的直接链接
Gets a schedule by ID. Bare agent schedule IDs are also resolved to the normalized agent_<slug> form.
const schedule = await mastra.schedules.get('pinger')
list(filter?)listfilter的直接链接
Lists schedules. Without a filter, it returns agent and workflow schedules.
const schedules = await mastra.schedules.list({
agentId: 'pinger',
status: 'active',
})
filter?:
agentId?:
workflowId?:
threadId?:
resourceId?:
name?:
status?:
Update schedulesUpdate schedules的直接链接
update(id, patch)updateid-patch的直接链接
Updates a schedule. Changing cron or timezone recomputes the next fire time. Updating status from paused to active also recomputes the next fire time. A schedule becomes completed once its cron has no future occurrence. A cadence edit that leaves no future occurrence completes the row even when it was paused. Editing the cadence of a completed schedule reactivates it when the new cadence can still fire, and leaves it completed when it can't.
const updated = await mastra.schedules.update('pinger', {
cron: '*/30 * * * *',
prompt: 'Give me a status update every 30 minutes.',
})
Agent schedule patches can update cron, timezone, prompt, name, signalType, tagName, attributes, providerOptions, ifActive, ifIdle, metadata, and status. threadId and resourceId aren't patchable. Create a new schedule when the thread target needs to change.
Workflow schedule patches can update cron, timezone, inputData, initialState, requestContext, metadata, and status. Agent-only patch fields such as prompt, signalType, and ifIdle throw on workflow schedules.
LifecycleLifecycle的直接链接
pause(id)pauseid的直接链接
Pauses a schedule. Pausing is durable and idempotent.
const paused = await mastra.schedules.pause('pinger')
resume(id)resumeid的直接链接
Resumes a paused schedule and recomputes the next fire time from the current time. A paused schedule whose cron has no future occurrence is completed instead of resumed.
const active = await mastra.schedules.resume('pinger')
run(id)runid的直接链接
Fires a schedule once immediately without changing its cron cadence.
const run = await mastra.schedules.run('pinger')
For agent schedules, claimId uses manual_<scheduleId>_<timestamp>. For workflow schedules, claimId uses sched_<scheduleId>_<timestamp> and is reused as the workflow run ID.
delete(id)deleteid的直接链接
Deletes a schedule. Deleting a missing schedule is a no-op.
await mastra.schedules.delete('pinger')
Schedule behaviorSchedule behavior的直接链接
- Agent schedule IDs use the
agent_prefix. Workflow schedule IDs created throughmastra.schedules.create()use theschedule_prefix. - Threaded agent schedules require
resourceIdwhenthreadIdis set. signalType,ifActive,ifIdle, andresourceIdrequirethreadId.- Workflow schedules don't accept agent-only patch fields such as
prompt,signalType, orifIdle. run()publishes a manual fire immediately and doesn't change the stored cron cadence.