Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DanNixon committed May 9, 2024
1 parent 97a8db1 commit e982652
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions client/src/announcer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ impl Announcer {

pub async fn poll(&mut self) -> crate::Result<AnnouncerPollResult> {
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(
Expand All @@ -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) => {
Expand All @@ -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);
Expand All @@ -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()
}
}
}

Expand Down

0 comments on commit e982652

Please sign in to comment.