refactor(revenue_recovery): Add configs for calculate job (#9106)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
Co-authored-by: Aniket Burman <aniket.burman@Aniket-Burman-JDXHW2PH34.local>
Co-authored-by: Hrithikesh <61539176+hrithikesh026@users.noreply.github.com>
This commit is contained in:
AdityaWNL
2025-08-29 20:48:55 +05:30
committed by GitHub
parent 4a60b07954
commit 8ce36a2fd5
8 changed files with 63 additions and 16 deletions

View File

@ -56,8 +56,9 @@ pub async fn upsert_calculate_pcr_task(
// Create process tracker ID in the format: CALCULATE_WORKFLOW_{payment_intent_id}
let process_tracker_id = format!("{runner}_{task}_{}", payment_id.get_string_repr());
// Set scheduled time to 1 hour from now
let schedule_time = common_utils::date_time::now() + time::Duration::hours(1);
// Scheduled time is now because this will be the first entry in
// process tracker and we dont want to wait
let schedule_time = common_utils::date_time::now();
let payment_attempt_id = payment_attempt_id
.ok_or(error_stack::report!(
@ -580,7 +581,13 @@ pub async fn perform_calculate_workflow(
update_calculate_job_schedule_time(
db,
process,
time::Duration::minutes(15),
time::Duration::seconds(
state
.conf
.revenue_recovery
.recovery_timestamp
.job_schedule_buffer_time_in_seconds,
),
scheduled_token.scheduled_at,
&connector_customer_id,
)
@ -607,7 +614,13 @@ pub async fn perform_calculate_workflow(
update_calculate_job_schedule_time(
db,
process,
time::Duration::minutes(15),
time::Duration::seconds(
state
.conf
.revenue_recovery
.recovery_timestamp
.job_schedule_buffer_time_in_seconds,
),
Some(common_utils::date_time::now()),
&connector_customer_id,
)

View File

@ -1180,7 +1180,14 @@ pub async fn reopen_calculate_workflow_on_payment_failure(
// 3. Set business status to QUEUED
// 4. Schedule for immediate execution
let new_retry_count = process.retry_count + 1;
let new_schedule_time = common_utils::date_time::now() + time::Duration::hours(1);
let new_schedule_time = common_utils::date_time::now()
+ time::Duration::seconds(
state
.conf
.revenue_recovery
.recovery_timestamp
.reopen_workflow_buffer_time_in_seconds,
);
let pt_update = storage::ProcessTrackerUpdate::Update {
name: Some(task.to_string()),

View File

@ -77,13 +77,19 @@ pub struct RevenueRecoverySettings {
#[derive(Debug, serde::Deserialize, Clone)]
pub struct RecoveryTimestamp {
pub initial_timestamp_in_hours: i64,
pub initial_timestamp_in_seconds: i64,
pub job_schedule_buffer_time_in_seconds: i64,
pub reopen_workflow_buffer_time_in_seconds: i64,
pub max_random_schedule_delay_in_seconds: i64,
}
impl Default for RecoveryTimestamp {
fn default() -> Self {
Self {
initial_timestamp_in_hours: 1,
initial_timestamp_in_seconds: 1,
job_schedule_buffer_time_in_seconds: 15,
reopen_workflow_buffer_time_in_seconds: 60,
max_random_schedule_delay_in_seconds: 300,
}
}
}

View File

@ -324,9 +324,9 @@ pub(crate) async fn get_schedule_time_for_smart_retry(
let start_time_primitive = payment_intent.created_at;
let recovery_timestamp_config = &state.conf.revenue_recovery.recovery_timestamp;
let modified_start_time_primitive = start_time_primitive.saturating_add(time::Duration::hours(
recovery_timestamp_config.initial_timestamp_in_hours,
));
let modified_start_time_primitive = start_time_primitive.saturating_add(
time::Duration::seconds(recovery_timestamp_config.initial_timestamp_in_seconds),
);
let start_time_proto = date_time::convert_to_prost_timestamp(modified_start_time_primitive);
@ -550,7 +550,8 @@ pub async fn get_token_with_schedule_time_based_on_retry_algorithm_type(
.change_context(errors::ProcessTrackerError::EApiErrorResponse)?;
}
}
let delayed_schedule_time = scheduled_time.map(add_random_delay_to_schedule_time);
let delayed_schedule_time =
scheduled_time.map(|time| add_random_delay_to_schedule_time(state, time));
Ok(delayed_schedule_time)
}
@ -776,10 +777,16 @@ pub async fn check_hard_decline(
#[cfg(feature = "v2")]
pub fn add_random_delay_to_schedule_time(
state: &SessionState,
schedule_time: time::PrimitiveDateTime,
) -> time::PrimitiveDateTime {
let mut rng = rand::thread_rng();
let random_secs = rng.gen_range(1..=3600);
let delay_limit = state
.conf
.revenue_recovery
.recovery_timestamp
.max_random_schedule_delay_in_seconds;
let random_secs = rng.gen_range(1..=delay_limit);
logger::info!("Adding random delay of {random_secs} seconds to schedule time");
schedule_time + time::Duration::seconds(random_secs)
}