From 4ed43e6e6f9c3ca38dd73f91c259ee9b3513023c Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Mon, 5 Aug 2024 17:15:38 -0400 Subject: [PATCH] collab: Ignore Stripe events that are older than an hour (#15830) This PR makes it so any Stripe events we receive that occurred over an hour ago are marked as processed. We don't want to process an old event long after it occurred and potentially overwrite more recent updates. This also makes running collab locally a bit nicer, as we won't be getting errors for a bunch of older events that will never get processed successfully. The period after time after which we consider an event "stale" can be modified, as needed. Release Notes: - N/A --- crates/collab/src/api/billing.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/collab/src/api/billing.rs b/crates/collab/src/api/billing.rs index 916d669d9d..bac0258ead 100644 --- a/crates/collab/src/api/billing.rs +++ b/crates/collab/src/api/billing.rs @@ -8,7 +8,7 @@ use axum::{ routing::{get, post}, Extension, Json, Router, }; -use chrono::{DateTime, SecondsFormat}; +use chrono::{DateTime, SecondsFormat, Utc}; use reqwest::StatusCode; use sea_orm::ActiveValue; use serde::{Deserialize, Serialize}; @@ -427,6 +427,24 @@ async fn poll_stripe_events( stripe_event_created_timestamp: event.created, }; + // If the event has happened too far in the past, we don't want to + // process it and risk overwriting other more-recent updates. + // + // 1 hour was chosen arbitrarily. This could be made longer or shorter. + let one_hour = Duration::from_secs(60 * 60); + let an_hour_ago = Utc::now() - one_hour; + if an_hour_ago.timestamp() > event.created { + log::info!( + "Stripe event {} is more than {one_hour:?} old, marking as processed", + event_id + ); + app.db + .create_processed_stripe_event(&processed_event_params) + .await?; + + return Ok(()); + } + let process_result = match event.type_ { EventType::CustomerCreated | EventType::CustomerUpdated => { handle_customer_event(app, stripe_client, event).await