Skip to content

Commit c04791c

Browse files
fix: add support for cron triggers in dev --local mode (#738)
Currently, there is not support for doing this in "remote" dev mode. Resolves #737
1 parent dc1f511 commit c04791c

File tree

7 files changed

+38
-0
lines changed

7 files changed

+38
-0
lines changed

.changeset/tricky-crabs-search.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"example-worker-app": patch
3+
"wrangler": patch
4+
---
5+
6+
fix: add support for cron triggers in `dev --local` mode
7+
8+
Currently, I don't know if there is support for doing this in "remote" dev mode.
9+
10+
Resolves #737

packages/example-worker-app/src/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ export default {
1010

1111
return new Response(`${request.url} ${now()}`);
1212
},
13+
14+
/**
15+
* Handle a scheduled event.
16+
*
17+
* If developing using `--local` mode, you can trigger this scheduled event via a CURL.
18+
* E.g. `curl "http://localhost:8787/cdn-cgi/mf/scheduled"`.
19+
* See the Miniflare docs: https://miniflare.dev/core/scheduled.
20+
*/
21+
scheduled(event, env, ctx) {
22+
ctx.waitUntil(Promise.resolve(event.scheduledTime));
23+
ctx.waitUntil(Promise.resolve(event.cron));
24+
},
1325
};
1426

1527
// addEventListener("fetch", (event) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name = "example-worker-app"
2+
compatibility_date = "2022-03-31"
3+
4+
main = "src/index.js"
5+
6+
[triggers]
7+
crons = ["1 * * * *"]

packages/wrangler/src/dev/dev.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export type DevProps = {
3737
localProtocol: "https" | "http";
3838
enableLocalPersistence: boolean;
3939
bindings: CfWorkerInit["bindings"];
40+
crons: Config["triggers"]["crons"];
4041
public: undefined | string;
4142
assetPaths: undefined | AssetPaths;
4243
compatibilityDate: undefined | string;
@@ -163,6 +164,7 @@ function DevSession(props: DevSessionProps) {
163164
rules={props.rules}
164165
inspectorPort={props.inspectorPort}
165166
enableLocalPersistence={props.enableLocalPersistence}
167+
crons={props.crons}
166168
/>
167169
) : (
168170
<Remote

packages/wrangler/src/dev/local.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface LocalProps {
2727
rules: Config["rules"];
2828
inspectorPort: number;
2929
enableLocalPersistence: boolean;
30+
crons: Config["triggers"]["crons"];
3031
}
3132

3233
export function Local(props: LocalProps) {
@@ -52,6 +53,7 @@ function useLocalWorker({
5253
rules,
5354
enableLocalPersistence,
5455
ip,
56+
crons,
5557
}: LocalProps) {
5658
// TODO: pass vars via command line
5759
const local = useRef<ReturnType<typeof spawn>>();
@@ -150,6 +152,7 @@ function useLocalWorker({
150152
dataBlobBindings,
151153
sourceMap: true,
152154
logUnhandledRejections: true,
155+
crons,
153156
};
154157

155158
// The path to the Miniflare CLI assumes that this file is being run from
@@ -247,6 +250,7 @@ function useLocalWorker({
247250
bindings.wasm_modules,
248251
bindings.text_blobs,
249252
bindings.data_blobs,
253+
crons,
250254
]);
251255
return { inspectorUrl };
252256
}

packages/wrangler/src/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,7 @@ export async function main(argv: string[]): Promise<void> {
927927
r2_buckets: config.r2_buckets,
928928
unsafe: config.unsafe?.bindings,
929929
}}
930+
crons={config.triggers.crons}
930931
/>
931932
);
932933
await waitUntilExit();
@@ -1310,6 +1311,7 @@ export async function main(argv: string[]): Promise<void> {
13101311
r2_buckets: config.r2_buckets,
13111312
unsafe: config.unsafe?.bindings,
13121313
}}
1314+
crons={config.triggers.crons}
13131315
inspectorPort={await getPort({ port: 9229 })}
13141316
/>
13151317
);

packages/wrangler/src/miniflare-cli/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ async function main() {
2626
try {
2727
// Start Miniflare development server
2828
await mf.startServer();
29+
await mf.startScheduler();
2930
} catch (e) {
3031
mf.log.error(e as Error);
3132
process.exitCode = 1;

0 commit comments

Comments
 (0)