Skip to content

Commit a90aade

Browse files
dom96ToriLindsay
andauthored
Workers: add cron trigger changelog and docs for Python Workers. (#21875)
* Workers: add cron trigger changelog and docs for Python Workers. * Fix capitalisation Co-authored-by: ToriLindsay <[email protected]> --------- Co-authored-by: ToriLindsay <[email protected]>
1 parent 2841cbe commit a90aade

File tree

8 files changed

+165
-35
lines changed

8 files changed

+165
-35
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: Cron triggers are now supported in Python Workers
3+
description: You can now set up scheduled handlers in your Python Workers
4+
products:
5+
- workers
6+
date: 2025-04-24T12:00:00Z
7+
---
8+
9+
import { WranglerConfig } from "~/components";
10+
11+
You can now create Python Workers which are executed via a cron trigger.
12+
13+
This is similar to how it's done in JavaScript Workers, simply define a scheduled event
14+
listener in your Worker:
15+
16+
```python
17+
from workers import handler
18+
19+
@handler
20+
async def on_scheduled(event, env, ctx):
21+
print("cron processed")
22+
```
23+
24+
Define a cron trigger configuration in your Wrangler configuration file:
25+
26+
<WranglerConfig>
27+
28+
```toml
29+
[triggers]
30+
# Schedule cron triggers:
31+
# - At every 3rd minute
32+
# - At 15:00 (UTC) on first day of the month
33+
# - At 23:59 (UTC) on the last weekday of the month
34+
crons = [ "*/3 * * * *", "0 15 1 * *", "59 23 LW * *" ]
35+
```
36+
37+
</WranglerConfig>
38+
39+
Then test your new handler by using Wrangler with the `--test-scheduled` flag and
40+
making a request to `/cdn-cgi/handler/scheduled?cron=*+*+*+*+*`:
41+
42+
```sh
43+
npx wrangler dev --test-scheduled
44+
45+
curl "http://localhost:8787/cdn-cgi/handler/scheduled?cron=*+*+*+*+*"
46+
```
47+
48+
Consult the [Workers Cron Triggers page](/workers/configuration/cron-triggers/) for full details on cron triggers in Workers.

src/content/docs/workers/configuration/cron-triggers.mdx

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ head: []
55
description: Enable your Worker to be executed on a schedule.
66
---
77

8-
import { Render, WranglerConfig } from "~/components";
8+
import { Render, WranglerConfig, TabItem, Tabs } from "~/components";
99

1010
## Background
1111

@@ -27,7 +27,42 @@ Cron Triggers execute on UTC time.
2727

2828
To respond to a Cron Trigger, you must add a [`"scheduled"` handler](/workers/runtime-apis/handlers/scheduled/) to your Worker.
2929

30-
<Render file="cron-trigger-example" />
30+
<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
31+
32+
```js
33+
export default {
34+
async scheduled(controller, env, ctx) {
35+
console.log("cron processed");
36+
},
37+
};
38+
```
39+
40+
</TabItem> <TabItem label="TypeScript" icon="seti:typescript">
41+
42+
```ts
43+
interface Env {}
44+
export default {
45+
async scheduled(
46+
controller: ScheduledController,
47+
env: Env,
48+
ctx: ExecutionContext,
49+
) {
50+
console.log("cron processed");
51+
},
52+
};
53+
```
54+
55+
</TabItem> <TabItem label="Python" icon="seti:python">
56+
57+
```python
58+
from workers import handler
59+
60+
@handler
61+
async def on_scheduled(controller, env, ctx):
62+
print("cron processed")
63+
```
64+
65+
</TabItem></Tabs>
3166

3267
Refer to the following additional examples to write your code:
3368

@@ -140,12 +175,14 @@ Changes such as adding a new Cron Trigger, updating an old Cron Trigger, or dele
140175

141176
:::
142177

143-
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.
178+
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.
144179

145180
```sh
146181
npx wrangler dev --test-scheduled
147182

148183
curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*"
184+
185+
curl "http://localhost:8787/cdn-cgi/handler/scheduled?cron=*+*+*+*+*" # Python Workers
149186
```
150187

151188
## View past events

src/content/docs/workers/examples/cron-trigger.mdx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ description: Set a Cron Trigger for your Worker.
1515

1616
import { Render, TabItem, Tabs, WranglerConfig } from "~/components";
1717

18-
<Render file="cron-trigger-example" />
18+
<Tabs syncKey="workersExamples"> <TabItem label="JavaScript" icon="seti:javascript">
1919

20-
<Tabs syncKey="workersExamples"> <TabItem label="TypeScript (Standard)" icon="seti:typescript">
20+
```js
21+
export default {
22+
async scheduled(controller, env, ctx) {
23+
console.log("cron processed");
24+
},
25+
};
26+
```
27+
28+
</TabItem> <TabItem label="TypeScript" icon="seti:typescript">
2129

2230
```ts
2331
interface Env {}
@@ -32,6 +40,16 @@ export default {
3240
};
3341
```
3442

43+
</TabItem> <TabItem label="Python" icon="seti:python">
44+
45+
```python
46+
from workers import handler
47+
48+
@handler
49+
async def on_scheduled(controller, env, ctx):
50+
print("cron processed")
51+
```
52+
3553
</TabItem> <TabItem label="Hono" icon="seti:typescript">
3654

3755
```ts
@@ -103,10 +121,12 @@ crons = ["0 * * * *"]
103121

104122
The recommended way of testing Cron Triggers is using Wrangler.
105123

106-
Cron Triggers can be tested 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.
124+
Cron Triggers can be tested 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.
107125

108126
```sh
109127
npx wrangler dev --test-scheduled
110128

111129
curl "http://localhost:8787/__scheduled?cron=0+*+*+*+*"
130+
131+
curl "http://localhost:8787/cdn-cgi/handler/scheduled?cron=*+*+*+*+*" # Python Workers
112132
```

src/content/docs/workers/examples/multiple-cron-triggers.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,12 @@ export default {
120120

121121
The recommended way of testing Cron Triggers is using Wrangler.
122122

123-
Cron Triggers can be tested 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.
123+
Cron Triggers can be tested 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.
124124

125125
```sh
126126
npx wrangler dev --test-scheduled
127127

128128
curl "http://localhost:8787/__scheduled?cron=*%2F3+*+*+*+*"
129+
130+
curl "http://localhost:8787/cdn-cgi/handler/scheduled?cron=*+*+*+*+*" # Python Workers
129131
```

src/content/docs/workers/runtime-apis/fetch.mdx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Worker-to-Worker `fetch` requests are possible with [Service bindings](/workers/
3333

3434
```js null {3-7}
3535
export default {
36-
async scheduled(event, env, ctx) {
36+
async scheduled(controller, env, ctx) {
3737
return await fetch("https://example.com", {
3838
headers: {
3939
"X-Source": "Cloudflare-Workers",
@@ -58,7 +58,17 @@ async function eventHandler(event) {
5858
}
5959
```
6060

61-
</TabItem> </Tabs>
61+
</TabItem> <TabItem label="Python Worker" icon="seti:python">
62+
63+
```python
64+
from workers import fetch, handler
65+
66+
@handler
67+
async def on_scheduled(controller, env, ctx):
68+
return await fetch("https://example.com", headers={"X-Source": "Cloudflare-Workers"})
69+
```
70+
71+
</TabItem></Tabs>
6272

6373

6474

src/content/docs/workers/runtime-apis/handlers/scheduled.mdx

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ pcx_content_type: configuration
33
title: Scheduled Handler
44
---
55

6+
import { TabItem, Tabs } from "~/components";
7+
68
## Background
79

810
When a Worker is invoked via a [Cron Trigger](/workers/configuration/cron-triggers/), the `scheduled()` handler handles the invocation.
@@ -11,12 +13,14 @@ When a Worker is invoked via a [Cron Trigger](/workers/configuration/cron-trigge
1113

1214
You can test the behavior of your `scheduled()` handler in local development using Wrangler.
1315

14-
Cron Triggers can be tested 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.
16+
Cron Triggers can be tested 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.
1517

1618
```sh
1719
npx wrangler dev --test-scheduled
1820

1921
curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*"
22+
23+
curl "http://localhost:8787/cdn-cgi/handler/scheduled?cron=*+*+*+*+*" # Python Workers
2024
```
2125

2226
:::
@@ -25,25 +29,54 @@ curl "http://localhost:8787/__scheduled?cron=*+*+*+*+*"
2529

2630
## Syntax
2731

32+
<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
33+
2834
```js
2935
export default {
30-
async scheduled(event, env, ctx) {
36+
async scheduled(controller, env, ctx) {
3137
ctx.waitUntil(doSomeTaskOnASchedule());
3238
},
3339
};
3440
```
3541

42+
</TabItem> <TabItem label="TypeScript" icon="seti:typescript">
43+
44+
```ts
45+
interface Env {}
46+
export default {
47+
async scheduled(
48+
controller: ScheduledController,
49+
env: Env,
50+
ctx: ExecutionContext,
51+
) {
52+
ctx.waitUntil(doSomeTaskOnASchedule());
53+
},
54+
};
55+
```
56+
57+
</TabItem> <TabItem label="Python" icon="seti:python">
58+
59+
```python
60+
from workers import handler
61+
62+
@handler
63+
async def on_scheduled(controller, env, ctx):
64+
ctx.waitUntil(doSomeTaskOnASchedule())
65+
```
66+
67+
</TabItem></Tabs>
68+
3669
### Properties
3770

38-
- `event.cron` string
71+
- `controller.cron` string
3972

4073
- The value of the [Cron Trigger](/workers/configuration/cron-triggers/) that started the `ScheduledEvent`.
4174

42-
- `event.type` string
75+
- `controller.type` string
4376

4477
- The type of event. This will always return `"scheduled"`.
4578

46-
- `event.scheduledTime` number
79+
- `controller.scheduledTime` number
4780

4881
- The time the `ScheduledEvent` was scheduled to be executed in milliseconds since January 1, 1970, UTC. It can be parsed as <code>new Date(event.scheduledTime)</code>.
4982

src/content/docs/workers/wrangler/commands.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ As of Wrangler v3.2.0, `wrangler dev` is supported by any Linux distributions pr
268268
- `--remote` <Type text="boolean" /> <MetaInfo text="(default: false) optional" />
269269
- Develop against remote resources and data stored on Cloudflare's network.
270270
- `--test-scheduled` <Type text="boolean" /> <MetaInfo text="(default: false) optional" />
271-
- Exposes a `/__scheduled` fetch route which will trigger a scheduled event (Cron Trigger) for testing during development. To simulate different cron patterns, a `cron` query parameter can be passed in: `/__scheduled?cron=*+*+*+*+*`.
271+
- Exposes a `/__scheduled` fetch route which will trigger a scheduled event (Cron Trigger) for testing during development. To simulate different cron patterns, a `cron` query parameter can be passed in: `/__scheduled?cron=*+*+*+*+*` or `/cdn-cgi/handler/scheduled?cron=*+*+*+*+*`.
272272
- `--log-level` <Type text="'debug'|'info'|'log'|'warn'|'error|'none'" /> <MetaInfo text="(default: log) optional" />
273273
- Specify Wrangler's logging level.
274274
- `--show-interactive-dev-session` <Type text="boolean" /> <MetaInfo text="(default: true if the terminal supports interactivity) optional" />

src/content/partials/workers/cron-trigger-example.mdx

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)