@@ -13,48 +13,19 @@ The latest releases of [@cloudflare/agents] (https://github.com/cloudflare/agent
1313### Lightweight .queue for fast task deferral
1414
1515You can use .queue() to enqueue background work — ideal for tasks like processing user messages, sending notifications etc.
16- Here's a full example using .queue() inside a tool, with batching and concurrency control:
1716
1817``` ts
19- const queueMessage = tool ({
20- description: " Queue a message to be processed by the agent" ,
21- parameters: z .object ({
22- message: z .string ().describe (" The message to process" ),
23- priority: z
24- .enum ([" low" , " normal" , " high" ])
25- .default (" normal" )
26- .describe (" Priority level for the message" ),
27- }),
28- execute : async ({ message , priority = " normal" }) => {
29- const { agent } = getCurrentAgent <Chat >();
30-
31- if (! agent ) {
32- return " Error: Agent not available" ;
33- }
34-
35- try {
36- // Create a batch of 100 messages
37- const batchNumbers = Array .from ({ length: 100 }, (_ , i ) => i + 1 );
38-
39- const taskIds = await pMap (
40- batchNumbers ,
41- async (i ) => {
42- return agent .queue (" processMessage" , {
43- message: ` ${message } (batch ${i }/100) ` ,
44- priority ,
45- });
46- },
47- { concurrency: 6 }, // Run up to 6 queues in parallel
48- );
49-
50- return ` Queued 100 messages. Example Task IDs: ${taskIds .slice (0 , 5 ).join (" , " )}... ` ;
51- } catch (error ) {
52- console .error (" Error queueing message" , error );
53- return ` Error queueing message: ${error } ` ;
54- }
55- },
56- });
57- ```
18+ class MyAgent extends Agent {
19+ doSomethingExpensive(payload ){
20+ // a long running process that you want to run in the background
21+ }
22+
23+ queueSomething(){
24+ await this .queue (' doSomethingExpensive' , somePayload ); // this will NOT block further execution, and runs in the background
25+ await this .queue (' doSomethingExpensive' , someOtherPayload ); // the callback will NOT run until the previous callback is complete
26+ // ... call as many times as you want
27+ }
28+ }
5829
5930Notes :
6031
0 commit comments