Skip to content

Instantly share code, notes, and snippets.

@pigri
Last active October 4, 2023 16:24
Show Gist options
  • Save pigri/109a78496966758f781df8bca0ecbf3d to your computer and use it in GitHub Desktop.
Save pigri/109a78496966758f781df8bca0ecbf3d to your computer and use it in GitHub Desktop.
Cloudflare worker - Example for Queue scaling
//https://www.npmjs.com/package/round-robin-js
import { RandomRoundRobin } from 'round-robin-js';
async function sender(queue: string, message: any, env: Env) {
try {
const processors = {
PROCESSOR1: env.ENVIRONMENT === 'dev' ? env.PROCESSOR1_DEV : env.PROCESSOR1,
PROCESSOR2: env.ENVIRONMENT === 'dev' ? env.PROCESSOR2_DEV : env.PROCESSOR2,
PROCESSOR3: env.ENVIRONMENT === 'dev' ? env.PROCESSOR3_DEV : env.PROCESSOR3,
PROCESSOR4: env.ENVIRONMENT === 'dev' ? env.PROCESSOR4_DEV : env.PROCESSOR4,
};
const processor = processors[queue];
if (processor) {
await processor.send(message);
message.ack();
}
} catch (error) {
console.log(error);
throw error;
}
}
async function response(request: Request, env: Env, writeKey: string) {
const data = (await request.json()) as Data;
const queues = new RandomRoundRobin<string>(['PROCESSOR1', 'PROCESSOR2', 'PROCESSOR3', 'PROCESSOR4']);
const queue = queues.next().value;
const message = {
data
};
await sender(queue, message, env);
}
@nathanclevenger
Copy link

@pigri great example! I've had this same issue ... where are you getting RandomRoundRobin from?

@pigri
Copy link
Author

pigri commented Aug 16, 2023

I'm sorry good point. I fixed it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment