CPU time limit ignored in paid worker

I have a worker which needs to perform a long task for about 2 minutes after the initial fetch request has been dealt with. I'm using ctx.waitUntil, but after about 30 seconds it stops. I've set CPU time limit to 300_000 ms (5 minutes), but it hasn't helped. What am I missing?
No description
11 Replies
Walshy
Walshy2mo ago
waitUntil only extends 30 seconds, that's normal. I'd recommend using Workflows, start a workflow and run your task there. It's what they're made for :)
Walshy
Walshy2mo ago
Cloudflare Docs
Cloudflare Workflows
With Workflows, you can build applications that chain together multiple steps, automatically retry failed tasks, and persist state for minutes, hours, or even weeks - with no infrastructure to manage.
Titan
TitanOP2mo ago
so if I just use a normal while loop then I can get past 30 seconds? I don't need to respond to the user so I didn't technically need to use waitUntil
Walshy
Walshy2mo ago
You don't need a while loop, you can just do whatever if you want to block the response
Titan
TitanOP2mo ago
thanks, I'll give that a go and checkout workflows. Thanks for your help!
Walshy
Walshy2mo ago
I'd recommend workflows though haha
Titan
TitanOP2mo ago
I use the Nuxt framework so it's not immediately clear how I'd get access to that step function in my Nuxt event handlers also does the worker exit if it detects idle time? I have a wait loop and at the end of each loop I have:
// Wait 5 seconds between each poll
await new Promise(resolve => setTimeout(resolve, 5000))
// Wait 5 seconds between each poll
await new Promise(resolve => setTimeout(resolve, 5000))
Walshy
Walshy2mo ago
your worker could wait for 24 hours if it wanted we do not limit wall time (note: your worker may stop executing any time after 30 seconds due to us updating the runtime on that particularly server the worker is running on but not a frequent occurrence)
Titan
TitanOP2mo ago
I can't seem to get the loop to go past 2 iterations before the worker is silently killed. It doesn't continue on past the for loop or anything, just stops executing in the 2nd loop
for (const _maxPolls of Array.from({ length: 20 })) {
// Call an API then wait 5 seconds to do it again, 20 times
await new Promise(resolve => setTimeout(resolve, 5000))
}
for (const _maxPolls of Array.from({ length: 20 })) {
// Call an API then wait 5 seconds to do it again, 20 times
await new Promise(resolve => setTimeout(resolve, 5000))
}
Titan
TitanOP2mo ago
No description
Titan
TitanOP2mo ago
and today after switching back to waitUntil I get: IoContext timed out due to inactivity, waitUntil tasks were cancelled without completing.

Did you find this page helpful?