Skip to content

Commit

Permalink
server: error management
Browse files Browse the repository at this point in the history
  • Loading branch information
pnmadelaine committed Oct 1, 2023
1 parent ee13c9e commit 49a8450
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
21 changes: 17 additions & 4 deletions typhon/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::actions;
use crate::handles;
use crate::nix;
use crate::tasks;

#[derive(Debug)]
pub enum Error {
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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),
}
}
}
Expand All @@ -90,13 +96,20 @@ impl From<actions::Error> for Error {
}
}

impl From<tasks::Error> for Error {
fn from(e: tasks::Error) -> Error {
Error::TaskError(e)
}
}

impl Into<typhon_types::responses::ResponseError> 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))
}
Expand Down
13 changes: 7 additions & 6 deletions typhon/src/evaluations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -116,12 +116,11 @@ impl Evaluation {
.first::<Jobset>(&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 };
Expand Down Expand Up @@ -154,6 +153,8 @@ impl Evaluation {

log_event(Event::EvaluationFinished(handle));
};
EVALUATIONS.run(id, task, f).await;
EVALUATIONS.run(id, task, f).await?;

Ok(())
}
}
12 changes: 7 additions & 5 deletions typhon/src/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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::<String>();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(())
}
}
2 changes: 1 addition & 1 deletion typhon/src/jobsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions typhon/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sender<()>>,
Expand Down

0 comments on commit 49a8450

Please sign in to comment.