TheLegendVibes
TheLegendVibes
Explore posts from servers
CDCloudflare Developers
Created by TheLegendVibes on 2/1/2025 in #workers-help
POST method is not working in fetch inside Queue Handler
Hono version: 4.6.20 Wrangler version: 3.107.2 I've tried a lot but fetch function inside queue is not executing in the worker both deployed version and local dev server.
import { Hono, Env } from 'hono'
import { env } from "hono/adapter";
import { send_to_queue } from '../utils/send-to-queue';

type Bindings = {
REFERRAL_ENGINE_URL: string;
DB_QUEUE: Queue;
}

interface QueueDataType {
data: any;
ref_url: string;
}

const app = new Hono<{ Bindings: Bindings }>()

app.get('/', (c) => {
return c.json({ message: "Go Away" }, 200);
})

app.post('/', async (c) => {
const data = await c.req.json()
const EP_URL = env(c).EP_URL;
console.log("Data Received")

const Q_DATA : QueueDataType = {
data: data,
ref_url: EP_URL
}

await send_to_queue(Q_DATA, c)

return c.json({ message: 'Request Received!' })
})

async function queueHandler(batch: MessageBatch<QueueDataType>, c: Context) {
for (let message of batch.messages) {
console.log("Data Received from Queue");
try {
console.log("Sending data to referral engine");
console.log(message.body.data);
console.log(typeof(message.body.data));
const response = await fetch(`${message.body.ref_url}/api/v1/referral`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer 9e985a31cd0581672bf8454123662de7'
},
body: JSON.stringify(message.body.data),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// await send_to_ref(message.body.data, message.body.ref_url);
message.ack();
} catch (error) {
console.error("Error forwarding data from queue:", error);
console.log("Retrying after 100 seconds");
message.retry({ delaySeconds: 100 });
}
}
}

export default {
fetch: app.fetch,
async queue(batch: MessageBatch<QueueDataType>, c: Env){
queueHandler(batch, c)
},
};
import { Hono, Env } from 'hono'
import { env } from "hono/adapter";
import { send_to_queue } from '../utils/send-to-queue';

type Bindings = {
REFERRAL_ENGINE_URL: string;
DB_QUEUE: Queue;
}

interface QueueDataType {
data: any;
ref_url: string;
}

const app = new Hono<{ Bindings: Bindings }>()

app.get('/', (c) => {
return c.json({ message: "Go Away" }, 200);
})

app.post('/', async (c) => {
const data = await c.req.json()
const EP_URL = env(c).EP_URL;
console.log("Data Received")

const Q_DATA : QueueDataType = {
data: data,
ref_url: EP_URL
}

await send_to_queue(Q_DATA, c)

return c.json({ message: 'Request Received!' })
})

async function queueHandler(batch: MessageBatch<QueueDataType>, c: Context) {
for (let message of batch.messages) {
console.log("Data Received from Queue");
try {
console.log("Sending data to referral engine");
console.log(message.body.data);
console.log(typeof(message.body.data));
const response = await fetch(`${message.body.ref_url}/api/v1/referral`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer 9e985a31cd0581672bf8454123662de7'
},
body: JSON.stringify(message.body.data),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// await send_to_ref(message.body.data, message.body.ref_url);
message.ack();
} catch (error) {
console.error("Error forwarding data from queue:", error);
console.log("Retrying after 100 seconds");
message.retry({ delaySeconds: 100 });
}
}
}

export default {
fetch: app.fetch,
async queue(batch: MessageBatch<QueueDataType>, c: Env){
queueHandler(batch, c)
},
};
5 replies
HHono
Created by TheLegendVibes on 2/1/2025 in #help
Post method not working with Queues on CF Worker
Hono version: 4.6.20 Wrangler version: 3.107.2 I've tried a lot but fetch function inside queue is not executing in the worker both deployed version and local dev server.
import { Hono, Env } from 'hono'
import { env } from "hono/adapter";
import { send_to_queue } from '../utils/send-to-queue';

type Bindings = {
REFERRAL_ENGINE_URL: string;
DB_QUEUE: Queue;
}

interface QueueDataType {
data: any;
ref_url: string;
}

const app = new Hono<{ Bindings: Bindings }>()

app.get('/', (c) => {
return c.json({ message: "Go Away" }, 200);
})

app.post('/', async (c) => {
const data = await c.req.json()
const EP_URL = env(c).EP_URL;
console.log("Data Received")

const Q_DATA : QueueDataType = {
data: data,
ref_url: EP_URL
}

await send_to_queue(Q_DATA, c)

return c.json({ message: 'Request Received!' })
})

async function queueHandler(batch: MessageBatch<QueueDataType>, c: Context) {
for (let message of batch.messages) {
console.log("Data Received from Queue");
try {
console.log("Sending data to referral engine");
console.log(message.body.data);
console.log(typeof(message.body.data));
const response = await fetch(`${message.body.ref_url}/api/v1/referral`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer 9e985a31cd0581672bf8454123662de7'
},
body: JSON.stringify(message.body.data),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// await send_to_ref(message.body.data, message.body.ref_url);
message.ack();
} catch (error) {
console.error("Error forwarding data from queue:", error);
console.log("Retrying after 100 seconds");
message.retry({ delaySeconds: 100 });
}
}
}

export default {
fetch: app.fetch,
async queue(batch: MessageBatch<QueueDataType>, c: Env){
queueHandler(batch, c)
},
};
import { Hono, Env } from 'hono'
import { env } from "hono/adapter";
import { send_to_queue } from '../utils/send-to-queue';

type Bindings = {
REFERRAL_ENGINE_URL: string;
DB_QUEUE: Queue;
}

interface QueueDataType {
data: any;
ref_url: string;
}

const app = new Hono<{ Bindings: Bindings }>()

app.get('/', (c) => {
return c.json({ message: "Go Away" }, 200);
})

app.post('/', async (c) => {
const data = await c.req.json()
const EP_URL = env(c).EP_URL;
console.log("Data Received")

const Q_DATA : QueueDataType = {
data: data,
ref_url: EP_URL
}

await send_to_queue(Q_DATA, c)

return c.json({ message: 'Request Received!' })
})

async function queueHandler(batch: MessageBatch<QueueDataType>, c: Context) {
for (let message of batch.messages) {
console.log("Data Received from Queue");
try {
console.log("Sending data to referral engine");
console.log(message.body.data);
console.log(typeof(message.body.data));
const response = await fetch(`${message.body.ref_url}/api/v1/referral`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer 9e985a31cd0581672bf8454123662de7'
},
body: JSON.stringify(message.body.data),
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// await send_to_ref(message.body.data, message.body.ref_url);
message.ack();
} catch (error) {
console.error("Error forwarding data from queue:", error);
console.log("Retrying after 100 seconds");
message.retry({ delaySeconds: 100 });
}
}
}

export default {
fetch: app.fetch,
async queue(batch: MessageBatch<QueueDataType>, c: Env){
queueHandler(batch, c)
},
};
35 replies