parent
076d89e866
commit
b975e893e2
12 changed files with 101 additions and 72 deletions
|
@ -5,6 +5,5 @@ pub mod async_worker_pool;
|
|||
|
||||
pub use async_queue::*;
|
||||
pub use async_runnable::AsyncRunnable;
|
||||
pub use async_runnable::Error as AsyncError;
|
||||
pub use async_worker::*;
|
||||
pub use async_worker_pool::*;
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use crate::asynk::async_runnable::AsyncRunnable;
|
||||
use crate::asynk::async_runnable::Error as FangError;
|
||||
use crate::CronError;
|
||||
use crate::Scheduled::*;
|
||||
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]
|
||||
pub trait AsyncQueueable: Send {
|
||||
async fn fetch_and_touch_task(
|
||||
|
@ -708,8 +698,8 @@ mod async_queue_tests {
|
|||
use super::AsyncQueueable;
|
||||
use super::FangTaskState;
|
||||
use super::Task;
|
||||
use crate::asynk::AsyncError as Error;
|
||||
use crate::asynk::AsyncRunnable;
|
||||
use crate::FangError;
|
||||
use crate::Scheduled;
|
||||
use async_trait::async_trait;
|
||||
use bb8_postgres::bb8::Pool;
|
||||
|
@ -729,7 +719,7 @@ mod async_queue_tests {
|
|||
#[typetag::serde]
|
||||
#[async_trait]
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
@ -743,7 +733,7 @@ mod async_queue_tests {
|
|||
#[typetag::serde]
|
||||
#[async_trait]
|
||||
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(())
|
||||
}
|
||||
|
||||
|
|
|
@ -1,18 +1,45 @@
|
|||
use crate::async_queue::AsyncQueueError;
|
||||
use crate::asynk::async_queue::AsyncQueueable;
|
||||
use crate::FangError;
|
||||
use crate::Scheduled;
|
||||
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";
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
pub description: String,
|
||||
impl From<AsyncQueueError> for FangError {
|
||||
fn from(error: AsyncQueueError) -> Self {
|
||||
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")]
|
||||
#[async_trait]
|
||||
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 {
|
||||
COMMON_TYPE.to_string()
|
||||
|
|
|
@ -3,7 +3,7 @@ use crate::asynk::async_queue::FangTaskState;
|
|||
use crate::asynk::async_queue::Task;
|
||||
use crate::asynk::async_queue::DEFAULT_TASK_TYPE;
|
||||
use crate::asynk::async_runnable::AsyncRunnable;
|
||||
use crate::asynk::AsyncError as Error;
|
||||
use crate::FangError;
|
||||
use crate::Scheduled::*;
|
||||
use crate::{RetentionMode, SleepParams};
|
||||
use log::error;
|
||||
|
@ -32,7 +32,7 @@ where
|
|||
&mut self,
|
||||
task: Task,
|
||||
actual_task: Box<dyn AsyncRunnable>,
|
||||
) -> Result<(), Error> {
|
||||
) -> Result<(), FangError> {
|
||||
let result = self.execute_task(task, actual_task).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 {
|
||||
RetentionMode::KeepAll => match result {
|
||||
Ok(task) => {
|
||||
|
@ -92,7 +95,7 @@ where
|
|||
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 {
|
||||
//fetch task
|
||||
match self
|
||||
|
@ -146,7 +149,7 @@ impl<'a> AsyncWorkerTest<'a> {
|
|||
&mut self,
|
||||
task: Task,
|
||||
actual_task: Box<dyn AsyncRunnable>,
|
||||
) -> Result<(), Error> {
|
||||
) -> Result<(), FangError> {
|
||||
let result = self.execute_task(task, actual_task).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 {
|
||||
RetentionMode::KeepAll => match result {
|
||||
Ok(task) => {
|
||||
|
@ -206,7 +212,7 @@ impl<'a> AsyncWorkerTest<'a> {
|
|||
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 {
|
||||
match self
|
||||
.queue
|
||||
|
@ -246,8 +252,8 @@ mod async_worker_tests {
|
|||
use crate::asynk::async_queue::AsyncQueueable;
|
||||
use crate::asynk::async_queue::FangTaskState;
|
||||
use crate::asynk::async_worker::Task;
|
||||
use crate::asynk::AsyncError as Error;
|
||||
use crate::asynk::AsyncRunnable;
|
||||
use crate::FangError;
|
||||
use crate::RetentionMode;
|
||||
use crate::Scheduled;
|
||||
use async_trait::async_trait;
|
||||
|
@ -266,7 +272,7 @@ mod async_worker_tests {
|
|||
#[typetag::serde]
|
||||
#[async_trait]
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
@ -279,7 +285,7 @@ mod async_worker_tests {
|
|||
#[typetag::serde]
|
||||
#[async_trait]
|
||||
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(())
|
||||
}
|
||||
fn cron(&self) -> Option<Scheduled> {
|
||||
|
@ -295,10 +301,10 @@ mod async_worker_tests {
|
|||
#[typetag::serde]
|
||||
#[async_trait]
|
||||
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);
|
||||
|
||||
Err(Error {
|
||||
Err(FangError {
|
||||
description: message,
|
||||
})
|
||||
}
|
||||
|
@ -310,7 +316,7 @@ mod async_worker_tests {
|
|||
#[typetag::serde]
|
||||
#[async_trait]
|
||||
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(())
|
||||
}
|
||||
|
||||
|
@ -325,7 +331,7 @@ mod async_worker_tests {
|
|||
#[typetag::serde]
|
||||
#[async_trait]
|
||||
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(())
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::asynk::async_queue::AsyncQueueable;
|
||||
use crate::asynk::async_queue::DEFAULT_TASK_TYPE;
|
||||
use crate::asynk::async_worker::AsyncWorker;
|
||||
use crate::asynk::AsyncError as Error;
|
||||
use crate::FangError;
|
||||
use crate::{RetentionMode, SleepParams};
|
||||
use async_recursion::async_recursion;
|
||||
use log::error;
|
||||
|
@ -61,7 +61,7 @@ where
|
|||
sleep_params: SleepParams,
|
||||
retention_mode: RetentionMode,
|
||||
task_type: String,
|
||||
) -> JoinHandle<Result<(), Error>> {
|
||||
) -> JoinHandle<Result<(), FangError>> {
|
||||
tokio::spawn(async move {
|
||||
Self::run_worker(queue, sleep_params, retention_mode, task_type).await
|
||||
})
|
||||
|
@ -71,7 +71,7 @@ where
|
|||
sleep_params: SleepParams,
|
||||
retention_mode: RetentionMode,
|
||||
task_type: String,
|
||||
) -> Result<(), Error> {
|
||||
) -> Result<(), FangError> {
|
||||
let mut worker: AsyncWorker<AQueue> = AsyncWorker::builder()
|
||||
.queue(queue)
|
||||
.sleep_params(sleep_params)
|
||||
|
|
|
@ -5,7 +5,6 @@ pub mod schema;
|
|||
pub mod worker;
|
||||
pub mod worker_pool;
|
||||
|
||||
pub use error::FangError;
|
||||
pub use queue::*;
|
||||
pub use runnable::Runnable;
|
||||
pub use schema::*;
|
||||
|
|
|
@ -1,23 +1,31 @@
|
|||
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::sync::PoisonError;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum FangError {
|
||||
#[error("The shared state in an executor thread became poisoned")]
|
||||
PoisonedLock,
|
||||
#[error(transparent)]
|
||||
QueueError(#[from] QueueError),
|
||||
#[error("Failed to create executor thread")]
|
||||
ExecutorThreadCreationFailed {
|
||||
#[from]
|
||||
source: IoError,
|
||||
},
|
||||
impl From<IoError> for FangError {
|
||||
fn from(error: IoError) -> Self {
|
||||
let description = format!("{:?}", error);
|
||||
FangError { description }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<PoisonError<T>> for FangError {
|
||||
fn from(_: PoisonError<T>) -> Self {
|
||||
Self::PoisonedLock
|
||||
impl From<QueueError> for FangError {
|
||||
fn from(error: QueueError) -> Self {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -369,7 +369,7 @@ mod queue_tests {
|
|||
use crate::runnable::COMMON_TYPE;
|
||||
use crate::schema::FangTaskState;
|
||||
use crate::typetag;
|
||||
use crate::worker::Error as WorkerError;
|
||||
use crate::FangError;
|
||||
use chrono::Utc;
|
||||
use diesel::connection::Connection;
|
||||
use diesel::result::Error;
|
||||
|
@ -382,7 +382,7 @@ mod queue_tests {
|
|||
|
||||
#[typetag::serde]
|
||||
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);
|
||||
|
||||
Ok(())
|
||||
|
@ -399,7 +399,7 @@ mod queue_tests {
|
|||
|
||||
#[typetag::serde]
|
||||
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);
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
use crate::queue::Queueable;
|
||||
use crate::Error;
|
||||
use crate::FangError;
|
||||
use crate::Scheduled;
|
||||
|
||||
pub const COMMON_TYPE: &str = "common";
|
||||
|
||||
#[typetag::serde(tag = "type")]
|
||||
pub trait Runnable {
|
||||
fn run(&self, _queueable: &dyn Queueable) -> Result<(), Error>;
|
||||
fn run(&self, _queueable: &dyn Queueable) -> Result<(), FangError>;
|
||||
|
||||
fn task_type(&self) -> String {
|
||||
COMMON_TYPE.to_string()
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use crate::error::FangError;
|
||||
use crate::queue::Queueable;
|
||||
use crate::queue::Task;
|
||||
use crate::runnable::Runnable;
|
||||
use crate::runnable::COMMON_TYPE;
|
||||
use crate::schema::FangTaskState;
|
||||
use crate::FangError;
|
||||
use crate::Scheduled::*;
|
||||
use crate::{RetentionMode, SleepParams};
|
||||
use log::error;
|
||||
|
@ -25,11 +25,6 @@ where
|
|||
pub retention_mode: RetentionMode,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
impl<BQueue> Worker<BQueue>
|
||||
where
|
||||
BQueue: Queueable + Clone + Sync + Send + 'static,
|
||||
|
@ -148,7 +143,6 @@ where
|
|||
|
||||
#[cfg(test)]
|
||||
mod worker_tests {
|
||||
use super::Error;
|
||||
use super::RetentionMode;
|
||||
use super::Runnable;
|
||||
use super::Worker;
|
||||
|
@ -156,6 +150,7 @@ mod worker_tests {
|
|||
use crate::queue::Queueable;
|
||||
use crate::schema::FangTaskState;
|
||||
use crate::typetag;
|
||||
use crate::FangError;
|
||||
use chrono::Utc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
@ -166,7 +161,7 @@ mod worker_tests {
|
|||
|
||||
#[typetag::serde]
|
||||
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);
|
||||
|
||||
Ok(())
|
||||
|
@ -184,10 +179,10 @@ mod worker_tests {
|
|||
|
||||
#[typetag::serde]
|
||||
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);
|
||||
|
||||
Err(Error {
|
||||
Err(FangError {
|
||||
description: message,
|
||||
})
|
||||
}
|
||||
|
@ -202,7 +197,7 @@ mod worker_tests {
|
|||
|
||||
#[typetag::serde]
|
||||
impl Runnable for TaskType1 {
|
||||
fn run(&self, _queue: &dyn Queueable) -> Result<(), Error> {
|
||||
fn run(&self, _queue: &dyn Queueable) -> Result<(), FangError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -216,7 +211,7 @@ mod worker_tests {
|
|||
|
||||
#[typetag::serde]
|
||||
impl Runnable for TaskType2 {
|
||||
fn run(&self, _queue: &dyn Queueable) -> Result<(), Error> {
|
||||
fn run(&self, _queue: &dyn Queueable) -> Result<(), FangError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::error::FangError;
|
||||
use crate::queue::Queueable;
|
||||
use crate::worker::Worker;
|
||||
use crate::FangError;
|
||||
use crate::RetentionMode;
|
||||
use crate::SleepParams;
|
||||
use log::error;
|
||||
|
|
|
@ -62,6 +62,11 @@ impl Default for SleepParams {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FangError {
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
#[macro_use]
|
||||
#[cfg(feature = "blocking")]
|
||||
extern crate diesel;
|
||||
|
|
Loading…
Reference in a new issue