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.
import { proxyActivities } from '@temporalio/workflow';
import type * as activities from './activities';
const { withdrawMoney, depositMoney } = proxyActivities<typeof activities>({
retry: {
initialInterval: '2s', // duration before the first retry
backoffCoefficient: 2, // multiplier used for subsequent retries
maximumInterval: '1m', // maximum duration between retries
maximumAttempts: 100, // maximum number of retry attempts
},
startToCloseTimeout: '5s', //maximum time allowed for a single attempt of an Activity to execute
});
export async function reimbursementWorkflow(userId: string, amount: number): Promise<string> {
await withdrawMoney(amount);
await depositMoney(amount);
return `reimbursement to ${userId} successfully complete`;
}