mvniekerk / tokio-cron-scheduler

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

Multi-threading of Jobs #85

Open abhi3700 opened 1 month ago

abhi3700 commented 1 month ago

This is my code:

#[tokio::main(flavor = "multi_thread")]
async fn main() -> eyre::Result<()> {
    // ... existing code ...

    // Create a shared JobScheduler instance
    let sched = JobScheduler::new().await?;
    let sched = Arc::new(sched);

    // Add a job that runs every 10 seconds
    sched
        .add(Job::new("1/10 * * * * *", |_uuid, _l| {
            println!("I run every 10 seconds");
        })?)
        .await?;

    // Add an async job that runs every 1 minute
    sched
        .add(Job::new_async("every 1 minutes", move |_uuid, _l| {
            Box::pin(async move {
                println!("I run every 1 minutes");
                let _ = perform_task_1().await;
            })
        })?)
        .await?;

    // Start the scheduler in a separate task
    tokio::spawn(async move {
        tracing::info!("Starting scheduler");
        let _ = sched.start().await;
    });

    // ... existing code ...
}

Here, I am starting the scheduler only once for 2 scheduled jobs. And using tokio::spawn() only once. Can someone confirm if my jobs would run in separate threads each time?