Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
feat(src/pages.rs): add error variant for Mutex::lock failing
Lars Wirzenius committed 1 year ago
commit 2104c1a6711aaa7a50ad4d8224378547df6aa313
parent 4acabd5f559c2abe0ca9f206070bbbdb3f6d91bd
1 file changed +18 -10
modified 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<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()
        };