Skip to content

Commit

Permalink
fix: add support for cron triggers in dev --local mode
Browse files Browse the repository at this point in the history
Currently, I don't know if there is support for doing this in "remote" dev mode.

Resolves #737
  • Loading branch information
petebacondarwin committed Mar 31, 2022
1 parent c38ae3d commit 4424250
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 0 deletions.
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 @@ -157,6 +158,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 @@ -141,6 +143,7 @@ function useLocalWorker({
textBlobBindings,
sourceMap: true,
logUnhandledRejections: true,
crons,
};

// The path to the Miniflare CLI assumes that this file is being run from
Expand Down Expand Up @@ -237,6 +240,7 @@ function useLocalWorker({
rules,
bindings.wasm_modules,
bindings.text_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 @@ -926,6 +926,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 @@ -1308,6 +1309,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

0 comments on commit 4424250

Please sign in to comment.