Return ok status immediately on async process or not?

My use case: - A webhook intercepts request from telegram bot - Request message is sent to openai and response awaited - Response is sent to Telegram bot with Telegram API - Request returns a 200 response I am using Holo the issue here seems to be that if the 200 response seems to take a while, telegram can retry the request, entering into a never ending loop potentially and pinging openai several times with the same question I think maybe an option is to immediately send a 200 response and then await for openai response (the response is sent to telegram via telegram API, not in the response) However I'm not sure that is okay with Holo or workers (looks like Holo needs to return a response) I.e. - app.post (from telegram) - retrieve user message - send 200 response - send message to OpenAI API - wait for OpenAI response - Send OpenAI response to Telegram API - return I don't think this will work I'm not really a backender, what is the correct way to do this?
3 Replies
Dullaz
Dullaz•3mo ago
app.post (from telegram) retrieve user message send 200 response send message to OpenAI API wait for OpenAI response Send OpenAI response to Telegram API return
This should work, you can run the logic for communicating with openAI and telegram api in an async function. make sure you call it with
ctx.waitUntil
ctx.waitUntil
For example
export default {
async fetch(request, env, ctx) {
ctx.waitUntil(aiLogic())
return new Response("ok!");
},
};

async function aiLogic() {
functionToCallOpenAi();
functionToMessageTelegram();
}
export default {
async fetch(request, env, ctx) {
ctx.waitUntil(aiLogic())
return new Response("ok!");
},
};

async function aiLogic() {
functionToCallOpenAi();
functionToMessageTelegram();
}
The ctx.waitUntil makes sure the worker stays alive after the response has been returned until the aiLogic promise is resolved
saito200
saito200•3mo ago
your suggestion seems to work perfectly, I will only add that the waitUntil method can be accessed from Hono context via c.executionCtx.waitUntil
Dullaz
Dullaz•3mo ago
ahh yes, sorry for that 😄
Want results from more Discord servers?
Add your server