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
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 returnThis should work, you can run the logic for communicating with openAI and telegram api in an async function. make sure you call it with For example The
ctx.waitUntil
makes sure the worker stays alive after the response has been returned until the aiLogic
promise is resolvedyour suggestion seems to work perfectly, I will only add that the waitUntil method can be accessed from Hono context via
c.executionCtx.waitUntil
ahh yes, sorry for that 😄