-
Notifications
You must be signed in to change notification settings - Fork 10k
Workers: add cron trigger changelog and docs for Python Workers. #21875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| --- | ||
| title: Cron triggers are now supported in Python Workers | ||
| description: You can now set up scheduled handlers in your Python Workers | ||
| products: | ||
| - workers | ||
| date: 2025-04-24T12:00:00Z | ||
| --- | ||
|
|
||
| import { WranglerConfig } from "~/components"; | ||
|
|
||
| You can now create Python Workers which are executed via a cron trigger. | ||
|
|
||
| This is similar to how it's done in JavaScript workers, simply define a scheduled event | ||
dom96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| listener in your Worker: | ||
|
|
||
| ```python | ||
| from workers import handler | ||
|
|
||
| @handler | ||
| async def on_scheduled(event, env, ctx): | ||
| print("cron processed") | ||
| ``` | ||
|
|
||
| Define a cron trigger configuration in your Wrangler configuration file: | ||
|
|
||
| <WranglerConfig> | ||
|
|
||
| ```toml | ||
| [triggers] | ||
| # Schedule cron triggers: | ||
| # - At every 3rd minute | ||
| # - At 15:00 (UTC) on first day of the month | ||
| # - At 23:59 (UTC) on the last weekday of the month | ||
| crons = [ "*/3 * * * *", "0 15 1 * *", "59 23 LW * *" ] | ||
| ``` | ||
|
|
||
| </WranglerConfig> | ||
|
|
||
| Then test your new handler by using Wrangler with the `--test-scheduled` flag and | ||
| making a request to `/cdn-cgi/handler/scheduled?cron=*+*+*+*+*`: | ||
|
|
||
| ```sh | ||
| npx wrangler dev --test-scheduled | ||
|
|
||
| curl "http://localhost:8787/cdn-cgi/handler/scheduled?cron=*+*+*+*+*" | ||
| ``` | ||
|
|
||
| Consult the [Workers Cron Triggers page](/workers/configuration/cron-triggers/) for full details on cron triggers in Workers. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ head: [] | |
| description: Enable your Worker to be executed on a schedule. | ||
| --- | ||
|
|
||
| import { Render, WranglerConfig } from "~/components"; | ||
| import { Render, WranglerConfig, TabItem, Tabs } from "~/components"; | ||
|
|
||
| ## Background | ||
|
|
||
|
|
@@ -27,7 +27,42 @@ Cron Triggers execute on UTC time. | |
|
|
||
| To respond to a Cron Trigger, you must add a [`"scheduled"` handler](/workers/runtime-apis/handlers/scheduled/) to your Worker. | ||
|
|
||
| <Render file="cron-trigger-example" /> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what the repercussions of this change are but I seem to recall that we intentionally wanted to move away from inlined examples in the docs to allow testing them properly in case they suddenly break. |
||
| <Tabs> <TabItem label="JavaScript" icon="seti:javascript"> | ||
|
|
||
| ```js | ||
| export default { | ||
| async scheduled(controller, env, ctx) { | ||
| console.log("cron processed"); | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| </TabItem> <TabItem label="TypeScript" icon="seti:typescript"> | ||
|
|
||
| ```ts | ||
| interface Env {} | ||
| export default { | ||
| async scheduled( | ||
| controller: ScheduledController, | ||
| env: Env, | ||
| ctx: ExecutionContext, | ||
| ) { | ||
| console.log("cron processed"); | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| </TabItem> <TabItem label="Python" icon="seti:python"> | ||
|
|
||
| ```python | ||
| from workers import handler | ||
|
|
||
| @handler | ||
| async def on_scheduled(controller, env, ctx): | ||
| print("cron processed") | ||
| ``` | ||
|
|
||
| </TabItem></Tabs> | ||
|
|
||
| Refer to the following additional examples to write your code: | ||
|
|
||
|
|
@@ -140,12 +175,14 @@ Changes such as adding a new Cron Trigger, updating an old Cron Trigger, or dele | |
|
|
||
| ::: | ||
|
|
||
| Test Cron Triggers using `Wrangler` by passing in the `--test-scheduled` flag to [`wrangler dev`](/workers/wrangler/commands/#dev). This will expose a `/__scheduled` route which can be used to test using a HTTP request. To simulate different cron patterns, a `cron` query parameter can be passed in. | ||
| Test Cron Triggers using `Wrangler` by passing in the `--test-scheduled` flag to [`wrangler dev`](/workers/wrangler/commands/#dev). This will expose a `/__scheduled` (or `/cdn-cgi/handler/scheduled` for Python Workers) route which can be used to test using a HTTP request. To simulate different cron patterns, a `cron` query parameter can be passed in. | ||
|
|
||
| ```sh | ||
| npx wrangler dev --test-scheduled | ||
|
|
||
| curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*" | ||
|
|
||
| curl "http://localhost:8787/cdn-cgi/handler/scheduled?cron=*+*+*+*+*" # Python Workers | ||
| ``` | ||
|
|
||
| ## View past events | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional - you can link here to remind people of the JS API