本页还没有译文
本页 9 段还没译成繁體中文,下面显示的是英文原文。Scheduled Events
还没翻译译自英文(developers.cloudflare.com)本页 9 段都还没译
Cron Triggers
scheduled events are automatically dispatched according to the specified cron
triggers:
const mf = new Miniflare({
crons: ["15 * * * *", "45 * * * *"],
});HTTP Triggers
Because waiting for cron triggers is annoying, you can also make HTTP requests
to /cdn-cgi/local/scheduled to trigger scheduled events:
$ curl "http://localhost:8787/cdn-cgi/local/scheduled"To simulate different values of scheduledTime and cron in the dispatched
event, use the time and cron query parameters:
$ curl "http://localhost:8787/cdn-cgi/local/scheduled?time=1000"
$ curl "http://localhost:8787/cdn-cgi/local/scheduled?cron=*+*+*+*+*"Dispatching Events
When using the API, the getWorker function can be used to dispatch
scheduled events to your Worker. This can be used for testing responses. It
takes optional scheduledTime and cron parameters, which default to the
current time and the empty string respectively. It will return a promise which
resolves to an array containing data returned by all waited promises:
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 }