Skip to content

Commit

Permalink
Trigger scheduled handler on '/cdn-cgi/mf/scheduled'
Browse files Browse the repository at this point in the history
  • Loading branch information
jspspike committed Aug 11, 2023
1 parent cf080a8 commit 37c58b2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/miniflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"zod": "^3.20.6"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230419.0",
"@cloudflare/workers-types": "^4.20230807.0",
"@types/better-sqlite3": "^7.6.2",
"@types/debug": "^4.1.7",
"@types/estree": "^1.0.0",
Expand Down
23 changes: 22 additions & 1 deletion packages/miniflare/src/workers/core/entry.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ async function handleQueue(
return Response.json(queueResponse);
}

async function handleScheduled(
params: URLSearchParams,
service: Fetcher
): Promise<Response> {
const time = params.get("time");
const scheduledTime = time ? new Date(parseInt(time)) : undefined;
const cron = params.get("cron") ?? undefined;

const result = await service.scheduled({
scheduledTime,
cron,
});

return new Response(result.outcome, {
status: result.outcome === "ok" ? 200 : 500,
});
}

export default <ExportedHandler<Env>>{
async fetch(request, env, ctx) {
const startTime = Date.now();
Expand All @@ -183,11 +201,14 @@ export default <ExportedHandler<Env>>{

try {
const customEvent = request.headers.get(CoreHeaders.CUSTOM_EVENT);
// TODO(soon): support scheduled events, requires support from workerd
if (customEvent === "queue") {
return await handleQueue(request, url, service, startTime);
}

if (url.pathname === "/cdn-cgi/mf/scheduled") {
return await handleScheduled(url.searchParams, service);
}

let response = await service.fetch(request);
if (!isDispatchFetch) {
response = await maybePrettifyError(request, response, env);
Expand Down
28 changes: 28 additions & 0 deletions packages/miniflare/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,31 @@ test("Miniflare: Accepts https requests", async (t) => {

t.assert(log.logs[0][1].startsWith("Ready on https://"));
});

test("Miniflare: Manually triggered scheduled events", async (t) => {
const log = new TestLog(t);

const mf = new Miniflare({
log,
modules: true,
script: `
let scheduledRun = false;
export default {
fetch() {
return new Response(scheduledRun);
},
scheduled() {
scheduledRun = true;
}
}`,
});

let res = await mf.dispatchFetch("http://localhost");
t.is(await res.text(), "false");

res = await mf.dispatchFetch("http://localhost/cdn-cgi/mf/scheduled");
t.is(await res.text(), "ok");

res = await mf.dispatchFetch("http://localhost");
t.is(await res.text(), "true");
});

0 comments on commit 37c58b2

Please sign in to comment.