mvniekerk / tokio-cron-scheduler

Schedule tasks on Tokio using cron-like annotation
Apache License 2.0
453 stars 54 forks source link

JobScheduler::new() - CantInit Error #27

Closed ev94 closed 1 year ago

ev94 commented 2 years ago

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

tokio::spawn(async move {
    info!("spawning cron job");
    let sched = JobScheduler::new().expect("failed to create job scheduler");
    let _ = sched.add(
        Job::new_async(
            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);
    }
});
mvniekerk commented 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(, ) code.

ev94 commented 2 years ago

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);
    }
});
mvniekerk commented 2 years ago

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.

mvniekerk commented 2 years ago

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.

mvniekerk commented 2 years ago

https://www.reddit.com/r/rust/comments/vvn5fp/tokio_async_sync_bridging_causes_block/ FYI

ev94 commented 2 years ago

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);
    }
});
mvniekerk commented 1 year ago

Hi @ev94 Please check 0.8.0. I've made an example that covers your use case. Note that the API did change accordingly