kstulgys
kstulgys5mo ago

What's the best way to manage long running functions in convex actions?

I have 3 async functions that depend one on each other so I can not do Promise.all I'm running these in internal action ("use node") each function can take ~5 seconds so total action execution time could take ~15 seconds Is this is healthy for convex? is there a better way todo this?
9 Replies
ian
ian5mo ago
Am I understanding this right that they need to run sequentially, and you don't want to just run the code from one place inline? Having a top level action is the simple approach, but it contributes to action execution time for something that is running idle. You could also have each one schedule the next one when it's done. An older post that still has some good ideas: https://stack.convex.dev/background-job-management
Background Job Management
Implement asynchronous job patterns using a table to track progress. Fire-and-forget, cancelation, timeouts, and more.
ian
ian5mo ago
I might have misunderstood - if it's not calling 3 convex functions but just doing inline slow operations, then you're doing it right. Actions can run for up to 10m so it's "healthy" for Convex.
kstulgys
kstulgysOP5mo ago
Yeah I want to run 3 long running api calls (no reading writing convex data).
ian
ian5mo ago
Great. Then yes just calling them from an action that sits and waits is what I would do.
kstulgys
kstulgysOP5mo ago
how does that impact my billing though? Execution time in lamdas cost money, right? I wouldn't expect that function which runs 15-20s be running very often though but anyway
ian
ian5mo ago
For actions you pay for GBh - a combo of how much memory it's using and how long it runs. If you're using 5MB and running for 15s, you can do ~1 million of them each month on the free plan (20GBh) or 12 million on the pro plan, after which every 1 million calls would be ~$6 if my napkin math is right
ian
ian5mo ago
I'm not sure how much memory you'd be using though. You can check in the log stream and attribute it to each function call if you want though
Log Streams | Convex Developer Hub
Configure logging integrations for your Convex deployment
ian
ian5mo ago
other solutions would look like calling something that calls back to a webhook, or kicking something off and polling with the scheduler, but personally I wouldn't worry about 15s - the cost of whatever service you're hitting is likely much larger than some action compute in Convex
kstulgys
kstulgysOP5mo ago
thanks for reply!

Did you find this page helpful?