outbox_pattern_processor/
outbox.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use crate::http_destination::HttpDestination;
use crate::outbox_destination::OutboxDestination;
use crate::sns_destination::SnsDestination;
use crate::sqs_destination::SqsDestination;
use serde_json::Value;
use sqlx::types::chrono::{DateTime, Utc};
use sqlx::types::Json;
use sqlx::FromRow;
use std::collections::HashMap;
use uuid::Uuid;

#[derive(Debug, FromRow, Clone, PartialEq)]
pub struct Outbox {
    pub idempotent_key: Uuid,
    pub partition_key: Uuid,
    pub destinations: Json<Vec<OutboxDestination>>,
    pub headers: Option<Json<HashMap<String, String>>>,
    pub payload: String,
    pub attempts: i32,
    pub created_at: DateTime<Utc>,
    pub processing_until: Option<DateTime<Utc>>,
    pub processed_at: Option<DateTime<Utc>>,
}

impl Outbox {
    pub fn start_delay(
        &self,
        process_after: DateTime<Utc>,
    ) -> Self {
        Outbox {
            idempotent_key: self.idempotent_key,
            partition_key: self.partition_key,
            destinations: self.destinations.clone(),
            headers: self.headers.clone(),
            payload: self.payload.clone(),
            attempts: self.attempts,
            created_at: self.created_at,
            processing_until: Some(process_after),
            processed_at: self.processed_at,
        }
    }

    pub fn http_post_json(
        partition_key: Uuid,
        url: &str,
        headers: Option<HashMap<String, String>>,
        payload: &Value,
    ) -> Self {
        Self::http(partition_key, url, headers, vec![], &payload.to_string(), None)
    }

    pub fn http_put_json(
        partition_key: Uuid,
        url: &str,
        headers: Option<HashMap<String, String>>,
        payload: &Value,
    ) -> Self {
        Self::http(partition_key, url, headers, vec![], &payload.to_string(), Some("put".to_string()))
    }

    pub fn http_patch_json(
        partition_key: Uuid,
        url: &str,
        headers: Option<HashMap<String, String>>,
        payload: &Value,
    ) -> Self {
        Self::http(partition_key, url, headers, vec![], &payload.to_string(), Some("patch".to_string()))
    }

    pub fn sqs(
        partition_key: Uuid,
        queue_url: &str,
        headers: Option<HashMap<String, String>>,
        payload: &str,
    ) -> Self {
        let destinations = vec![OutboxDestination::SqsDestination(SqsDestination { queue_url: queue_url.to_string() })];

        Self::new(partition_key, destinations, headers, payload)
    }

    pub fn sns(
        partition_key: Uuid,
        topic_arn: &str,
        headers: Option<HashMap<String, String>>,
        payload: &str,
    ) -> Self {
        let destinations = vec![OutboxDestination::SnsDestination(SnsDestination { topic_arn: topic_arn.to_string() })];

        Self::new(partition_key, destinations, headers, payload)
    }

    pub fn http_and_sns(
        partition_key: Uuid,
        url: &str,
        topic_arn: &str,
        headers: Option<HashMap<String, String>>,
        payload: &Value,
    ) -> Self {
        let destinations = vec![OutboxDestination::SnsDestination(SnsDestination { topic_arn: topic_arn.to_string() })];
        Self::http(partition_key, url, headers, destinations, &payload.to_string(), None)
    }

    pub fn http_and_sqs(
        partition_key: Uuid,
        url: &str,
        queue_url: &str,
        headers: Option<HashMap<String, String>>,
        payload: &Value,
    ) -> Self {
        let destinations = vec![OutboxDestination::SqsDestination(SqsDestination { queue_url: queue_url.to_string() })];
        Self::http(partition_key, url, headers, destinations, &payload.to_string(), None)
    }

    fn http(
        partition_key: Uuid,
        url: &str,
        headers: Option<HashMap<String, String>>,
        destinations: Vec<OutboxDestination>,
        payload: &str,
        method: Option<String>,
    ) -> Self {
        let mut extended_headers = headers.unwrap_or_default();
        extended_headers.extend(HashMap::from([("Content-Type".to_string(), "application/json".to_string())]));

        let mut all_destinations = vec![];
        all_destinations.extend(destinations);
        all_destinations.push(OutboxDestination::HttpDestination(HttpDestination {
            url: url.to_string(),
            headers: Some(extended_headers),
            method,
        }));

        Self::new(partition_key, all_destinations, None, payload)
    }

    pub fn new(
        partition_key: Uuid,
        destinations: Vec<OutboxDestination>,
        headers: Option<HashMap<String, String>>,
        payload: &str,
    ) -> Self {
        Outbox {
            idempotent_key: Uuid::now_v7(),
            partition_key,
            destinations: Json(destinations),
            headers: headers.map(Json),
            payload: payload.to_string(),
            attempts: 0,
            created_at: Utc::now(),
            processing_until: None,
            processed_at: None,
        }
    }
}