Scheduled Handler
When a Worker is invoked via a Cron Trigger, the scheduled() handler handles the invocation.
export default { async scheduled(controller, env, ctx) { ctx.waitUntil(doSomeTaskOnASchedule()); },};interface Env {}export default { async scheduled( controller: ScheduledController, env: Env, ctx: ExecutionContext, ) { ctx.waitUntil(doSomeTaskOnASchedule()); },};from workers import handler
@handlerasync def on_scheduled(controller, env, ctx): ctx.waitUntil(doSomeTaskOnASchedule())-
controller.cronstring- The value of the Cron Trigger that started the
ScheduledEvent.
- The value of the Cron Trigger that started the
-
controller.typestring- The type of controller. This will always return
"scheduled".
- The type of controller. This will always return
-
controller.scheduledTimenumber- The time the
ScheduledEventwas scheduled to be executed in milliseconds since January 1, 1970, UTC. It can be parsed asnew Date(controller.scheduledTime).
- The time the
-
envobject- An object containing the bindings associated with your Worker using ES modules format, such as KV namespaces and Durable Objects.
-
ctxobject- An object containing the context associated with your Worker using ES modules format. Currently, this object just contains the
waitUntilfunction.
- An object containing the context associated with your Worker using ES modules format. Currently, this object just contains the
When a Workers script is invoked by a Cron Trigger, the Workers runtime starts a ScheduledEvent which will be handled by the scheduled function in your Workers Module class. The ctx argument represents the context your function runs in, and contains the following methods to control what happens next:
-
ctx.waitUntil(promisePromise): void- Use this method to notify the runtime to wait for asynchronous tasks (for example, logging, analytics to third-party services, streaming and caching). The first
ctx.waitUntilto fail will be observed and recorded as the status in the Cron Trigger Past Events table. Otherwise, it will be reported as a success.
- Use this method to notify the runtime to wait for asynchronous tasks (for example, logging, analytics to third-party services, streaming and caching). The first
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark