Debugging fetch in a consumer
Yes, I'm
await
ing the fetch
and I also put inside a try/catch
, but no error is logged.
The last log I see is the API URL
log, and then the code skips everything below the log
19 Replies
Can you show the entire consumer code, just to make sure nothing is missed?
And is this running locally or is it deployed?
cc @Charlie B | KV, Queues, PubSub
I tried both, same behavior
did you try a normal for of loop instead of
batch.messages.forEach
? what you have now will just create a bunch of unresolved promises - forEach is not really ideal for asyncUnknown User•14mo ago
Message Not Public
Sign In & Join Server To View
us meaning cloudflare? Hopefully you are not using
batch.messages.forEach
in any public samples!Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
ideally cf would show the logs even from work that occurs after the
queue
handler returnsUnknown User•14mo ago
Message Not Public
Sign In & Join Server To View
i wonder what the guidance/semantics is around work intended to be deferred until after the queue handler returns (ie not really like this case) - same as with
fetch
handlers: waitUntil
? Or do you wait for all pending promises up to a certain limit like DONot sure if this is the case here. I'm just triggering one event at a time, but if this code isn't ideal I'll change and test
that foreach is sync and will fire off your code async, and not wait for it
yea just change to
for (const m of batch.messages) {
that way you can wait for the results
or await Promise.all(batch.messages.map(work))
if you want to be fancy and run them all in parallel (prob overkill in your case)Got it
thank, will do
It works, thanks @John Spurlock <a:wumpus_party:393564669765353483>
nice!
that'll be better for errors too, if your external fetch call throws, the error will now be raised up and out of the queue handler - triggering the infrastructure to retry that message later, which is probably what you want never mind, you have a giant try catch around it! : )
Got it, thanks for your explanation and help
waitUntil. Anything else is undefined (and should, as of now, be cancelled immediately when the handler returns)
thanks - wouldn't expect deferred logic to be nearly as desired for
queue
where no clients are potentially waiting vs fetch
where they potentially are - and most failures I'd imagine you'd want to wait for, before ack or not. only use case for deferred/waitUntil I could see for queue logic is analytics pings, where you might not want them in the critical path for processing, and don't care if some never make itYep, that's exactly right - metrics/logging after the fact. We should probably document this a little more clearly and although
forEach
and the interaction with promises isn't our problem, also document that
cc @Charlie B | KV, Queues, PubSub ^