cancel scheduled shsutdowns upon reactivation

This commit is contained in:
LastExceed 2024-02-13 22:58:34 +01:00
parent cdfd93674b
commit b497436ec4

View File

@ -151,11 +151,13 @@ impl State<Listener> {
.filter(|slave| if SLAVE_PROPERTIES[slave][index::RUNS_CONTINUOUSLY] { long_slaves } else { short_slaves }); .filter(|slave| if SLAVE_PROPERTIES[slave][index::RUNS_CONTINUOUSLY] { long_slaves } else { short_slaves });
for slave in slaves_to_update { for slave in slaves_to_update {
if !power && SLAVE_PROPERTIES[&slave][index::NEEDS_TRAILING_TIME] { if SLAVE_PROPERTIES[&slave][index::NEEDS_TRAILING_TIME] {
dark_grey_ln!("scheduling delayed shutdown for {}", slave); if power {
let shutdown_timestamp = Instant::now() + Duration::from_secs(30); self.cancel_scheduled_shutdown(&slave).await;
self.scheduled_shutdowns.write().await.push_back((shutdown_timestamp, slave)); } else {
continue; self.schedule_shutdown(slave).await;
continue;
}
} }
self.set_power_state(&slave, power).await; self.set_power_state(&slave, power).await;
@ -163,4 +165,24 @@ impl State<Listener> {
Ok(()) Ok(())
} }
async fn schedule_shutdown(&self, slave: String) {
dark_grey_ln!("scheduling delayed shutdown for {}", slave);
let shutdown_timestamp = Instant::now() + Duration::from_secs(30);
self.scheduled_shutdowns
.write()
.await
.push_back((shutdown_timestamp, slave));
}
async fn cancel_scheduled_shutdown(&self, slave: &String) {
let mut schedule = self.scheduled_shutdowns.write().await;
if let Some(index) = schedule.iter().position(|(_, name)| name == slave) {
dark_grey_ln!("cancelling scheduling shutdown for {}", slave);
schedule.remove(index);
}
}
} }