Adding a Retry Policy
Let’s go ahead and add a retry policy and timeout to our Workflow code. Remember, your Activity contains code that is prone to failure. By default, Temporal automatically retries failed Activities until it either succeeds or is canceled. You can also override the default retry policies like in the code sample.
With the following configuration, your Activities will retry up to 100 times with an exponential backoff — waiting 2 seconds before the first retry, doubling the delay after each failure up to a 1-minute cap — and each attempt can run for at most 5 seconds.
class ReimbursementWorkflow < Temporalio::Workflow::Definition
def execute(user_id, amount)
Temporalio::Workflow.execute_activity(
WithdrawMoneyActivity,
amount,
start_to_close_timeout: 5, # maximum time allowed for a single attempt of an Activity to execute
retry_policy: Temporalio::RetryPolicy.new(
initial_interval: 2, # duration before the first retry
backoff_coefficient: 2, # multiplier used for subsequent retries
max_interval: 60, # maximum duration between retries
max_attempts: 100 # maximum number of retry attempts before giving up
)
)
Temporalio::Workflow.execute_activity(
DepositMoneyActivity,
amount,
start_to_close_timeout: 5,
retry_policy: Temporalio::RetryPolicy.new(
initial_interval: 2,
backoff_coefficient: 2,
max_interval: 60,
max_attempts: 100
)
)
"reimbursement to #{user_id} successfully complete"
end
end