← Cloudflare Queues / queues / příklady
Používání Queues z Durable Objects
Následující příklad ukazuje, jak napsat skript Workeru, který publikuje do Cloudflare Queues zevnitř Durable Object.
Požadavky:
- A fronta vytvořena prostřednictvím Cloudflare dashboardu nebo přes wrangler CLI.
- A nakonfigurováno producent binding v dashboardu Cloudflare nebo v souboru Wrangler.
- A Vazba na namespace Durable Object.
Soubor Wrangler nakonfigurujte následovně:
{
"$schema": "./node_modules/wrangler/config-schema.json",
"name": "my-worker",
"queues": {
"producers": [
{
"queue": "my-queue",
"binding": "YOUR_QUEUE"
}
]
},
"durable_objects": {
"bindings": [
{
"name": "YOUR_DO_CLASS",
"class_name": "YourDurableObject"
}
]
},
"migrations": [
{
"tag": "v1",
"new_sqlite_classes": [
"YourDurableObject"
]
}
]
}"$schema" = "./node_modules/wrangler/config-schema.json"
name = "my-worker"
[[queues.producers]]
queue = "my-queue"
binding = "YOUR_QUEUE"
[[durable_objects.bindings]]
name = "YOUR_DO_CLASS"
class_name = "YourDurableObject"
[[migrations]]
tag = "v1"
new_sqlite_classes = [ "YourDurableObject" ]Následující skript Workeru:
- Vytvoří stub objektu Durable Object nebo načte existující na základě userId.
- Předá data požadavku objektu Durable Object.
- Publikuje do fronty přímo z objektu Durable Object.
Rozšíření DurableObject základní třída zajišťuje, že váš Env dostupné na this.env a stav Durable Object dostupný na this.ctx v rámci fetch() handler v Durable Object.
import { DurableObject } from "cloudflare:workers";
interface Env {
YOUR_QUEUE: Queue;
YOUR_DO_CLASS: DurableObjectNamespace<YourDurableObject>;
}
export default {
async fetch(req, env, ctx): Promise<Response> {
// Assume each Durable Object is mapped to a userId in a query parameter
// In a production application, this will be a userId defined by your application
// that you validate (and/or authenticate) first.
const url = new URL(req.url);
const userIdParam = url.searchParams.get("userId");
if (userIdParam) {
// Get a stub that allows you to call that Durable Object
const durableObjectStub = env.YOUR_DO_CLASS.getByName(userIdParam);
// Pass the request to that Durable Object and await the response
// This invokes the constructor once on your Durable Object class (defined further down)
// on the first initialization, and the fetch method on each request.
// We pass the original Request to the Durable Object's fetch method
const response = await durableObjectStub.fetch(req);
// This would return "wrote to queue", but you could return any response.
return response;
}
return new Response("userId must be provided", { status: 400 });
},
} satisfies ExportedHandler<Env>;
export class YourDurableObject extends DurableObject<Env> {
async fetch(req: Request): Promise<Response> {
// Error handling elided for brevity.
// Publish to your queue
await this.env.YOUR_QUEUE.send({
id: this.ctx.id.toString(), // Write the ID of the Durable Object to your queue
// Write any other properties to your queue
});
return new Response("wrote to queue");
}
}