diff --git a/client/src/announcer/mod.rs b/client/src/announcer/mod.rs index 3dba0c6..bd88205 100644 --- a/client/src/announcer/mod.rs +++ b/client/src/announcer/mod.rs @@ -105,9 +105,11 @@ impl Announcer { pub async fn poll(&mut self) -> crate::Result { loop { + // Calculate when the next schedule refresh is due let next_schedule_update_time = self.schedule_timestamp + self.settings.schedule_refresh; + // Determine what the next event to announce is and in how much time it is due to be announced let next_event = self.get_next_event_to_announce(); let event_wait_time = match next_event { Some(ref event) => self::utils::get_duration_before_event( @@ -119,7 +121,9 @@ impl Announcer { }; debug!("Time to wait before next event: {:?}", event_wait_time); + // Wait for one of several things to happen... tokio::select! { + // 1. The schedule is refreshed at the requested interval _ = tokio::time::sleep_until(next_schedule_update_time) => { match self.update_schedule().await { Ok(changes) => { @@ -130,6 +134,7 @@ impl Announcer { }, } } + // 2. The next event to be announced needs to be announced _ = tokio::time::sleep(event_wait_time) => { if let Some(event) = next_event { self.update_event_marker(&event); @@ -149,16 +154,26 @@ impl Announcer { .iter() .position(|event| marker.matches(event)) { - Some(idx) => self.schedule.events.get(idx + 1).cloned(), - None => self - .schedule - .events - .iter() - .find(|event| event.start > marker.start) - .cloned(), + Some(idx) => { + debug!("Matched last notified event marker, picking next in schedule as next to announce"); + self.schedule.events.get(idx + 1).cloned() + } + None => { + debug!("Last notified event marker matched no events (something's fucky...), picking next chronological event as next to announce"); + self.schedule + .events + .iter() + .find(|event| event.start > marker.start) + .cloned() + } } } - None => self.schedule.events.first().cloned(), + None => { + debug!( + "No last notified event marker, picking first in schedule as next to announce" + ); + self.schedule.events.first().cloned() + } } }