scheduled イベントは、指定した Cron Triggers に従って自動でディスパッチされます。
const mf = new Miniflare({
crons: ["15 * * * *", "45 * * * *"],
});
Cron Triggers を待つのは面倒なので、/cdn-cgi/local/scheduled へ HTTP リクエストを送って scheduled イベントを発火することもできます。
$ curl "http://localhost:8787/cdn-cgi/local/scheduled"ディスパッチされるイベントの scheduledTime と cron に別の値をシミュレートするには、time と cron クエリパラメーターを使います。
$ curl "http://localhost:8787/cdn-cgi/local/scheduled?time=1000"
$ curl "http://localhost:8787/cdn-cgi/local/scheduled?cron=*+*+*+*+*"
API を使う場合、getWorker 関数で Worker に scheduled イベントをディスパッチできます。レスポンスのテストに使えます。省略可能な scheduledTime と cron パラメーターを取り、それぞれ現在時刻と空文字列がデフォルトです。待機した Promise が返したデータを含む配列に解決する Promise を返します。
import { Miniflare } from "miniflare";
const mf = new Miniflare({
modules: true,
script: `
export default {
async scheduled(controller, env, ctx) {
const lastScheduledController = controller;
if (controller.cron === "* * * * *") controller.noRetry();
}
}
`,
});
const worker = await mf.getWorker();
let scheduledResult = await worker.scheduled({
cron: "* * * * *",
});
console.log(scheduledResult); // { outcome: 'ok', noRetry: true }
scheduledResult = await worker.scheduled({
scheduledTime: new Date(1000),
cron: "30 * * * *",
});
console.log(scheduledResult); // { outcome: 'ok', noRetry: false }