mvniekerk / tokio-cron-scheduler

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

Improve code quality #65

Open JakkuSakura opened 2 months ago

JakkuSakura commented 2 months ago

Hi, thank's for the effort being done.

Recently, I hit an issue that if scheduler.spawn() is called in the code, without actual tasks, my server blocks.

I started reviewing code in this crate. I noticed the following points to improve

  1. use of Arc<Mutex<bool>> can be written as Arc<AtomicBool>
  2. use of
    tokio::spawn(async move {
    if let Err(e) = tx.send(uuid) {
        error!("Error sending deletion {:?}", e);
    }
    });

    can be replaced with just tx.send()

  3. use of
    let mut start_rx: Option<Receiver<bool>> = None;
    std::mem::swap(&mut start_rx, &mut *w);
    start_rx

    can be replaced as w.take()

haydenflinner commented 1 month ago

Interestingly, the ::init function is not mentioned in the example of how to use the scheduler, it gets called implicitly when adding a job. But we don't use atomic swap for writing to inited, just acquire a lock and then write true, so it seems that if someone had a few clones of the JobScheduler (uninitted) and called add on them at the same time, the actors may get started twice. Seems to be likely harmless, though.