async actions and cron jobs
I think looking at the doc it should be ok to do async actions. Is it okay to use these with cron jobs?
8 Replies
hi
i want slso to join in this
Hi Joe, yes async actions work with cron jobs — async actions are the most common kind (you're talking about the
async () => { ... }
syntax in an action() wrapper, right?Yup! Thanks! As a follow up, can I do something like a simulated sleep then (https://www.sitepoint.com/delay-sleep-pause-wait/ ) ? I have a rate limited API I want to ping, but if I hit the rate limit, I was hoping to just sleep in the action until I could go again since data I want to store in convex might be partially but not fully retrieved at that point. Would there be a problem if one instance of the action hasn't finished when the cron job runs again ?
Delay, Sleep, Pause, & Wait in JavaScript — SitePoint
James Hibbard explains the pitfalls of implementing a sleep function in JavaScript, and digs into solutions for dealing with JavaScript timing issues.
@ian: You might also consider using the scheduer and passing the in-progress work in the arguments. You can always store in-progress things to the db too, via
runMutation
, which you can read from other cron invocations. https://docs.convex.dev/scheduling/scheduled-functionsTo answer your cron question- if one is still running when another invocation is due, it will skip it.
ah okay cool that works well - thanks!
+1 to Ian's point, scheduled functions https://docs.convex.dev/scheduling/scheduled-functions are pretty ideal for interacting with rate-limited APIs: you can store partial progress in the database and decide on the fly how far in the future to schedule the next invocation.
You can sleep with
await new Promise(r => setTimeout(r, 1000))
in an action that runs in Node.js, but there's still a limit of a couple minutes clock time. So better to use scheduled functions once your loop might take longer than that. You'll be able to see partial progress in the dashboard better this way too.oh okay awesome thanks