From 6af77193da66f502f121bd5cbe6d7f55855d1c69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Somhairle=20MacLe=C3=B2id?= Date: Thu, 13 Mar 2025 17:19:54 +0000 Subject: [PATCH 1/2] Update get-started.mdx --- src/content/docs/workers/testing/miniflare/get-started.mdx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/content/docs/workers/testing/miniflare/get-started.mdx b/src/content/docs/workers/testing/miniflare/get-started.mdx index d4086669e4d64b3..493ba7393714db0 100644 --- a/src/content/docs/workers/testing/miniflare/get-started.mdx +++ b/src/content/docs/workers/testing/miniflare/get-started.mdx @@ -150,6 +150,8 @@ const res = await mf.dispatchFetch("http://localhost:8787/", { }); console.log(await res.text()); // Hello Miniflare! +const worker = await mf.getWorker(); + const scheduledResult = await worker.scheduled({ cron: "* * * * *", }); @@ -367,6 +369,8 @@ const res = await mf.dispatchFetch("http://localhost:8787/", { }); const text = await res.text(); +const worker = await mf.getWorker(); + // Dispatch "scheduled" event to worker const scheduledResult = await worker.scheduled({ cron: "30 * * * *" }) From b5632822630fc0a81359e26ec44b8a7deb8f8b77 Mon Sep 17 00:00:00 2001 From: Samuel Macleod Date: Thu, 13 Mar 2025 18:29:22 +0000 Subject: [PATCH 2/2] fix code block --- .../workers/testing/miniflare/get-started.mdx | 82 +++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/src/content/docs/workers/testing/miniflare/get-started.mdx b/src/content/docs/workers/testing/miniflare/get-started.mdx index 493ba7393714db0..d7e39b87b0f36ef 100644 --- a/src/content/docs/workers/testing/miniflare/get-started.mdx +++ b/src/content/docs/workers/testing/miniflare/get-started.mdx @@ -103,46 +103,46 @@ to workers respectively: import { Miniflare } from "miniflare"; const mf = new Miniflare({ + modules: true, script: ` - export default { - let lastScheduledController; - let lastQueueBatch; - async fetch(request, env, ctx) { - const { pathname } = new URL(request.url); - if (pathname === "/scheduled") { - return Response.json({ - scheduledTime: lastScheduledController?.scheduledTime, - cron: lastScheduledController?.cron, - }); - } else if (pathname === "/queue") { - return Response.json({ - queue: lastQueueBatch.queue, - messages: lastQueueBatch.messages.map((message) => ({ - id: message.id, - timestamp: message.timestamp.getTime(), - body: message.body, - bodyType: message.body.constructor.name, - })), - }); - } else if (pathname === "/get-url") { - return new Response(request.url); - } else { - return new Response(null, { status: 404 }); - } - }, - async scheduled(controller, env, ctx) { - lastScheduledController = controller; - if (controller.cron === "* * * * *") controller.noRetry(); - }, - async queue(batch, env, ctx) { - lastQueueBatch = batch; - if (batch.queue === "needy") batch.retryAll(); - for (const message of batch.messages) { - if (message.id === "perfect") message.ack(); - } - } - } - `, + let lastScheduledController; + let lastQueueBatch; + export default { + async fetch(request, env, ctx) { + const { pathname } = new URL(request.url); + if (pathname === "/scheduled") { + return Response.json({ + scheduledTime: lastScheduledController?.scheduledTime, + cron: lastScheduledController?.cron, + }); + } else if (pathname === "/queue") { + return Response.json({ + queue: lastQueueBatch.queue, + messages: lastQueueBatch.messages.map((message) => ({ + id: message.id, + timestamp: message.timestamp.getTime(), + body: message.body, + bodyType: message.body.constructor.name, + })), + }); + } else if (pathname === "/get-url") { + return new Response(request.url); + } else { + return new Response(null, { status: 404 }); + } + }, + async scheduled(controller, env, ctx) { + lastScheduledController = controller; + if (controller.cron === "* * * * *") controller.noRetry(); + }, + async queue(batch, env, ctx) { + lastQueueBatch = batch; + if (batch.queue === "needy") batch.retryAll(); + for (const message of batch.messages) { + if (message.id === "perfect") message.ack(); + } + } + }`, }); const res = await mf.dispatchFetch("http://localhost:8787/", { @@ -158,8 +158,8 @@ const scheduledResult = await worker.scheduled({ console.log(scheduledResult); // { outcome: "ok", noRetry: true }); const queueResult = await worker.queue("needy", [ - { id: "a", timestamp: new Date(1000), body: "a" }, - { id: "b", timestamp: new Date(2000), body: { b: 1 } }, + { id: "a", timestamp: new Date(1000), body: "a", attempts: 1 }, + { id: "b", timestamp: new Date(2000), body: { b: 1 }, attempts: 1 }, ]); console.log(queueResult); // { outcome: "ok", retryAll: true, ackAll: false, explicitRetries: [], explicitAcks: []} ```