diff --git a/src/pages.rs b/src/pages.rs index 7ba85dae..321ae624 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -48,6 +48,9 @@ pub enum PageError { #[error(transparent)] Db(#[from] DbError), + + #[error("failed to lock page data structure")] + Lock(&'static str), } /// A builder for constructing a [`StatusPage`] value. It will only @@ -459,33 +462,38 @@ impl StatusPage { } } - fn lock(&mut self) -> MutexGuard { - self.data.lock().expect("lock StatusPage::data") + fn lock(&mut self) -> Result, PageError> { + self.data + .lock() + .map_err(|_| PageError::Lock("lock StatusPage::data")) } pub fn set_output_dir(&mut self, dirname: &Path) { self.dirname = Some(dirname.into()); } - pub fn update_timestamp(&mut self) { - let mut data = self.lock(); + pub fn update_timestamp(&mut self) -> Result<(), PageError> { + let mut data = self.lock()?; data.timestamp = now(); + Ok(()) } - pub fn broker_event(&mut self, event: &BrokerEvent) { - let mut data = self.lock(); + pub fn broker_event(&mut self, event: &BrokerEvent) -> Result<(), PageError> { + let mut data = self.lock()?; data.latest_broker_event = Some(event.clone()); data.broker_event_counter += 1; + Ok(()) } /// Add a new CI run to the status page. - pub fn push_run(&mut self, new: Run) { + pub fn push_run(&mut self, new: Run) -> Result<(), PageError> { // We silently ignore a run until its id has been set. if let Some(id) = new.adapter_run_id() { - let mut data = self.lock(); + let mut data = self.lock()?; data.latest_ci_run = Some(new.clone()); data.runs.insert(id.clone(), new); } + Ok(()) } pub fn update_in_thread( @@ -532,7 +540,7 @@ impl StatusPage { // We avoid writing while keeping the lock, to reduce // contention. let (status, repos) = { - let data = self.lock(); + let data = self.lock()?; let status = data.status_page_as_html().to_string(); @@ -562,7 +570,7 @@ impl StatusPage { // We avoid writing while keeping the lock, to reduce // contention. let status = { - let data = self.lock(); + let data = self.lock()?; StatusData::from(&*data).as_json() };