| 1 |
|
| 2 |
|
| 3 |
use crate::error::Result; |
| 4 |
use chrono::{DateTime, Utc}; |
| 5 |
use sqlx::{PgPool, Postgres, Transaction}; |
| 6 |
|
| 7 |
|
| 8 |
#[derive(Debug, sqlx::FromRow)] |
| 9 |
#[allow(dead_code)] |
| 10 |
pub struct DbWebhookEvent { |
| 11 |
pub id: uuid::Uuid, |
| 12 |
pub source: String, |
| 13 |
pub event_type: String, |
| 14 |
pub payload: String, |
| 15 |
pub signature: Option<String>, |
| 16 |
pub status: String, |
| 17 |
pub attempts: i32, |
| 18 |
pub last_error: Option<String>, |
| 19 |
pub next_retry_at: DateTime<Utc>, |
| 20 |
pub created_at: DateTime<Utc>, |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#[tracing::instrument(skip_all)] |
| 34 |
pub async fn is_event_processed(pool: &PgPool, event_id: &str) -> Result<bool> { |
| 35 |
let exists = sqlx::query_scalar::<_, bool>( |
| 36 |
"SELECT EXISTS(SELECT 1 FROM processed_webhook_events WHERE event_id = $1)", |
| 37 |
) |
| 38 |
.bind(event_id) |
| 39 |
.fetch_one(pool) |
| 40 |
.await?; |
| 41 |
|
| 42 |
Ok(exists) |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#[tracing::instrument(skip_all)] |
| 49 |
pub async fn mark_event_processed(pool: &PgPool, event_id: &str) -> Result<()> { |
| 50 |
sqlx::query( |
| 51 |
"INSERT INTO processed_webhook_events (event_id) VALUES ($1) ON CONFLICT DO NOTHING", |
| 52 |
) |
| 53 |
.bind(event_id) |
| 54 |
.execute(pool) |
| 55 |
.await?; |
| 56 |
|
| 57 |
Ok(()) |
| 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 |
#[tracing::instrument(skip_all)] |
| 101 |
pub async fn try_lock_event<'a>( |
| 102 |
pool: &'a PgPool, |
| 103 |
event_id: &str, |
| 104 |
) -> Result<Option<Transaction<'a, Postgres>>> { |
| 105 |
let mut tx = pool.begin().await?; |
| 106 |
let acquired = sqlx::query_scalar::<_, bool>( |
| 107 |
"SELECT pg_try_advisory_xact_lock(hashtextextended('stripe_webhook:' || $1::text, 0))", |
| 108 |
) |
| 109 |
.bind(event_id) |
| 110 |
.fetch_one(&mut *tx) |
| 111 |
.await?; |
| 112 |
Ok(acquired.then_some(tx)) |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
#[tracing::instrument(skip_all)] |
| 121 |
pub async fn prune_processed_events(pool: &PgPool, days: i64) -> Result<u64> { |
| 122 |
let result = sqlx::query( |
| 123 |
"DELETE FROM processed_webhook_events \ |
| 124 |
WHERE processed_at < NOW() - make_interval(days => $1::int)", |
| 125 |
) |
| 126 |
.bind(days as i32) |
| 127 |
.execute(pool) |
| 128 |
.await?; |
| 129 |
|
| 130 |
Ok(result.rows_affected()) |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
#[tracing::instrument(skip_all)] |
| 135 |
pub async fn insert_failed_event( |
| 136 |
pool: &PgPool, |
| 137 |
source: &str, |
| 138 |
event_type: &str, |
| 139 |
payload: &str, |
| 140 |
signature: Option<&str>, |
| 141 |
error: &str, |
| 142 |
) -> Result<()> { |
| 143 |
sqlx::query( |
| 144 |
r"INSERT INTO webhook_events (source, event_type, payload, signature, last_error) |
| 145 |
VALUES ($1, $2, $3, $4, $5)", |
| 146 |
) |
| 147 |
.bind(source) |
| 148 |
.bind(event_type) |
| 149 |
.bind(payload) |
| 150 |
.bind(signature) |
| 151 |
.bind(error) |
| 152 |
.execute(pool) |
| 153 |
.await?; |
| 154 |
|
| 155 |
Ok(()) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
#[tracing::instrument(skip_all)] |
| 163 |
pub async fn get_retryable_events(pool: &PgPool) -> Result<Vec<DbWebhookEvent>> { |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
let events = sqlx::query_as::<_, DbWebhookEvent>( |
| 172 |
r" |
| 173 |
UPDATE webhook_events |
| 174 |
SET next_retry_at = NOW() + INTERVAL '2 minutes' |
| 175 |
WHERE id IN ( |
| 176 |
SELECT id FROM webhook_events |
| 177 |
WHERE status IN ('failed', 'retrying') |
| 178 |
AND attempts < 5 |
| 179 |
AND next_retry_at <= NOW() |
| 180 |
ORDER BY next_retry_at |
| 181 |
LIMIT 10 |
| 182 |
FOR UPDATE SKIP LOCKED |
| 183 |
) |
| 184 |
RETURNING * |
| 185 |
", |
| 186 |
) |
| 187 |
.fetch_all(pool) |
| 188 |
.await?; |
| 189 |
|
| 190 |
Ok(events) |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
#[tracing::instrument(skip_all)] |
| 195 |
pub async fn mark_processed(pool: &PgPool, id: uuid::Uuid) -> Result<()> { |
| 196 |
sqlx::query("DELETE FROM webhook_events WHERE id = $1") |
| 197 |
.bind(id) |
| 198 |
.execute(pool) |
| 199 |
.await?; |
| 200 |
|
| 201 |
Ok(()) |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
#[tracing::instrument(skip_all)] |
| 207 |
pub async fn schedule_retry( |
| 208 |
pool: &PgPool, |
| 209 |
id: uuid::Uuid, |
| 210 |
attempt: i32, |
| 211 |
error: &str, |
| 212 |
) -> Result<()> { |
| 213 |
let max_attempts = 5; |
| 214 |
|
| 215 |
if attempt >= max_attempts { |
| 216 |
sqlx::query( |
| 217 |
"UPDATE webhook_events SET status = 'dead', attempts = $2, last_error = $3 WHERE id = $1", |
| 218 |
) |
| 219 |
.bind(id) |
| 220 |
.bind(attempt) |
| 221 |
.bind(error) |
| 222 |
.execute(pool) |
| 223 |
.await?; |
| 224 |
} else { |
| 225 |
|
| 226 |
let delay_secs: i64 = match attempt { |
| 227 |
0 => 60, |
| 228 |
1 => 300, |
| 229 |
2 => 1800, |
| 230 |
3 => 7200, |
| 231 |
_ => 86400, |
| 232 |
}; |
| 233 |
|
| 234 |
sqlx::query( |
| 235 |
r"UPDATE webhook_events |
| 236 |
SET status = 'retrying', |
| 237 |
attempts = $2, |
| 238 |
last_error = $3, |
| 239 |
next_retry_at = NOW() + make_interval(secs => $4::double precision) |
| 240 |
WHERE id = $1", |
| 241 |
) |
| 242 |
.bind(id) |
| 243 |
.bind(attempt) |
| 244 |
.bind(error) |
| 245 |
.bind(delay_secs as f64) |
| 246 |
.execute(pool) |
| 247 |
.await?; |
| 248 |
} |
| 249 |
|
| 250 |
Ok(()) |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
#[allow(dead_code)] |
| 255 |
#[tracing::instrument(skip_all)] |
| 256 |
pub async fn get_dead_events(pool: &PgPool) -> Result<Vec<DbWebhookEvent>> { |
| 257 |
let events = sqlx::query_as::<_, DbWebhookEvent>( |
| 258 |
"SELECT * FROM webhook_events WHERE status = 'dead' ORDER BY created_at DESC LIMIT 50", |
| 259 |
) |
| 260 |
.fetch_all(pool) |
| 261 |
.await?; |
| 262 |
|
| 263 |
Ok(events) |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
#[allow(dead_code)] |
| 268 |
#[tracing::instrument(skip_all)] |
| 269 |
pub async fn retry_dead_event(pool: &PgPool, id: uuid::Uuid) -> Result<bool> { |
| 270 |
let result = sqlx::query( |
| 271 |
"UPDATE webhook_events SET status = 'failed', next_retry_at = NOW() WHERE id = $1 AND status = 'dead'", |
| 272 |
) |
| 273 |
.bind(id) |
| 274 |
.execute(pool) |
| 275 |
.await?; |
| 276 |
|
| 277 |
Ok(result.rows_affected() > 0) |
| 278 |
} |
| 279 |
|