Skip to content

Commit

Permalink
Merge pull request #652 from cloudflare/jspspike/cron-trigger
Browse files Browse the repository at this point in the history
Trigger scheduled handler on '/cdn-cgi/mf/scheduled'
  • Loading branch information
jspspike authored Aug 14, 2023
2 parents cf080a8 + 9b183da commit 3b4b175
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 15 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
25 changes: 23 additions & 2 deletions packages/miniflare/src/workers/core/entry.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,29 @@ async function handleQueue(
const flattened = await request.json<number | unknown[]>();
const messages = unflatten(flattened, structuredSerializableRevivers);
const queueResponse = await service.queue(queueName, messages);
(queueResponse as QueueResponse & { time: number }).time =
(queueResponse as FetcherQueueResult & { time: number }).time =
Date.now() - startTime;
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");
});
3 changes: 0 additions & 3 deletions packages/miniflare/test/plugins/d1/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ export class TestD1Database implements D1Database {
return this[kSend]("/batch", statements);
}

// @ts-expect-error this function should return a `Promise<D1ExecResult>`,
// not a `Promise<D1Result<T>>`, `@cloudflare/workers-types` is wrong here
// TODO(now): fix in `@cloudflare/workers-types`
async exec(query: string): Promise<D1ExecResult> {
return this[kSend]("/exec", query);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/miniflare/test/plugins/d1/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export default (binding: string, WORKER_PATH: string) => {
const select = await db.prepare(`SELECT * FROM ${tableColours}`);
let result: ColourRow | null = await select.first<ColourRow>();
t.deepEqual(result, { id: 1, name: "red", rgb: 0xff0000 });
let id = await select.first<number>("id");
let id: number | null = await select.first<number>("id");
t.is(id, 1);

// Check with multiple statements (should only match on first statement)
Expand Down

0 comments on commit 3b4b175

Please sign in to comment.