Is there a way to run a task in rust, a thread at best, at a specific time or in an interval again and again?
So that I can run my function every 5 minutes or every day at 12 o'clock.
In Java there is the TimerTask, so I'm searching for something like that.
You can use
Timer::periodic
to create a channel that gets sent a message at regular intervals, e.g.Receiver::iter
blocks, waiting for the next message, and those messages are 5 minutes apart, so the body of thefor
loop is run at those regular intervals. NB. this will use a whole thread for that single function, but I believe one can generalise to any fixed number of functions with different intervals by creating multiple timer channels and usingselect!
to work out which function should execute next.I'm fairly sure that running every day at a specified time, correctly, isn't possible with the current standard library. E.g. using a simple
Timer::periodic(Duration::days(1))
won't handle the system clock changing, e.g. when the user moves timezones, or goes in/out of daylight savings.For the latest Rust nightly-version: