Closed ev94 closed 2 years ago
Hi @ev94
Thanks for the bug report. Did this code compile at all? You're missing the cron text for the Job::new_async(
Hi @mvniekerk, Thanks for your reply. Sorry my code sample was incorrect. The cron text is in it.
tokio::spawn(async move {
info!("spawning cron job");
let sched = JobScheduler::new().expect("failed to create job scheduler");
let _ = sched.add(
Job::new_async(
app_config.load_data_cron.as_str(),
move |_uuid, _l| {
Box::pin(async move {
info!("job started);
})
},
).expect("failed to create async job"),
);
if let Err(e) = sched.start() {
error!("failed to start scheduler: {:?}", e);
}
});
Hi @ev94 I could replicate the problem. For some reason it seems like init-ing the scheduler inside a tokio::spawn causes a block on the async-sync code.
I've reached out to the Rust Reddit community on this. I don't know how to resolve this issue apart from marking all the functions as async fn's and then breaking the API of this library.
Hi @mvniekerk Thanks for your feedback. So a possible solution is to init the scheduler outside of the tokio::spawn and then to move it to the spawn and start it?
let sched = JobScheduler::new().expect("failed to create job scheduler");
let _ = sched.add(
Job::new_async(
app_config.load_data_cron.as_str(),
move |_uuid, _l| {
Box::pin(async move {
info!("job started);
})
},
).expect("failed to create async job"),
);
tokio::spawn(async move {
info!("starting cron jobs");
if let Err(e) = sched.start() {
error!("failed to start scheduler: {:?}", e);
}
});
Hi @ev94 Please check 0.8.0. I've made an example that covers your use case. Note that the API did change accordingly
Hi,
Thx for the great work. We just update to 0.7.2. The call to JobScheduler::new() in the code below fails with a CantInit error. Are we missing something?
Thx for your help Emmanuel