lholden / job_scheduler

A simple cron-like job scheduling library for Rust.
Apache License 2.0
196 stars 34 forks source link

`dyn std::ops::FnMut()` cannot be sent between threads safely #20

Open alopatindev opened 4 years ago

alopatindev commented 4 years ago

Thanks for this crate. It'd be handy to use it in async programs.

run: Box<dyn (FnMut() -> ()) + 'a> causes

`dyn std::ops::FnMut()` cannot be sent between threads safely

in the following example:

use job_scheduler::{Job, JobScheduler};
use std::time::Duration;
use tokio::time::delay_for;

#[tokio::main]
async fn main() {
    let mut scheduler = JobScheduler::new();
    scheduler.add(Job::new("1/1 * * * * * *".parse().unwrap(), || {
        dbg!("hello");
    }));
    tokio::spawn(async move {
        loop {
            scheduler.tick();
            delay_for(Duration::from_millis(500)).await;
        }
    })
    .await
    .unwrap();
}
[package]
name = "hello_job_scheduler"
version = "0.1.0"
edition = "2018"

[dependencies]
job_scheduler = "1"
tokio = { version = "0.2", features = ["rt-threaded", "macros", "time"]}