| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
use std::sync::Arc; |
| 14 |
|
| 15 |
use crate::background::BackgroundTx; |
| 16 |
use crate::db::StripeAccountId; |
| 17 |
use crate::payments::PaymentProvider; |
| 18 |
|
| 19 |
|
| 20 |
#[derive(Clone, Copy, Debug)] |
| 21 |
pub enum FanSubOp { |
| 22 |
|
| 23 |
CancelAtPeriodEnd(bool), |
| 24 |
|
| 25 |
Pause, |
| 26 |
|
| 27 |
Resume, |
| 28 |
|
| 29 |
Cancel, |
| 30 |
} |
| 31 |
|
| 32 |
impl FanSubOp { |
| 33 |
fn label(self) -> &'static str { |
| 34 |
match self { |
| 35 |
FanSubOp::CancelAtPeriodEnd(true) => "set_cancel_at_period_end", |
| 36 |
FanSubOp::CancelAtPeriodEnd(false) => "clear_cancel_at_period_end", |
| 37 |
FanSubOp::Pause => "pause", |
| 38 |
FanSubOp::Resume => "resume", |
| 39 |
FanSubOp::Cancel => "cancel", |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
async fn apply( |
| 44 |
self, |
| 45 |
stripe: &Arc<dyn PaymentProvider>, |
| 46 |
sub_id: &str, |
| 47 |
account_id: &str, |
| 48 |
) -> crate::error::Result<()> { |
| 49 |
match self { |
| 50 |
FanSubOp::CancelAtPeriodEnd(c) => { |
| 51 |
stripe.set_cancel_at_period_end(sub_id, account_id, c).await |
| 52 |
} |
| 53 |
FanSubOp::Pause => stripe.pause_subscription(sub_id, account_id).await, |
| 54 |
FanSubOp::Resume => stripe.resume_subscription(sub_id, account_id).await, |
| 55 |
FanSubOp::Cancel => stripe.cancel_subscription(sub_id, account_id).await, |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
pub async fn run_fan_sub_fanout( |
| 78 |
stripe: &Arc<dyn PaymentProvider>, |
| 79 |
account_id: &StripeAccountId, |
| 80 |
sub_ids: &[String], |
| 81 |
op: FanSubOp, |
| 82 |
) -> usize { |
| 83 |
let mut failed = 0usize; |
| 84 |
for sub_id in sub_ids { |
| 85 |
if let Err(e) = op.apply(stripe, sub_id, account_id.as_str()).await { |
| 86 |
failed += 1; |
| 87 |
tracing::warn!(stripe_sub_id = %sub_id, op = op.label(), error = ?e, "fan subscription op failed (inline)"); |
| 88 |
} |
| 89 |
} |
| 90 |
if failed > 0 { |
| 91 |
tracing::warn!( |
| 92 |
total = sub_ids.len(), |
| 93 |
failed, |
| 94 |
op = op.label(), |
| 95 |
"inline fan subscription fan-out completed with failures" |
| 96 |
); |
| 97 |
} |
| 98 |
failed |
| 99 |
} |
| 100 |
|
| 101 |
pub fn spawn_fan_sub_fanout( |
| 102 |
bg: &BackgroundTx, |
| 103 |
stripe: Arc<dyn PaymentProvider>, |
| 104 |
account_id: StripeAccountId, |
| 105 |
sub_ids: Vec<String>, |
| 106 |
op: FanSubOp, |
| 107 |
wam: Option<crate::wam_client::WamClient>, |
| 108 |
) { |
| 109 |
if sub_ids.is_empty() { |
| 110 |
return; |
| 111 |
} |
| 112 |
bg.spawn("fan-sub stripe fan-out", async move { |
| 113 |
let total = sub_ids.len(); |
| 114 |
let mut failed_ids: Vec<&str> = Vec::new(); |
| 115 |
for sub_id in &sub_ids { |
| 116 |
if let Err(e) = op.apply(&stripe, sub_id, account_id.as_str()).await { |
| 117 |
failed_ids.push(sub_id); |
| 118 |
tracing::warn!(stripe_sub_id = %sub_id, op = op.label(), error = ?e, "fan subscription op failed"); |
| 119 |
} |
| 120 |
} |
| 121 |
if failed_ids.is_empty() { |
| 122 |
tracing::debug!(total, op = op.label(), "fan subscription fan-out completed"); |
| 123 |
return; |
| 124 |
} |
| 125 |
let failed = failed_ids.len(); |
| 126 |
tracing::warn!(total, failed, op = op.label(), "fan subscription fan-out completed with failures"); |
| 127 |
if let Some(wam) = wam { |
| 128 |
let title = format!( |
| 129 |
"Fan subscription fan-out incomplete: {} of {total} '{}' ops failed", |
| 130 |
failed, |
| 131 |
op.label() |
| 132 |
); |
| 133 |
let body = format!( |
| 134 |
"Applying '{}' to a creator's fan subscriptions (account {}) left {failed} of {total} \ |
| 135 |
unreconciled. These subscriptions may still be charging (or retaining access) against \ |
| 136 |
the creator's current state and need manual reconciliation in Stripe.\n\nFailed subscription IDs:\n{}", |
| 137 |
op.label(), |
| 138 |
account_id.as_str(), |
| 139 |
failed_ids.join("\n"), |
| 140 |
); |
| 141 |
wam.create_ticket(&title, Some(&body), "high", "fan-fanout-incomplete", Some(account_id.as_str())).await; |
| 142 |
} |
| 143 |
}); |
| 144 |
} |
| 145 |
|