Skip to content

Commit f248fe2

Browse files
jarnuragithub-actions[bot]akshay-97akshay.sdracarys18
authored
feat: spawn webhooks and async scheduling in background (#2780)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: akshay-97 <[email protected]> Co-authored-by: akshay.s <[email protected]> Co-authored-by: Kartikeya Hegde <[email protected]>
1 parent bd55e57 commit f248fe2

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

crates/router/src/core/payments/operations/payment_confirm.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,27 @@ impl<F: Clone + Send, Ctx: PaymentMethodRetrieve> Domain<F, api::PaymentsRequest
493493
requeue: bool,
494494
schedule_time: Option<time::PrimitiveDateTime>,
495495
) -> CustomResult<(), errors::ApiErrorResponse> {
496-
helpers::add_domain_task_to_pt(self, state, payment_attempt, requeue, schedule_time).await
496+
// This spawns this futures in a background thread, the exception inside this future won't affect
497+
// the current thread and the lifecycle of spawn thread is not handled by runtime.
498+
// So when server shutdown won't wait for this thread's completion.
499+
let m_payment_attempt = payment_attempt.clone();
500+
let m_state = state.clone();
501+
let m_self = *self;
502+
tokio::spawn(
503+
async move {
504+
helpers::add_domain_task_to_pt(
505+
&m_self,
506+
m_state.as_ref(),
507+
&m_payment_attempt,
508+
requeue,
509+
schedule_time,
510+
)
511+
.await
512+
}
513+
.in_current_span(),
514+
);
515+
516+
Ok(())
497517
}
498518

499519
async fn get_connector<'a>(

crates/router/src/utils.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use nanoid::nanoid;
2424
use qrcode;
2525
use serde::de::DeserializeOwned;
2626
use serde_json::Value;
27+
use tracing_futures::Instrument;
2728
use uuid::Uuid;
2829

2930
pub use self::ext_traits::{OptionExt, ValidateCall};
@@ -754,21 +755,30 @@ where
754755
payments_response
755756
{
756757
let m_state = state.clone();
757-
758-
Box::pin(
759-
webhooks_core::create_event_and_trigger_appropriate_outgoing_webhook(
760-
m_state,
761-
merchant_account,
762-
business_profile,
763-
event_type,
764-
diesel_models::enums::EventClass::Payments,
765-
None,
766-
payment_id,
767-
diesel_models::enums::EventObjectType::PaymentDetails,
768-
webhooks::OutgoingWebhookContent::PaymentDetails(payments_response_json),
769-
),
770-
)
771-
.await?;
758+
// This spawns this futures in a background thread, the exception inside this future won't affect
759+
// the current thread and the lifecycle of spawn thread is not handled by runtime.
760+
// So when server shutdown won't wait for this thread's completion.
761+
tokio::spawn(
762+
async move {
763+
Box::pin(
764+
webhooks_core::create_event_and_trigger_appropriate_outgoing_webhook(
765+
m_state,
766+
merchant_account,
767+
business_profile,
768+
event_type,
769+
diesel_models::enums::EventClass::Payments,
770+
None,
771+
payment_id,
772+
diesel_models::enums::EventObjectType::PaymentDetails,
773+
webhooks::OutgoingWebhookContent::PaymentDetails(
774+
payments_response_json,
775+
),
776+
),
777+
)
778+
.await
779+
}
780+
.in_current_span(),
781+
);
772782
}
773783
}
774784

0 commit comments

Comments
 (0)