I am building a small app that's supposed to schedule two tasks (based on rusoto
AWS SDK) at different intervals: every X seconds, run one task, and every Y seconds, run the other.
I found crate crossbeam
that offers a tick timer and a select!
macro and put them all together like this:
fn main() -> Result<(), Error> {
let cloudwatch_client = rusoto_cloudwatch::CloudWatchClient::new();
let rt = Runtime::new().unwrap();
let tick_a = tick(Duration::from_secs(60));
let tick_b = tick(Duration::from_secs(30));
loop {
select! {
recv(tick_a) -> _ => {
rt.block_on(cloudwatch_client.put_metric_data( /* ... */ ));
},
/* similar for tick_b */
}
}
}
This compiles, however the program panics with thread 'main' panicked at 'not currently running on the Tokio runtime.'
. This seems to come from the Rusoto call, by analyzing the backtrace.
What am I missing here? Is there a way to make this work? Is there a better way to handle the scheduling of tasks at intervals in Rust?
Please note that this question does not seem to address my issue. That question starts out by using futures::executor::block_on
function and is solved by using the block_on
method implemented by the tokio Runtime
. I am already using the block_on
method in Runtime
.