Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
use common_utils::errors::CustomResult;
pub use diesel_models as storage;
use diesel_models::enums as storage_enums;
use error_stack::{report, ResultExt};
use storage_impl::{connection, errors, mock_db::MockDb};
use time::PrimitiveDateTime;
use crate::{metrics, scheduler::Store};
#[router_derive::track_async_interface]
#[async_trait::async_trait]
pub trait ProcessTrackerInterface: Send + Sync + 'static {
async fn reinitialize_limbo_processes(
&self,
ids: Vec<String>,
schedule_time: PrimitiveDateTime,
) -> CustomResult<usize, errors::StorageError>;
async fn find_process_by_id(
&self,
id: &str,
) -> CustomResult<Option<storage::ProcessTracker>, errors::StorageError>;
async fn update_process(
&self,
this: storage::ProcessTracker,
process: storage::ProcessTrackerUpdate,
) -> CustomResult<storage::ProcessTracker, errors::StorageError>;
async fn process_tracker_update_process_status_by_ids(
&self,
task_ids: Vec<String>,
task_update: storage::ProcessTrackerUpdate,
) -> CustomResult<usize, errors::StorageError>;
async fn insert_process(
&self,
new: storage::ProcessTrackerNew,
) -> CustomResult<storage::ProcessTracker, errors::StorageError>;
async fn reset_process(
&self,
this: storage::ProcessTracker,
schedule_time: PrimitiveDateTime,
) -> CustomResult<(), errors::StorageError>;
async fn retry_process(
&self,
this: storage::ProcessTracker,
schedule_time: PrimitiveDateTime,
) -> CustomResult<(), errors::StorageError>;
async fn finish_process_with_business_status(
&self,
this: storage::ProcessTracker,
business_status: &'static str,
) -> CustomResult<(), errors::StorageError>;
/// Finish a process while updating any other columns in the same write.
async fn finish_process_with_update(
&self,
this: storage::ProcessTracker,
update: storage::ProcessTrackerUpdate,
) -> CustomResult<(), errors::StorageError>;
async fn find_processes_by_time_status(
&self,
time_lower_limit: PrimitiveDateTime,
time_upper_limit: PrimitiveDateTime,
status: storage_enums::ProcessTrackerStatus,
limit: Option<i64>,
) -> CustomResult<Vec<storage::ProcessTracker>, errors::StorageError>;
}
#[async_trait::async_trait]
impl ProcessTrackerInterface for Store {
async fn find_process_by_id(
&self,
id: &str,
) -> CustomResult<Option<storage::ProcessTracker>, errors::StorageError> {
let conn = connection::pg_connection_read(self).await?;
storage::ProcessTracker::find_process_by_id(&conn, id)
.await
.map_err(|error| report!(errors::StorageError::from(error)))
}
async fn reinitialize_limbo_processes(
&self,
ids: Vec<String>,
schedule_time: PrimitiveDateTime,
) -> CustomResult<usize, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
storage::ProcessTracker::reinitialize_limbo_processes(&conn, ids, schedule_time)
.await
.map_err(|error| report!(errors::StorageError::from(error)))
}
async fn find_processes_by_time_status(
&self,
time_lower_limit: PrimitiveDateTime,
time_upper_limit: PrimitiveDateTime,
status: storage_enums::ProcessTrackerStatus,
limit: Option<i64>,
) -> CustomResult<Vec<storage::ProcessTracker>, errors::StorageError> {
let conn = connection::pg_connection_read(self).await?;
storage::ProcessTracker::find_processes_by_time_status(
&conn,
time_lower_limit,
time_upper_limit,
status,
limit,
common_types::consts::API_VERSION,
)
.await
.map_err(|error| report!(errors::StorageError::from(error)))
}
async fn insert_process(
&self,
new: storage::ProcessTrackerNew,
) -> CustomResult<storage::ProcessTracker, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
new.insert_process(&conn)
.await
.map_err(|error| report!(errors::StorageError::from(error)))
}
async fn update_process(
&self,
this: storage::ProcessTracker,
process: storage::ProcessTrackerUpdate,
) -> CustomResult<storage::ProcessTracker, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
this.update(&conn, process)
.await
.map_err(|error| report!(errors::StorageError::from(error)))
}
async fn reset_process(
&self,
this: storage::ProcessTracker,
schedule_time: PrimitiveDateTime,
) -> CustomResult<(), errors::StorageError> {
ProcessTrackerInterface::update_process(
self,
this,
storage::ProcessTrackerUpdate::StatusRetryUpdate {
status: storage_enums::ProcessTrackerStatus::New,
retry_count: 0,
schedule_time,
},
)
.await?;
Ok(())
}
async fn retry_process(
&self,
this: storage::ProcessTracker,
schedule_time: PrimitiveDateTime,
) -> CustomResult<(), errors::StorageError> {
metrics::TASK_RETRIED.add(1, &[]);
let retry_count = this.retry_count + 1;
ProcessTrackerInterface::update_process(
self,
this,
storage::ProcessTrackerUpdate::StatusRetryUpdate {
status: storage_enums::ProcessTrackerStatus::Pending,
retry_count,
schedule_time,
},
)
.await?;
Ok(())
}
async fn finish_process_with_business_status(
&self,
this: storage::ProcessTracker,
business_status: &'static str,
) -> CustomResult<(), errors::StorageError> {
ProcessTrackerInterface::update_process(
self,
this,
storage::ProcessTrackerUpdate::StatusUpdate {
status: storage_enums::ProcessTrackerStatus::Finish,
business_status: Some(String::from(business_status)),
},
)
.await
.attach_printable("Failed to update business status of process")?;
metrics::TASK_FINISHED.add(1, &[]);
Ok(())
}
async fn finish_process_with_update(
&self,
this: storage::ProcessTracker,
update: storage::ProcessTrackerUpdate,
) -> CustomResult<(), errors::StorageError> {
ProcessTrackerInterface::update_process(self, this, update)
.await
.attach_printable("Failed to finish process with update")?;
metrics::TASK_FINISHED.add(1, &[]);
Ok(())
}
async fn process_tracker_update_process_status_by_ids(
&self,
task_ids: Vec<String>,
task_update: storage::ProcessTrackerUpdate,
) -> CustomResult<usize, errors::StorageError> {
let conn = connection::pg_connection_write(self).await?;
storage::ProcessTracker::update_process_status_by_ids(&conn, task_ids, task_update)
.await
.map_err(|error| report!(errors::StorageError::from(error)))
}
}
#[async_trait::async_trait]
impl ProcessTrackerInterface for MockDb {
async fn find_process_by_id(
&self,
id: &str,
) -> CustomResult<Option<storage::ProcessTracker>, errors::StorageError> {
let optional = self
.processes
.lock()
.await
.iter()
.find(|process| process.id == id)
.cloned();
Ok(optional)
}
async fn reinitialize_limbo_processes(
&self,
_ids: Vec<String>,
_schedule_time: PrimitiveDateTime,
) -> CustomResult<usize, errors::StorageError> {
// [#172]: Implement function for `MockDb`
Err(errors::StorageError::MockDbError)?
}
async fn find_processes_by_time_status(
&self,
_time_lower_limit: PrimitiveDateTime,
_time_upper_limit: PrimitiveDateTime,
_status: storage_enums::ProcessTrackerStatus,
_limit: Option<i64>,
) -> CustomResult<Vec<storage::ProcessTracker>, errors::StorageError> {
// [#172]: Implement function for `MockDb`
Err(errors::StorageError::MockDbError)?
}
async fn insert_process(
&self,
new: storage::ProcessTrackerNew,
) -> CustomResult<storage::ProcessTracker, errors::StorageError> {
let mut processes = self.processes.lock().await;
let process = storage::ProcessTracker {
id: new.id,
name: new.name,
tag: new.tag,
runner: new.runner,
retry_count: new.retry_count,
schedule_time: new.schedule_time,
rule: new.rule,
tracking_data: new.tracking_data,
business_status: new.business_status,
status: new.status,
event: new.event,
created_at: new.created_at,
updated_at: new.updated_at,
version: new.version,
application_source: new.application_source,
};
processes.push(process.clone());
Ok(process)
}
async fn update_process(
&self,
_this: storage::ProcessTracker,
_process: storage::ProcessTrackerUpdate,
) -> CustomResult<storage::ProcessTracker, errors::StorageError> {
// [#172]: Implement function for `MockDb`
Err(errors::StorageError::MockDbError)?
}
async fn reset_process(
&self,
_this: storage::ProcessTracker,
_schedule_time: PrimitiveDateTime,
) -> CustomResult<(), errors::StorageError> {
// [#172]: Implement function for `MockDb`
Err(errors::StorageError::MockDbError)?
}
async fn retry_process(
&self,
_this: storage::ProcessTracker,
_schedule_time: PrimitiveDateTime,
) -> CustomResult<(), errors::StorageError> {
// [#172]: Implement function for `MockDb`
Err(errors::StorageError::MockDbError)?
}
async fn finish_process_with_business_status(
&self,
_this: storage::ProcessTracker,
_business_status: &'static str,
) -> CustomResult<(), errors::StorageError> {
// [#172]: Implement function for `MockDb`
Err(errors::StorageError::MockDbError)?
}
async fn finish_process_with_update(
&self,
_this: storage::ProcessTracker,
_update: storage::ProcessTrackerUpdate,
) -> CustomResult<(), errors::StorageError> {
// [#172]: Implement function for `MockDb`
Err(errors::StorageError::MockDbError)?
}
async fn process_tracker_update_process_status_by_ids(
&self,
_task_ids: Vec<String>,
_task_update: storage::ProcessTrackerUpdate,
) -> CustomResult<usize, errors::StorageError> {
// [#172]: Implement function for `MockDb`
Err(errors::StorageError::MockDbError)?
}
}