I am trying to run a background promise

I am trying to run a background promise to drain logs in a durable object that looks like this:
const res = new Response("broadcasted")
logger.Drain()
return res
const res = new Response("broadcasted")
logger.Drain()
return res
With logger.Drain() looking like:
async (lines) => {
console.log("draining logs!")
await googleDestinationFunction(lines, c.env)
console.log("done!")
}
async (lines) => {
console.log("draining logs!")
await googleDestinationFunction(lines, c.env)
console.log("done!")
}
Looking at my execution logs within cloudflare, I see only the draining logs! log, but not the done! log. I've also tried returning the googleDestinationFunction log directly (which also contains logs that I do not see) and it just seems like the DO is exiting once the promise starts running... It is my understanding that the DO should let any other promises run the in background, correct?
8 Replies
DanTheGoodman
DanTheGoodmanOP2y ago
In more detail, I am using the hono framework and the route looks like:
this.app.post("/broadcast/:eventKey", async (c) => {
const eventKey = c.req.param("eventKey")
const logger = new WorkerLogger({
level: "DEBUG",
metaInConsole: true,
withMeta: {
apiURL: this.env.API_URL,
eventKey
},
levelKey: "severity",
destinationFunction: async (lines) => {
console.log("draining logs!")
return googleDestinationFunction(lines, c.env)
}
})
const payload = await c.req.text()
logger.info("broadcasting", {
payload
})
this.sendWebSocketMessage(payload)
const res = new Response("broadcasted")
logger.Drain()
return res
})
this.app.post("/broadcast/:eventKey", async (c) => {
const eventKey = c.req.param("eventKey")
const logger = new WorkerLogger({
level: "DEBUG",
metaInConsole: true,
withMeta: {
apiURL: this.env.API_URL,
eventKey
},
levelKey: "severity",
destinationFunction: async (lines) => {
console.log("draining logs!")
return googleDestinationFunction(lines, c.env)
}
})
const payload = await c.req.text()
logger.info("broadcasting", {
payload
})
this.sendWebSocketMessage(payload)
const res = new Response("broadcasted")
logger.Drain()
return res
})
(I adjusted the desintationfunction and same issue)
DanTheGoodman
DanTheGoodmanOP2y ago
added some logs in the promise and I can see that it only gets to internal log 1/8 before it exits
DanTheGoodman
DanTheGoodmanOP2y ago
unless it only includes logs before the response.... but still I am not getting the logs in google
DanTheGoodman
DanTheGoodmanOP2y ago
kian
kian2y ago
you're not awaiting logger.Drain() so this seems like expected behaviour to me if you don't wrap it in a waitUntil then the Worker will just close after returning a Response
kian
kian2y ago
Ah, a DO Nevermind then, no idea haha
DanTheGoodman
DanTheGoodmanOP2y ago
D: there are no exceptions too so it's not erroring on the JSON.parse() WOW ok figured it out this is cursed changed to return googleDestinationFunction(lines, this.env) (using the DO reference to .env vs the hono reference)
Want results from more Discord servers?
Add your server