From 82170122378bd0e548d2f7882f0e39c20223e214 Mon Sep 17 00:00:00 2001 From: asonix Date: Sun, 4 Feb 2024 22:19:41 -0600 Subject: [PATCH] Add type Error to Job --- examples/basic-example/src/main.rs | 1 + examples/error-example/Cargo.toml | 2 +- examples/error-example/src/main.rs | 7 ++++--- examples/long-example/src/main.rs | 2 ++ examples/managed-example/src/main.rs | 2 ++ examples/metrics-example/src/main.rs | 1 + examples/panic-example/src/main.rs | 2 ++ examples/postgres-example/src/main.rs | 1 + examples/tokio-example/src/main.rs | 1 + jobs-core/src/job.rs | 9 ++++++--- jobs-core/src/lib.rs | 6 ++---- jobs-core/src/unsend_job.rs | 9 ++++++--- 12 files changed, 29 insertions(+), 14 deletions(-) diff --git a/examples/basic-example/src/main.rs b/examples/basic-example/src/main.rs index ae28c10..ae33766 100644 --- a/examples/basic-example/src/main.rs +++ b/examples/basic-example/src/main.rs @@ -84,6 +84,7 @@ impl MyJob { impl Job for MyJob { type State = MyState; + type Error = Error; type Future = Ready>; type Spawner = Spawner; diff --git a/examples/error-example/Cargo.toml b/examples/error-example/Cargo.toml index 4f3cc7f..4e66133 100644 --- a/examples/error-example/Cargo.toml +++ b/examples/error-example/Cargo.toml @@ -11,8 +11,8 @@ actix-rt = "2.0.0" anyhow = "1.0" background-jobs = { version = "0.17.0", path = "../..", features = [ "error-logging", + "sled", ] } -background-jobs-sled-storage = { version = "0.10.0", path = "../../jobs-sled" } time = "0.3" tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } diff --git a/examples/error-example/src/main.rs b/examples/error-example/src/main.rs index a4a95ea..5aed057 100644 --- a/examples/error-example/src/main.rs +++ b/examples/error-example/src/main.rs @@ -1,7 +1,6 @@ use actix_rt::Arbiter; use anyhow::Error; -use background_jobs::{Job, MaxRetries, WorkerConfig}; -use background_jobs_sled_storage::Storage; +use background_jobs::{actix::WorkerConfig, sled::Storage, Job, MaxRetries}; use std::{ future::{ready, Ready}, time::{Duration, SystemTime}, @@ -88,6 +87,7 @@ impl MyJob { impl Job for MyJob { type State = MyState; + type Error = Error; type Future = Ready>; // The name of the job. It is super important that each job has a unique name, @@ -117,6 +117,7 @@ impl Job for MyJob { impl Job for ErroringJob { type State = MyState; + type Error = Error; type Future = Ready>; const NAME: &'static str = "ErroringJob"; @@ -126,6 +127,6 @@ impl Job for ErroringJob { const MAX_RETRIES: MaxRetries = MaxRetries::Count(0); fn run(self, _: MyState) -> Self::Future { - std::future::ready(Err(anyhow::anyhow!("boom"))) + ready(Err(anyhow::anyhow!("boom"))) } } diff --git a/examples/long-example/src/main.rs b/examples/long-example/src/main.rs index a23b885..6e9568b 100644 --- a/examples/long-example/src/main.rs +++ b/examples/long-example/src/main.rs @@ -90,6 +90,7 @@ impl MyJob { impl Job for MyJob { type State = MyState; + type Error = Error; type Future = Ready>; type Spawner = Spawner; @@ -120,6 +121,7 @@ impl Job for MyJob { impl Job for LongJob { type State = MyState; + type Error = Error; type Future = Pin>>>; type Spawner = Spawner; diff --git a/examples/managed-example/src/main.rs b/examples/managed-example/src/main.rs index c5c912e..1b974c3 100644 --- a/examples/managed-example/src/main.rs +++ b/examples/managed-example/src/main.rs @@ -102,6 +102,7 @@ impl MyJob { impl Job for MyJob { type State = MyState; + type Error = Error; type Future = Ready>; type Spawner = Spawner; @@ -132,6 +133,7 @@ impl Job for MyJob { impl Job for StopJob { type State = MyState; + type Error = Error; type Future = Ready>; type Spawner = Spawner; diff --git a/examples/metrics-example/src/main.rs b/examples/metrics-example/src/main.rs index e5c966a..327d520 100644 --- a/examples/metrics-example/src/main.rs +++ b/examples/metrics-example/src/main.rs @@ -89,6 +89,7 @@ impl MyJob { impl Job for MyJob { type State = MyState; + type Error = Error; type Future = Pin> + 'static>>; type Spawner = Spawner; diff --git a/examples/panic-example/src/main.rs b/examples/panic-example/src/main.rs index effa604..523742d 100644 --- a/examples/panic-example/src/main.rs +++ b/examples/panic-example/src/main.rs @@ -83,6 +83,7 @@ impl MyJob { impl Job for MyJob { type State = MyState; + type Error = Error; type Future = Ready>; // The name of the job. It is super important that each job has a unique name, @@ -112,6 +113,7 @@ impl Job for MyJob { impl Job for PanickingJob { type State = MyState; + type Error = Error; type Future = Ready>; const NAME: &'static str = "PanickingJob"; diff --git a/examples/postgres-example/src/main.rs b/examples/postgres-example/src/main.rs index 6d0e03a..8175f13 100644 --- a/examples/postgres-example/src/main.rs +++ b/examples/postgres-example/src/main.rs @@ -90,6 +90,7 @@ impl MyJob { impl Job for MyJob { type State = MyState; + type Error = anyhow::Error; type Future = Ready>; type Spawner = Spawner; diff --git a/examples/tokio-example/src/main.rs b/examples/tokio-example/src/main.rs index 03cb1ce..9261948 100644 --- a/examples/tokio-example/src/main.rs +++ b/examples/tokio-example/src/main.rs @@ -79,6 +79,7 @@ impl MyJob { impl Job for MyJob { type State = MyState; + type Error = Error; type Future = Ready>; // The name of the job. It is super important that each job has a unique name, diff --git a/jobs-core/src/job.rs b/jobs-core/src/job.rs index 0ff5022..2d0bc71 100644 --- a/jobs-core/src/job.rs +++ b/jobs-core/src/job.rs @@ -48,8 +48,11 @@ pub trait Job: Serialize + DeserializeOwned + 'static { /// The application state provided to this job at runtime. type State: Clone + 'static; + /// The error type this job returns + type Error: Into>; + /// The future returned by this job - type Future: Future> + Send; + type Future: Future> + Send; /// The name of the job /// @@ -176,9 +179,9 @@ where let (fut, span) = res?; if let Some(span) = span { - fut.instrument(span).await?; + fut.instrument(span).await.map_err(Into::into)?; } else { - fut.await?; + fut.await.map_err(Into::into)?; } Ok(()) diff --git a/jobs-core/src/lib.rs b/jobs-core/src/lib.rs index abc5223..d1943c7 100644 --- a/jobs-core/src/lib.rs +++ b/jobs-core/src/lib.rs @@ -6,8 +6,6 @@ //! This crate shouldn't be depended on directly, except in the case of implementing a custom jobs //! processor. For a default solution based on Actix and Sled, look at the `background-jobs` crate. -use anyhow::Error; - mod catch_unwind; mod job; mod job_info; @@ -28,8 +26,8 @@ pub use unsend_job::{JoinError, UnsendJob, UnsendSpawner}; /// The error type returned by the `process` method pub enum JobError { /// Some error occurred while processing the job - #[error("Error performing job")] - Processing(#[from] Error), + #[error("{0}")] + Processing(#[from] Box), /// Creating a `Job` type from the provided `serde_json::Value` failed #[error("Could not make JSON value from arguments")] diff --git a/jobs-core/src/unsend_job.rs b/jobs-core/src/unsend_job.rs index 6aefc52..54564fa 100644 --- a/jobs-core/src/unsend_job.rs +++ b/jobs-core/src/unsend_job.rs @@ -1,5 +1,4 @@ use crate::{Backoff, Job, MaxRetries}; -use anyhow::Error; use serde::{de::DeserializeOwned, ser::Serialize}; use std::{ fmt::Debug, @@ -45,10 +44,13 @@ pub trait UnsendJob: Serialize + DeserializeOwned + 'static { /// The application state provided to this job at runtime. type State: Clone + 'static; + /// The error type this job returns + type Error: Into> + Send; + /// The future returned by this job /// /// Importantly, this Future does not require Send - type Future: Future>; + type Future: Future>; /// The spawner type that will be used to spawn the unsend future type Spawner: UnsendSpawner; @@ -148,7 +150,8 @@ where T: UnsendJob, { type State = T::State; - type Future = UnwrapFuture<::Handle>>; + type Error = T::Error; + type Future = UnwrapFuture<::Handle>>; const NAME: &'static str = ::NAME; const QUEUE: &'static str = ::QUEUE;