mvniekerk / tokio-cron-scheduler

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

no method named `add` found for opaque type `impl futures::Future<Output = std::result::Result<JobScheduler, JobSchedulerError>>` in the current scope #37

Closed aldanchenko closed 1 year ago

aldanchenko commented 2 years ago

Hello,

I'm trying to use tokio-cron-scheduler in my project. But receiving this error after compilation

error[E0599]: no method named `add` found for opaque type `impl futures::Future<Output = std::result::Result<JobScheduler, JobSchedulerError>>` in the current scope

My code is next

let mut job_scheduler = JobScheduler::new();
job_scheduler.add(Job::new_async(cron_time_str, |uuid, mut l| Box::pin( async {

Maybe you could help with this?

Thanks in advance!

Alex

sudo-jaa commented 2 years ago

Hey @aldanchenko, I was just having a play around with this and ran into a similar issue when copying the example from the docs.

Your issue is that JobScheduler::new(); is returning a future, so you'll need to await it and then unwrap() or match to get the JobSchedulerLocked which is where the add function is actually implemented. I managed to get this working with the following snippet. Hopefully it helps you too.

#[tokio::main]
async fn main() {
    let mut scheduler = JobScheduler::new().await;

    match scheduler {
        Ok(mut sched) => {
            let job = Job::new("1/1 * * * * *", |uuid, l| {
                println!("Running every second");
            }).unwrap();
            sched.add(job).await.unwrap();
            // ...

This is just after a few minutes of playing around, so there might be a more idiomatic approach that someone can suggest!

mvniekerk commented 1 year ago

Hi @aldanchenko Thanks for the report, I've updated the README. Also thanks @sudo-jaa for helping.