Skip to content

Commit d0d0dcf

Browse files
committed
Fix billing setup
1 parent 41b9729 commit d0d0dcf

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed
Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/labrinth/src/database/models/charge_item.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ impl ChargeItem {
206206
Ok(res.and_then(|r| r.try_into().ok()))
207207
}
208208

209-
pub async fn get_chargeable(
210-
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
209+
pub async fn get_chargeable_lock(
210+
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
211211
) -> Result<Vec<ChargeItem>, DatabaseError> {
212212
let charge_type = ChargeType::Subscription.as_str();
213213
let res = select_charges_with_predicate!(
@@ -218,10 +218,11 @@ impl ChargeItem {
218218
(status = 'open' AND due < NOW()) OR
219219
(status = 'failed' AND last_attempt < NOW() - INTERVAL '2 days')
220220
)
221+
FOR UPDATE SKIP LOCKED
221222
"#,
222223
charge_type
223224
)
224-
.fetch_all(exec)
225+
.fetch_all(&mut **transaction)
225226
.await?;
226227

227228
Ok(res

apps/labrinth/src/routes/internal/billing.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,9 +2271,11 @@ pub async fn index_billing(
22712271
info!("Indexing billing queue");
22722272
let res = async {
22732273
// If a charge is open and due or has been attempted more than two days ago, it should be processed
2274+
let mut transaction = pool.begin().await?;
2275+
22742276
let charges_to_do =
2275-
crate::database::models::charge_item::ChargeItem::get_chargeable(
2276-
&pool,
2277+
crate::database::models::charge_item::ChargeItem::get_chargeable_lock(
2278+
&mut transaction,
22772279
)
22782280
.await?;
22792281

@@ -2396,8 +2398,11 @@ pub async fn index_billing(
23962398

23972399
charge.status = ChargeStatus::Processing;
23982400

2399-
stripe::PaymentIntent::create(&stripe_client, intent)
2400-
.await?;
2401+
if let Err(e) = stripe::PaymentIntent::create(&stripe_client, intent).await {
2402+
tracing::error!("Failed to create payment intent: {:?}", e);
2403+
charge.status = ChargeStatus::Failed;
2404+
charge.last_attempt = Some(Utc::now());
2405+
}
24012406
} else {
24022407
charge.status = ChargeStatus::Failed;
24032408
charge.last_attempt = Some(Utc::now());

0 commit comments

Comments
 (0)