this post was submitted on 18 Sep 2023
7 points (73.3% liked)
Rust Programming
8161 readers
21 users here now
founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
How would you "just run" a task every 30 minutes? Every 5 hours? Once a day?
Works pretty well. Maybe add a bit of code to crash the whole process on panic or some other logging. Wastes a few KiB of memory per loop but probably not a major issue. Doing this with async will waste only the tiniest amount of memory.
@kevincox How do you stop the job? Do you use channels like in Go?
It depends. Sometimes you can just put an exit call at the end of
main
to kill the thread. If you want to attempt graceful shutdown then usually I just use a booleanshutdown
flag. Then the loop becomeswhile !shutdown.get() {
what if one of the calls crashes? how would you re-run it?
Best option is probably to add a wrapper around the thread that re-spawns it. But you can also just catch panics in the loop.
How would you re-run it multiple times then? An internal should be progressively greater. How would you terminate it if it continues to produce an exception?