feat(src/pages.rs): add error variant for Mutex::lock failing

This means we do not panic if the locking fails. Instead we return an
error that will hopefully be logged and thus visible to the user in a
way that is easier to deal with than a panic

Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
Lars Wirzenius
2024-07-29 12:57:16 +03:00
parent 4acabd5f55
commit 2104c1a671
+18 -10
View File
@@ -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<PageData> {
self.data.lock().expect("lock StatusPage::data")
fn lock(&mut self) -> Result<MutexGuard<PageData>, 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()
};