deno.com

Deno 队列

警告:这是一个不稳定的 API,可能随时更改或移除。
Deno Queues 基于 Deno KV 构建,允许您卸载应用程序的部分任务或调度未来工作以异步运行。它是一种为您的项目添加可伸缩后台处理的简便方法。

描述您的消息对象的结构(可选)
interface Notification {
  forUser: string;
  body: string;
}
获取 KV 实例的引用
const kv = await Deno.openKv();
创建通知对象
const message: Notification = {
  forUser: "alovelace",
  body: "You've got mail!",
};
将消息入队以便立即发送
await kv.enqueue(message);
将消息入队以便在 3 天后发送
const delay = 1000 * 60 * 60 * 24 * 3;
await kv.enqueue(message, { delay });
通过配置键来检索未发送的消息
const backupKey = ["failed_notifications", "alovelace", Date.now()];
await kv.enqueue(message, { keysIfUndelivered: [backupKey] });
... 灾难来袭 ... 获取未发送的消息
const r = await kv.get<Notification>(backupKey);
console.log("Found failed notification for:", r.value?.forUser);
监听并处理消息。
kv.listenQueue((msg: Notification) => {
  console.log(`Dear ${msg.forUser}: ${msg.body}`);
});

使用 Deno CLI 在本地运行此示例

deno run --unstable-kv https://docs.deno.org.cn/examples/scripts/queues.ts

您找到所需内容了吗?

隐私政策