Skip to content
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

fix: add support for cron triggers in dev --local mode #738

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/tricky-crabs-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"example-worker-app": patch
"wrangler": patch
---

fix: add support for cron triggers in `dev --local` mode

Currently, I don't know if there is support for doing this in "remote" dev mode.

Resolves #737
12 changes: 12 additions & 0 deletions packages/example-worker-app/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ export default {

return new Response(`${request.url} ${now()}`);
},

/**
* Handle a scheduled event.
*
* If developing using `--local` mode, you can trigger this scheduled event via a CURL.
* E.g. `curl "http://localhost:8787/cdn-cgi/mf/scheduled"`.
* See the Miniflare docs: https://miniflare.dev/core/scheduled.
*/
scheduled(event, env, ctx) {
ctx.waitUntil(Promise.resolve(event.scheduledTime));
ctx.waitUntil(Promise.resolve(event.cron));
},
};

// addEventListener("fetch", (event) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/example-worker-app/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name = "example-worker-app"
compatibility_date = "2022-03-31"

main = "src/index.js"

[triggers]
crons = ["1 * * * *"]
2 changes: 2 additions & 0 deletions packages/wrangler/src/dev/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type DevProps = {
localProtocol: "https" | "http";
enableLocalPersistence: boolean;
bindings: CfWorkerInit["bindings"];
crons: Config["triggers"]["crons"];
public: undefined | string;
assetPaths: undefined | AssetPaths;
compatibilityDate: undefined | string;
Expand Down Expand Up @@ -163,6 +164,7 @@ function DevSession(props: DevSessionProps) {
rules={props.rules}
inspectorPort={props.inspectorPort}
enableLocalPersistence={props.enableLocalPersistence}
crons={props.crons}
/>
) : (
<Remote
Expand Down
4 changes: 4 additions & 0 deletions packages/wrangler/src/dev/local.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface LocalProps {
rules: Config["rules"];
inspectorPort: number;
enableLocalPersistence: boolean;
crons: Config["triggers"]["crons"];
}

export function Local(props: LocalProps) {
Expand All @@ -52,6 +53,7 @@ function useLocalWorker({
rules,
enableLocalPersistence,
ip,
crons,
}: LocalProps) {
// TODO: pass vars via command line
const local = useRef<ReturnType<typeof spawn>>();
Expand Down Expand Up @@ -150,6 +152,7 @@ function useLocalWorker({
dataBlobBindings,
sourceMap: true,
logUnhandledRejections: true,
crons,
};

// The path to the Miniflare CLI assumes that this file is being run from
Expand Down Expand Up @@ -247,6 +250,7 @@ function useLocalWorker({
bindings.wasm_modules,
bindings.text_blobs,
bindings.data_blobs,
crons,
]);
return { inspectorUrl };
}
2 changes: 2 additions & 0 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ export async function main(argv: string[]): Promise<void> {
r2_buckets: config.r2_buckets,
unsafe: config.unsafe?.bindings,
}}
crons={config.triggers.crons}
/>
);
await waitUntilExit();
Expand Down Expand Up @@ -1310,6 +1311,7 @@ export async function main(argv: string[]): Promise<void> {
r2_buckets: config.r2_buckets,
unsafe: config.unsafe?.bindings,
}}
crons={config.triggers.crons}
inspectorPort={await getPort({ port: 9229 })}
/>
);
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/miniflare-cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async function main() {
try {
// Start Miniflare development server
await mf.startServer();
await mf.startScheduler();
} catch (e) {
mf.log.error(e as Error);
process.exitCode = 1;
Expand Down