Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

feat: spawn webhooks and async scheduling in background (#2780)

Arun Raj Mf248fe2889c9
diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs
index 88462e7f85..afb7f110ed 100644
--- a/crates/router/src/core/payments/operations/payment_confirm.rs
+++ b/crates/router/src/core/payments/operations/payment_confirm.rs
@@ -493,7 +493,27 @@ impl<F: Clone + Send, Ctx: PaymentMethodRetrieve> Domain<F, api::PaymentsRequest
requeue: bool,
schedule_time: Option<time::PrimitiveDateTime>,
) -> CustomResult<(), errors::ApiErrorResponse> {
- helpers::add_domain_task_to_pt(self, state, payment_attempt, requeue, schedule_time).await
+ // This spawns this futures in a background thread, the exception inside this future won't affect
+ // the current thread and the lifecycle of spawn thread is not handled by runtime.
+ // So when server shutdown won't wait for this thread's completion.
+ let m_payment_attempt = payment_attempt.clone();
+ let m_state = state.clone();
+ let m_self = *self;
+ tokio::spawn(
+ async move {
+ helpers::add_domain_task_to_pt(
+ &m_self,
+ m_state.as_ref(),
+ &m_payment_attempt,
+ requeue,
+ schedule_time,
+ )
+ .await
+ }
+ .in_current_span(),
+ );
+
+ Ok(())
}
async fn get_connector<'a>(
diff --git a/crates/router/src/utils.rs b/crates/router/src/utils.rs
index 4933b4d700..83586e51d6 100644
--- a/crates/router/src/utils.rs
+++ b/crates/router/src/utils.rs
@@ -24,6 +24,7 @@ use nanoid::nanoid;
use qrcode;
use serde::de::DeserializeOwned;
use serde_json::Value;
+use tracing_futures::Instrument;
use uuid::Uuid;
pub use self::ext_traits::{OptionExt, ValidateCall};
@@ -754,21 +755,30 @@ where
payments_response
{
let m_state = state.clone();
-
- Box::pin(
- webhooks_core::create_event_and_trigger_appropriate_outgoing_webhook(
- m_state,
- merchant_account,
- business_profile,
- event_type,
- diesel_models::enums::EventClass::Payments,
- None,
- payment_id,
- diesel_models::enums::EventObjectType::PaymentDetails,
- webhooks::OutgoingWebhookContent::PaymentDetails(payments_response_json),
- ),
- )
- .await?;
+ // This spawns this futures in a background thread, the exception inside this future won't affect
+ // the current thread and the lifecycle of spawn thread is not handled by runtime.
+ // So when server shutdown won't wait for this thread's completion.
+ tokio::spawn(
+ async move {
+ Box::pin(
+ webhooks_core::create_event_and_trigger_appropriate_outgoing_webhook(
+ m_state,
+ merchant_account,
+ business_profile,
+ event_type,
+ diesel_models::enums::EventClass::Payments,
+ None,
+ payment_id,
+ diesel_models::enums::EventObjectType::PaymentDetails,
+ webhooks::OutgoingWebhookContent::PaymentDetails(
+ payments_response_json,
+ ),
+ ),
+ )
+ .await
+ }
+ .in_current_span(),
+ );
}
}