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!');
}
}