From db errors (#77)

* Re-work errors

* delete aliases
This commit is contained in:
Pmarquez 2022-08-31 17:45:13 +00:00 committed by GitHub
parent 076d89e866
commit b975e893e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 101 additions and 72 deletions

View file

@ -5,6 +5,5 @@ pub mod async_worker_pool;
pub use async_queue::*; pub use async_queue::*;
pub use async_runnable::AsyncRunnable; pub use async_runnable::AsyncRunnable;
pub use async_runnable::Error as AsyncError;
pub use async_worker::*; pub use async_worker::*;
pub use async_worker_pool::*; pub use async_worker_pool::*;

View file

@ -1,5 +1,4 @@
use crate::asynk::async_runnable::AsyncRunnable; use crate::asynk::async_runnable::AsyncRunnable;
use crate::asynk::async_runnable::Error as FangError;
use crate::CronError; use crate::CronError;
use crate::Scheduled::*; use crate::Scheduled::*;
use async_trait::async_trait; use async_trait::async_trait;
@ -103,15 +102,6 @@ impl From<cron::error::Error> for AsyncQueueError {
} }
} }
impl From<AsyncQueueError> for FangError {
fn from(error: AsyncQueueError) -> Self {
let message = format!("{:?}", error);
FangError {
description: message,
}
}
}
#[async_trait] #[async_trait]
pub trait AsyncQueueable: Send { pub trait AsyncQueueable: Send {
async fn fetch_and_touch_task( async fn fetch_and_touch_task(
@ -708,8 +698,8 @@ mod async_queue_tests {
use super::AsyncQueueable; use super::AsyncQueueable;
use super::FangTaskState; use super::FangTaskState;
use super::Task; use super::Task;
use crate::asynk::AsyncError as Error;
use crate::asynk::AsyncRunnable; use crate::asynk::AsyncRunnable;
use crate::FangError;
use crate::Scheduled; use crate::Scheduled;
use async_trait::async_trait; use async_trait::async_trait;
use bb8_postgres::bb8::Pool; use bb8_postgres::bb8::Pool;
@ -729,7 +719,7 @@ mod async_queue_tests {
#[typetag::serde] #[typetag::serde]
#[async_trait] #[async_trait]
impl AsyncRunnable for AsyncTask { impl AsyncRunnable for AsyncTask {
async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), Error> { async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), FangError> {
Ok(()) Ok(())
} }
} }
@ -743,7 +733,7 @@ mod async_queue_tests {
#[typetag::serde] #[typetag::serde]
#[async_trait] #[async_trait]
impl AsyncRunnable for AsyncTaskSchedule { impl AsyncRunnable for AsyncTaskSchedule {
async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), Error> { async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), FangError> {
Ok(()) Ok(())
} }

View file

@ -1,18 +1,45 @@
use crate::async_queue::AsyncQueueError;
use crate::asynk::async_queue::AsyncQueueable; use crate::asynk::async_queue::AsyncQueueable;
use crate::FangError;
use crate::Scheduled; use crate::Scheduled;
use async_trait::async_trait; use async_trait::async_trait;
use bb8_postgres::bb8::RunError;
use bb8_postgres::tokio_postgres::Error as TokioPostgresError;
use serde_json::Error as SerdeError;
const COMMON_TYPE: &str = "common"; const COMMON_TYPE: &str = "common";
#[derive(Debug)] impl From<AsyncQueueError> for FangError {
pub struct Error { fn from(error: AsyncQueueError) -> Self {
pub description: String, let message = format!("{:?}", error);
FangError {
description: message,
}
}
}
impl From<TokioPostgresError> for FangError {
fn from(error: TokioPostgresError) -> Self {
Self::from(AsyncQueueError::PgError(error))
}
}
impl From<RunError<TokioPostgresError>> for FangError {
fn from(error: RunError<TokioPostgresError>) -> Self {
Self::from(AsyncQueueError::PoolError(error))
}
}
impl From<SerdeError> for FangError {
fn from(error: SerdeError) -> Self {
Self::from(AsyncQueueError::SerdeError(error))
}
} }
#[typetag::serde(tag = "type")] #[typetag::serde(tag = "type")]
#[async_trait] #[async_trait]
pub trait AsyncRunnable: Send + Sync { pub trait AsyncRunnable: Send + Sync {
async fn run(&self, client: &mut dyn AsyncQueueable) -> Result<(), Error>; async fn run(&self, client: &mut dyn AsyncQueueable) -> Result<(), FangError>;
fn task_type(&self) -> String { fn task_type(&self) -> String {
COMMON_TYPE.to_string() COMMON_TYPE.to_string()

View file

@ -3,7 +3,7 @@ use crate::asynk::async_queue::FangTaskState;
use crate::asynk::async_queue::Task; use crate::asynk::async_queue::Task;
use crate::asynk::async_queue::DEFAULT_TASK_TYPE; use crate::asynk::async_queue::DEFAULT_TASK_TYPE;
use crate::asynk::async_runnable::AsyncRunnable; use crate::asynk::async_runnable::AsyncRunnable;
use crate::asynk::AsyncError as Error; use crate::FangError;
use crate::Scheduled::*; use crate::Scheduled::*;
use crate::{RetentionMode, SleepParams}; use crate::{RetentionMode, SleepParams};
use log::error; use log::error;
@ -32,7 +32,7 @@ where
&mut self, &mut self,
task: Task, task: Task,
actual_task: Box<dyn AsyncRunnable>, actual_task: Box<dyn AsyncRunnable>,
) -> Result<(), Error> { ) -> Result<(), FangError> {
let result = self.execute_task(task, actual_task).await; let result = self.execute_task(task, actual_task).await;
self.finalize_task(result).await self.finalize_task(result).await
} }
@ -49,7 +49,10 @@ where
} }
} }
async fn finalize_task(&mut self, result: Result<Task, (Task, String)>) -> Result<(), Error> { async fn finalize_task(
&mut self,
result: Result<Task, (Task, String)>,
) -> Result<(), FangError> {
match self.retention_mode { match self.retention_mode {
RetentionMode::KeepAll => match result { RetentionMode::KeepAll => match result {
Ok(task) => { Ok(task) => {
@ -92,7 +95,7 @@ where
tokio::time::sleep(self.sleep_params.sleep_period).await; tokio::time::sleep(self.sleep_params.sleep_period).await;
} }
pub async fn run_tasks(&mut self) -> Result<(), Error> { pub async fn run_tasks(&mut self) -> Result<(), FangError> {
loop { loop {
//fetch task //fetch task
match self match self
@ -146,7 +149,7 @@ impl<'a> AsyncWorkerTest<'a> {
&mut self, &mut self,
task: Task, task: Task,
actual_task: Box<dyn AsyncRunnable>, actual_task: Box<dyn AsyncRunnable>,
) -> Result<(), Error> { ) -> Result<(), FangError> {
let result = self.execute_task(task, actual_task).await; let result = self.execute_task(task, actual_task).await;
self.finalize_task(result).await self.finalize_task(result).await
} }
@ -163,7 +166,10 @@ impl<'a> AsyncWorkerTest<'a> {
} }
} }
async fn finalize_task(&mut self, result: Result<Task, (Task, String)>) -> Result<(), Error> { async fn finalize_task(
&mut self,
result: Result<Task, (Task, String)>,
) -> Result<(), FangError> {
match self.retention_mode { match self.retention_mode {
RetentionMode::KeepAll => match result { RetentionMode::KeepAll => match result {
Ok(task) => { Ok(task) => {
@ -206,7 +212,7 @@ impl<'a> AsyncWorkerTest<'a> {
tokio::time::sleep(self.sleep_params.sleep_period).await; tokio::time::sleep(self.sleep_params.sleep_period).await;
} }
pub async fn run_tasks_until_none(&mut self) -> Result<(), Error> { pub async fn run_tasks_until_none(&mut self) -> Result<(), FangError> {
loop { loop {
match self match self
.queue .queue
@ -246,8 +252,8 @@ mod async_worker_tests {
use crate::asynk::async_queue::AsyncQueueable; use crate::asynk::async_queue::AsyncQueueable;
use crate::asynk::async_queue::FangTaskState; use crate::asynk::async_queue::FangTaskState;
use crate::asynk::async_worker::Task; use crate::asynk::async_worker::Task;
use crate::asynk::AsyncError as Error;
use crate::asynk::AsyncRunnable; use crate::asynk::AsyncRunnable;
use crate::FangError;
use crate::RetentionMode; use crate::RetentionMode;
use crate::Scheduled; use crate::Scheduled;
use async_trait::async_trait; use async_trait::async_trait;
@ -266,7 +272,7 @@ mod async_worker_tests {
#[typetag::serde] #[typetag::serde]
#[async_trait] #[async_trait]
impl AsyncRunnable for WorkerAsyncTask { impl AsyncRunnable for WorkerAsyncTask {
async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), Error> { async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), FangError> {
Ok(()) Ok(())
} }
} }
@ -279,7 +285,7 @@ mod async_worker_tests {
#[typetag::serde] #[typetag::serde]
#[async_trait] #[async_trait]
impl AsyncRunnable for WorkerAsyncTaskSchedule { impl AsyncRunnable for WorkerAsyncTaskSchedule {
async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), Error> { async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), FangError> {
Ok(()) Ok(())
} }
fn cron(&self) -> Option<Scheduled> { fn cron(&self) -> Option<Scheduled> {
@ -295,10 +301,10 @@ mod async_worker_tests {
#[typetag::serde] #[typetag::serde]
#[async_trait] #[async_trait]
impl AsyncRunnable for AsyncFailedTask { impl AsyncRunnable for AsyncFailedTask {
async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), Error> { async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), FangError> {
let message = format!("number {} is wrong :(", self.number); let message = format!("number {} is wrong :(", self.number);
Err(Error { Err(FangError {
description: message, description: message,
}) })
} }
@ -310,7 +316,7 @@ mod async_worker_tests {
#[typetag::serde] #[typetag::serde]
#[async_trait] #[async_trait]
impl AsyncRunnable for AsyncTaskType1 { impl AsyncRunnable for AsyncTaskType1 {
async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), Error> { async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), FangError> {
Ok(()) Ok(())
} }
@ -325,7 +331,7 @@ mod async_worker_tests {
#[typetag::serde] #[typetag::serde]
#[async_trait] #[async_trait]
impl AsyncRunnable for AsyncTaskType2 { impl AsyncRunnable for AsyncTaskType2 {
async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), Error> { async fn run(&self, _queueable: &mut dyn AsyncQueueable) -> Result<(), FangError> {
Ok(()) Ok(())
} }

View file

@ -1,7 +1,7 @@
use crate::asynk::async_queue::AsyncQueueable; use crate::asynk::async_queue::AsyncQueueable;
use crate::asynk::async_queue::DEFAULT_TASK_TYPE; use crate::asynk::async_queue::DEFAULT_TASK_TYPE;
use crate::asynk::async_worker::AsyncWorker; use crate::asynk::async_worker::AsyncWorker;
use crate::asynk::AsyncError as Error; use crate::FangError;
use crate::{RetentionMode, SleepParams}; use crate::{RetentionMode, SleepParams};
use async_recursion::async_recursion; use async_recursion::async_recursion;
use log::error; use log::error;
@ -61,7 +61,7 @@ where
sleep_params: SleepParams, sleep_params: SleepParams,
retention_mode: RetentionMode, retention_mode: RetentionMode,
task_type: String, task_type: String,
) -> JoinHandle<Result<(), Error>> { ) -> JoinHandle<Result<(), FangError>> {
tokio::spawn(async move { tokio::spawn(async move {
Self::run_worker(queue, sleep_params, retention_mode, task_type).await Self::run_worker(queue, sleep_params, retention_mode, task_type).await
}) })
@ -71,7 +71,7 @@ where
sleep_params: SleepParams, sleep_params: SleepParams,
retention_mode: RetentionMode, retention_mode: RetentionMode,
task_type: String, task_type: String,
) -> Result<(), Error> { ) -> Result<(), FangError> {
let mut worker: AsyncWorker<AQueue> = AsyncWorker::builder() let mut worker: AsyncWorker<AQueue> = AsyncWorker::builder()
.queue(queue) .queue(queue)
.sleep_params(sleep_params) .sleep_params(sleep_params)

View file

@ -5,7 +5,6 @@ pub mod schema;
pub mod worker; pub mod worker;
pub mod worker_pool; pub mod worker_pool;
pub use error::FangError;
pub use queue::*; pub use queue::*;
pub use runnable::Runnable; pub use runnable::Runnable;
pub use schema::*; pub use schema::*;

View file

@ -1,23 +1,31 @@
use crate::blocking::queue::QueueError; use crate::blocking::queue::QueueError;
use crate::FangError;
use diesel::r2d2::PoolError;
use diesel::result::Error as DieselError;
use std::io::Error as IoError; use std::io::Error as IoError;
use std::sync::PoisonError;
use thiserror::Error;
#[derive(Error, Debug)] impl From<IoError> for FangError {
pub enum FangError { fn from(error: IoError) -> Self {
#[error("The shared state in an executor thread became poisoned")] let description = format!("{:?}", error);
PoisonedLock, FangError { description }
#[error(transparent)] }
QueueError(#[from] QueueError),
#[error("Failed to create executor thread")]
ExecutorThreadCreationFailed {
#[from]
source: IoError,
},
} }
impl<T> From<PoisonError<T>> for FangError { impl From<QueueError> for FangError {
fn from(_: PoisonError<T>) -> Self { fn from(error: QueueError) -> Self {
Self::PoisonedLock let description = format!("{:?}", error);
FangError { description }
}
}
impl From<DieselError> for FangError {
fn from(error: DieselError) -> Self {
Self::from(QueueError::DieselError(error))
}
}
impl From<PoolError> for FangError {
fn from(error: PoolError) -> Self {
Self::from(QueueError::PoolError(error))
} }
} }

View file

@ -369,7 +369,7 @@ mod queue_tests {
use crate::runnable::COMMON_TYPE; use crate::runnable::COMMON_TYPE;
use crate::schema::FangTaskState; use crate::schema::FangTaskState;
use crate::typetag; use crate::typetag;
use crate::worker::Error as WorkerError; use crate::FangError;
use chrono::Utc; use chrono::Utc;
use diesel::connection::Connection; use diesel::connection::Connection;
use diesel::result::Error; use diesel::result::Error;
@ -382,7 +382,7 @@ mod queue_tests {
#[typetag::serde] #[typetag::serde]
impl Runnable for PepeTask { impl Runnable for PepeTask {
fn run(&self, _queue: &dyn Queueable) -> Result<(), WorkerError> { fn run(&self, _queue: &dyn Queueable) -> Result<(), FangError> {
println!("the number is {}", self.number); println!("the number is {}", self.number);
Ok(()) Ok(())
@ -399,7 +399,7 @@ mod queue_tests {
#[typetag::serde] #[typetag::serde]
impl Runnable for AyratTask { impl Runnable for AyratTask {
fn run(&self, _queue: &dyn Queueable) -> Result<(), WorkerError> { fn run(&self, _queue: &dyn Queueable) -> Result<(), FangError> {
println!("the number is {}", self.number); println!("the number is {}", self.number);
Ok(()) Ok(())

View file

@ -1,12 +1,12 @@
use crate::queue::Queueable; use crate::queue::Queueable;
use crate::Error; use crate::FangError;
use crate::Scheduled; use crate::Scheduled;
pub const COMMON_TYPE: &str = "common"; pub const COMMON_TYPE: &str = "common";
#[typetag::serde(tag = "type")] #[typetag::serde(tag = "type")]
pub trait Runnable { pub trait Runnable {
fn run(&self, _queueable: &dyn Queueable) -> Result<(), Error>; fn run(&self, _queueable: &dyn Queueable) -> Result<(), FangError>;
fn task_type(&self) -> String { fn task_type(&self) -> String {
COMMON_TYPE.to_string() COMMON_TYPE.to_string()

View file

@ -1,9 +1,9 @@
use crate::error::FangError;
use crate::queue::Queueable; use crate::queue::Queueable;
use crate::queue::Task; use crate::queue::Task;
use crate::runnable::Runnable; use crate::runnable::Runnable;
use crate::runnable::COMMON_TYPE; use crate::runnable::COMMON_TYPE;
use crate::schema::FangTaskState; use crate::schema::FangTaskState;
use crate::FangError;
use crate::Scheduled::*; use crate::Scheduled::*;
use crate::{RetentionMode, SleepParams}; use crate::{RetentionMode, SleepParams};
use log::error; use log::error;
@ -25,11 +25,6 @@ where
pub retention_mode: RetentionMode, pub retention_mode: RetentionMode,
} }
#[derive(Debug)]
pub struct Error {
pub description: String,
}
impl<BQueue> Worker<BQueue> impl<BQueue> Worker<BQueue>
where where
BQueue: Queueable + Clone + Sync + Send + 'static, BQueue: Queueable + Clone + Sync + Send + 'static,
@ -148,7 +143,6 @@ where
#[cfg(test)] #[cfg(test)]
mod worker_tests { mod worker_tests {
use super::Error;
use super::RetentionMode; use super::RetentionMode;
use super::Runnable; use super::Runnable;
use super::Worker; use super::Worker;
@ -156,6 +150,7 @@ mod worker_tests {
use crate::queue::Queueable; use crate::queue::Queueable;
use crate::schema::FangTaskState; use crate::schema::FangTaskState;
use crate::typetag; use crate::typetag;
use crate::FangError;
use chrono::Utc; use chrono::Utc;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -166,7 +161,7 @@ mod worker_tests {
#[typetag::serde] #[typetag::serde]
impl Runnable for WorkerTaskTest { impl Runnable for WorkerTaskTest {
fn run(&self, _queue: &dyn Queueable) -> Result<(), Error> { fn run(&self, _queue: &dyn Queueable) -> Result<(), FangError> {
println!("the number is {}", self.number); println!("the number is {}", self.number);
Ok(()) Ok(())
@ -184,10 +179,10 @@ mod worker_tests {
#[typetag::serde] #[typetag::serde]
impl Runnable for FailedTask { impl Runnable for FailedTask {
fn run(&self, _queue: &dyn Queueable) -> Result<(), Error> { fn run(&self, _queue: &dyn Queueable) -> Result<(), FangError> {
let message = format!("the number is {}", self.number); let message = format!("the number is {}", self.number);
Err(Error { Err(FangError {
description: message, description: message,
}) })
} }
@ -202,7 +197,7 @@ mod worker_tests {
#[typetag::serde] #[typetag::serde]
impl Runnable for TaskType1 { impl Runnable for TaskType1 {
fn run(&self, _queue: &dyn Queueable) -> Result<(), Error> { fn run(&self, _queue: &dyn Queueable) -> Result<(), FangError> {
Ok(()) Ok(())
} }
@ -216,7 +211,7 @@ mod worker_tests {
#[typetag::serde] #[typetag::serde]
impl Runnable for TaskType2 { impl Runnable for TaskType2 {
fn run(&self, _queue: &dyn Queueable) -> Result<(), Error> { fn run(&self, _queue: &dyn Queueable) -> Result<(), FangError> {
Ok(()) Ok(())
} }

View file

@ -1,6 +1,6 @@
use crate::error::FangError;
use crate::queue::Queueable; use crate::queue::Queueable;
use crate::worker::Worker; use crate::worker::Worker;
use crate::FangError;
use crate::RetentionMode; use crate::RetentionMode;
use crate::SleepParams; use crate::SleepParams;
use log::error; use log::error;

View file

@ -62,6 +62,11 @@ impl Default for SleepParams {
} }
} }
#[derive(Debug)]
pub struct FangError {
pub description: String,
}
#[macro_use] #[macro_use]
#[cfg(feature = "blocking")] #[cfg(feature = "blocking")]
extern crate diesel; extern crate diesel;