Skip to content

Inconsistent scheduling with custom scheduler thread #16

Closed
@msrd0

Description

I'm trying to have a somewhat accurate yet flexible scheduler (it seems like I'd need to replace the scheduler every time I update it), so I came up with some test code like this:

lazy_static! {
        static ref SCHEDULER: Mutex<Scheduler> = Mutex::new(Scheduler::new());
}

fn init_scheduler() {
        let mut sched = SCHEDULER.try_lock().expect("Mutex was locked after initialization");
        sched.every(5.seconds()).run(|| info!("Hello World"));
}

fn main() {
        pretty_env_logger::init_timed();

        init_scheduler();

        info!("Scheduler started");
        loop {
                let mut nanos = 1_100_000_000 - (Local::now().nanosecond() as u64);
                thread::sleep(Duration::from_nanos(nanos));
                match SCHEDULER.try_lock() {
                        Ok(mut sched) => sched.run_pending(),
                        Err(err) => warn!("Unable to run scheduler: {}", err)
                };
        }
}

I explicitly decided to align the scheduler calls to every second because even with just calling a logger, I got varying time intervals when just sleeping a second instead. However, this code made it absolutely worse as I now get 4, 5 and 6 second intervals randomly instead of a clean 5 second interval that I expected.

Since your scheduler seems to align everything to the next minute/hour/day, I'm supprised by this behaviour. Now, one second offset is not very bad, but I was hoping that for a schedule every 10 minutes and one every half our, I'd get away with calling the scheduler every 10 minutes, but having the interval fluctuate to 20 minutes is not acceptable.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions