H
Hono4mo ago
Paul

Can the queue only be defined in the root?

When writing a Cloudflare queue with Honojs, it seems the queue needs to be exported as part of the default. This default export cannot be mounted in another route path. Do all queue handlers need to be exported from the root?
1 Reply
mau
mau4mo ago
Yes, i also did with root and i wasn't able to think of any other way myself.
import { Hono } from 'hono';

type Environment = {
readonly ERROR_QUEUE: Queue<any>;
};

const queue = new Hono<{
Bindings: Environment;
}>();

queue.get('/', async (c) => {
await c.env.ERROR_QUEUE.send({ progressId: 1 });
return c.json({ success: true });
});

export default queue;

export default {
fetch: app.fetch,
async queue(batch: MessageBatch<any>, env: any) {
for (const message of batch.messages) {
queueProcessor(message);
}
},
};

function queueProcessor(message: any) {
const { body, attempts } = message;
if (Math.random() < 0.5) {
switch (body?.event) {
case 'start-quiz':
console.log('data', body, 'attempts', attempts);
break;
default:
console.log('data', body, 'attempts', attempts);
break;
}
} else {
throw new Error('Failed!');
}
}
import { Hono } from 'hono';

type Environment = {
readonly ERROR_QUEUE: Queue<any>;
};

const queue = new Hono<{
Bindings: Environment;
}>();

queue.get('/', async (c) => {
await c.env.ERROR_QUEUE.send({ progressId: 1 });
return c.json({ success: true });
});

export default queue;

export default {
fetch: app.fetch,
async queue(batch: MessageBatch<any>, env: any) {
for (const message of batch.messages) {
queueProcessor(message);
}
},
};

function queueProcessor(message: any) {
const { body, attempts } = message;
if (Math.random() < 0.5) {
switch (body?.event) {
case 'start-quiz':
console.log('data', body, 'attempts', attempts);
break;
default:
console.log('data', body, 'attempts', attempts);
break;
}
} else {
throw new Error('Failed!');
}
}
Want results from more Discord servers?
Add your server