From 49a8450c5e52fc36ac5a26aea4bfdc0dd1e75388 Mon Sep 17 00:00:00 2001 From: Paul-Nicolas Madelaine Date: Sat, 30 Sep 2023 16:36:07 +0200 Subject: [PATCH] server: error management --- typhon/src/error.rs | 21 +++++++++++++++++---- typhon/src/evaluations.rs | 13 +++++++------ typhon/src/jobs.rs | 12 +++++++----- typhon/src/jobsets.rs | 2 +- typhon/src/tasks.rs | 6 ++++++ 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/typhon/src/error.rs b/typhon/src/error.rs index 095800c1..d5fb55ef 100644 --- a/typhon/src/error.rs +++ b/typhon/src/error.rs @@ -1,6 +1,7 @@ use crate::actions; use crate::handles; use crate::nix; +use crate::tasks; #[derive(Debug)] pub enum Error { @@ -20,13 +21,17 @@ pub enum Error { Todo, UnexpectedDatabaseError(diesel::result::Error), LoginError, + TaskError(tasks::Error), } impl Error { pub fn is_internal(&self) -> bool { use Error::*; match self { - ActionError(actions::Error::Unexpected) | UnexpectedDatabaseError(_) | Todo => true, + ActionError(actions::Error::Unexpected) + | UnexpectedDatabaseError(_) + | TaskError(_) + | Todo => true, _ => false, } } @@ -68,6 +73,7 @@ impl std::fmt::Display for Error { LoginError => write!(f, "Login error"), Todo => write!(f, "Unspecified error"), UnexpectedDatabaseError(e) => write!(f, "Database error: {}", e), + TaskError(e) => write!(f, "Task error: {}", e), } } } @@ -90,13 +96,20 @@ impl From for Error { } } +impl From for Error { + fn from(e: tasks::Error) -> Error { + Error::TaskError(e) + } +} + impl Into for Error { fn into(self) -> typhon_types::responses::ResponseError { use {typhon_types::responses::ResponseError::*, Error::*}; match self { - ActionError(actions::Error::Unexpected) | UnexpectedDatabaseError(_) | Todo => { - InternalError - } + ActionError(actions::Error::Unexpected) + | UnexpectedDatabaseError(_) + | TaskError(_) + | Todo => InternalError, EvaluationNotFound(_) | JobNotFound(_) | JobsetNotFound(_) | ProjectNotFound(_) => { ResourceNotFound(format!("{}", self)) } diff --git a/typhon/src/evaluations.rs b/typhon/src/evaluations.rs index 4d5e9f49..cd052ec4 100644 --- a/typhon/src/evaluations.rs +++ b/typhon/src/evaluations.rs @@ -46,7 +46,7 @@ async fn evaluate_aux(id: i32, new_jobs: nix::NewJobs) -> Result<(), Error> { drop(conn); for job in created_jobs.into_iter() { - job.run().await; + job.run().await?; } Ok(()) @@ -116,12 +116,11 @@ impl Evaluation { .first::(&mut *conn)?) } - pub async fn run(self) -> () { + pub async fn run(self) -> Result<(), Error> { use handles::Log::*; - // TODO: error management - let handle = self.handle().await.unwrap(); - let jobset = self.jobset().await.unwrap(); + let handle = self.handle().await?; + let jobset = self.jobset().await?; let id = self.evaluation_id; let task = async move { nix::eval_jobs(&self.evaluation_url_locked, jobset.jobset_legacy).await }; @@ -154,6 +153,8 @@ impl Evaluation { log_event(Event::EvaluationFinished(handle)); }; - EVALUATIONS.run(id, task, f).await; + EVALUATIONS.run(id, task, f).await?; + + Ok(()) } } diff --git a/typhon/src/jobs.rs b/typhon/src/jobs.rs index 55eb5b9a..a094e0f6 100644 --- a/typhon/src/jobs.rs +++ b/typhon/src/jobs.rs @@ -105,13 +105,13 @@ impl Job { })) } - pub async fn run(self) -> () { + pub async fn run(self) -> Result<(), Error> { use crate::time::now; let id = self.job_id; let drv = nix::DrvPath::new(&self.job_build_drv); // FIXME? - let handle_1 = self.handle().await.unwrap(); + let handle_1 = self.handle().await?; let handle_2 = handle_1.clone(); let handle_3 = handle_1.clone(); let handle_4 = handle_1.clone(); @@ -175,7 +175,7 @@ impl Job { drop(conn); log_event(Event::JobUpdated(handle_2)); }; - JOBS_BEGIN.run(id, task_begin, finish_begin).await; + JOBS_BEGIN.run(id, task_begin, finish_begin).await?; // FIXME: write a more intelligent build manager let (sender, receiver) = tokio::sync::oneshot::channel::(); @@ -205,7 +205,7 @@ impl Job { drop(conn); log_event(Event::JobUpdated(handle_3)); }; - JOBS_BUILD.run(id, task_build, finish_build).await; + JOBS_BUILD.run(id, task_build, finish_build).await?; let task_end = async move { // wait for `begin` to finish @@ -267,6 +267,8 @@ impl Job { drop(conn); log_event(Event::JobUpdated(handle_5)); }; - JOBS_END.run(id, task_end, finish_end).await; + JOBS_END.run(id, task_end, finish_end).await?; + + Ok(()) } } diff --git a/typhon/src/jobsets.rs b/typhon/src/jobsets.rs index e88089de..a23450ee 100644 --- a/typhon/src/jobsets.rs +++ b/typhon/src/jobsets.rs @@ -59,7 +59,7 @@ impl Jobset { let handle = evaluation.handle().await?; log_event(Event::EvaluationNew(handle.clone())); - evaluation.run().await; + evaluation.run().await?; Ok(handle) } diff --git a/typhon/src/tasks.rs b/typhon/src/tasks.rs index 97967d92..a76a90ce 100644 --- a/typhon/src/tasks.rs +++ b/typhon/src/tasks.rs @@ -9,6 +9,12 @@ pub enum Error { ShuttingDown, } +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "Task manager is shutting down") + } +} + #[derive(Debug)] struct TaskHandle { canceler: Option>,