Improve error handling, logging

This commit is contained in:
2026-01-18 00:02:11 +02:00
parent 125ddc3117
commit 33de9720bf
11 changed files with 211 additions and 109 deletions

View File

@@ -3,6 +3,7 @@ use crate::{
resident::ResidentId,
schedule::MonthlySchedule,
slot::Slot,
timer::Timer,
workload::{WorkloadBounds, WorkloadTracker},
};
@@ -11,25 +12,40 @@ use rand::Rng;
pub struct Scheduler {
pub config: UserConfig,
pub bounds: WorkloadBounds,
pub timer: Timer,
}
impl Scheduler {
pub fn new(config: UserConfig, bounds: WorkloadBounds) -> Self {
Self { config, bounds }
Self {
config,
bounds,
timer: Timer::default(),
}
}
pub fn new_with_config(config: UserConfig) -> Self {
let bounds = WorkloadBounds::new_with_config(&config);
Self { config, bounds }
Self {
config,
bounds,
timer: Timer::default(),
}
}
pub fn run(&self, schedule: &mut MonthlySchedule, tracker: &mut WorkloadTracker) -> bool {
pub fn run(
&self,
schedule: &mut MonthlySchedule,
tracker: &mut WorkloadTracker,
) -> anyhow::Result<bool> {
schedule.prefill(&self.config);
for (slot, res_id) in schedule.0.iter() {
tracker.insert(*res_id, &self.config, *slot);
}
//TODO: add validation
self.search(schedule, tracker, Slot::default())
}
@@ -41,15 +57,19 @@ impl Scheduler {
schedule: &mut MonthlySchedule,
tracker: &mut WorkloadTracker,
slot: Slot,
) -> bool {
) -> anyhow::Result<bool> {
if self.timer.limit_exceeded() {
anyhow::bail!("Time exceeded. Restrictions too tight");
}
if !slot.is_first()
&& schedule.restrictions_violated(&slot.previous(), &self.config, &self.bounds, tracker)
{
return false;
return Ok(false);
}
if slot.greater_than(self.config.total_days) {
return tracker.are_all_thresholds_met(&self.config, &self.bounds);
return Ok(tracker.are_all_thresholds_met(&self.config, &self.bounds));
}
if schedule.is_slot_manually_assigned(&slot) {
@@ -68,15 +88,15 @@ impl Scheduler {
schedule.insert(slot, id);
tracker.insert(id, &self.config, slot);
if self.search(schedule, tracker, slot.next()) {
return true;
if self.search(schedule, tracker, slot.next())? {
return Ok(true);
}
schedule.remove(slot);
tracker.remove(id, &self.config, slot);
}
false
Ok(false)
}
/// Return all valid residents for the current slot
@@ -150,7 +170,7 @@ mod tests {
mut tracker: WorkloadTracker,
scheduler: Scheduler,
) {
assert!(scheduler.run(&mut schedule, &mut tracker));
assert!(scheduler.run(&mut schedule, &mut tracker).is_ok());
for d in 1..=scheduler.config.total_days {
let day = Day(d);