outbox_pattern_processor/
app_state.rs1use crate::aws::{SnsClient, SqsClient};
2use crate::error::OutboxPatternProcessorError;
3use crate::http_gateway::HttpGateway;
4use sqlx::{Pool, Postgres, Transaction};
5
6#[derive(Clone)]
7pub struct AppState {
8 pub postgres_pool: Pool<Postgres>,
9 pub sqs_client: Option<SqsClient>,
10 pub sns_client: Option<SnsClient>,
11 pub http_gateway: HttpGateway,
12 pub outbox_query_limit: Option<u32>,
13 pub delete_after_process_successfully: Option<bool>,
14 pub max_in_flight_interval_in_seconds: Option<u64>,
15 pub outbox_failure_limit: Option<u32>,
16 pub scheduled_clear_locked_partition: Option<bool>,
17 pub delay_for_failure_attempt_in_seconds: Option<u64>,
18}
19
20impl AppState {
21 pub async fn begin_transaction(&self) -> Result<Transaction<'_, Postgres>, OutboxPatternProcessorError> {
22 self.postgres_pool
23 .begin()
24 .await
25 .map_err(|error| OutboxPatternProcessorError::new(&error.to_string(), "Failed to create transaction"))
26 }
27
28 pub async fn commit_transaction(
29 &self,
30 transaction: Transaction<'_, Postgres>,
31 ) -> Result<(), OutboxPatternProcessorError> {
32 transaction
33 .commit()
34 .await
35 .map_err(|error| OutboxPatternProcessorError::new(&error.to_string(), "Failed to commit transaction"))?;
36
37 Ok(())
38 }
39}