Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-threading of Jobs #85

Open
abhi3700 opened this issue Sep 28, 2024 · 0 comments
Open

Multi-threading of Jobs #85

abhi3700 opened this issue Sep 28, 2024 · 0 comments

Comments

@abhi3700
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant