2022-08-31 17:45:13 +00:00
|
|
|
use crate::async_queue::AsyncQueueError;
|
2022-07-23 14:24:22 +00:00
|
|
|
use crate::asynk::async_queue::AsyncQueueable;
|
2022-08-31 17:45:13 +00:00
|
|
|
use crate::FangError;
|
2022-08-29 16:59:22 +00:00
|
|
|
use crate::Scheduled;
|
2022-07-17 16:22:00 +00:00
|
|
|
use async_trait::async_trait;
|
2022-08-31 17:45:13 +00:00
|
|
|
use bb8_postgres::bb8::RunError;
|
|
|
|
use bb8_postgres::tokio_postgres::Error as TokioPostgresError;
|
|
|
|
use serde_json::Error as SerdeError;
|
2022-07-17 16:22:00 +00:00
|
|
|
|
|
|
|
const COMMON_TYPE: &str = "common";
|
2022-09-17 16:39:47 +00:00
|
|
|
pub const RETRIES_NUMBER: i32 = 20;
|
2022-07-17 16:22:00 +00:00
|
|
|
|
2022-08-31 17:45:13 +00:00
|
|
|
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))
|
|
|
|
}
|
2022-07-17 16:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[typetag::serde(tag = "type")]
|
2022-07-31 13:32:37 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub trait AsyncRunnable: Send + Sync {
|
2022-08-31 17:45:13 +00:00
|
|
|
async fn run(&self, client: &mut dyn AsyncQueueable) -> Result<(), FangError>;
|
2022-07-17 16:22:00 +00:00
|
|
|
|
|
|
|
fn task_type(&self) -> String {
|
|
|
|
COMMON_TYPE.to_string()
|
|
|
|
}
|
2022-08-27 15:58:38 +00:00
|
|
|
|
|
|
|
fn uniq(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cron(&self) -> Option<Scheduled> {
|
|
|
|
None
|
|
|
|
}
|
2022-09-17 16:39:47 +00:00
|
|
|
|
|
|
|
fn max_retries(&self) -> i32 {
|
|
|
|
RETRIES_NUMBER
|
|
|
|
}
|
|
|
|
|
|
|
|
fn backoff(&self, attempt: u32) -> u32 {
|
|
|
|
u32::pow(2, attempt)
|
|
|
|
}
|
2022-07-17 16:22:00 +00:00
|
|
|
}
|