deno.com

Deno 队列

在 Github 上编辑

警告:这是一个不稳定的 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

您找到所需的内容了吗?

隐私政策