You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a way to send items to the queue while they only get processed based on a limit (like 10 or 15 at a time or concurrently)?
This is what I'm currently doing, but is there a better way or a native way to do this?
import{Hono}from"https://deno.land/x/hono/mod.ts";import{z}from"https://deno.land/x/zod/mod.ts";import{zValidator}from"npm:@hono/zod-validator";constapp=newHono();constkv=awaitDeno.openKv();constMAX_JOBS=3;app.post("/",zValidator("json",z.object({id: z.number(),name: z.string(),}),),async(c)=>{constbody=c.req.valid('json');awaitkv.enqueue(body);returnc.json({message: "Added to queue"});},);Deno.serve(app.fetch);conststream=kv.watch([["activeJobs"]]);forawait(constentriesofstream){// check if we're already processing more than 3 jobsconstactiveJobs=(entries[0].valueasnumber)||0;if(activeJobs<=MAX_JOBS){// if not, listen to queue for more jobsconsole.log("listening for jobs, active jobs:",activeJobs);kv.listenQueue(async(msg)=>{// once you get a job, increment the activeJobs by 1console.log("msg:",msg);awaitkv.atomic().sum(["activeJobs"],1n).commit();// will run some logic and once done, reduce the activejobs count// to accept another job from queuesetTimeout(async()=>{awaitkv.atomic().sum(["activeJobs"],-1n).commit();},4000);});}}
Steps to reproduce (if applicable)
No response
Expected behavior (if applicable)
No response
Possible solution (if applicable)
No response
Additional context
No response
The text was updated successfully, but these errors were encountered:
🔍
Type of feedback
Feature request
Description
Is there a way to send items to the queue while they only get processed based on a limit (like 10 or 15 at a time or concurrently)?
This is what I'm currently doing, but is there a better way or a native way to do this?
Steps to reproduce (if applicable)
No response
Expected behavior (if applicable)
No response
Possible solution (if applicable)
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: