Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Created February 13, 2018 18:43
Show Gist options
  • Select an option

  • Save ramonsmits/60e5a728f7bec2a03a02b3bb0053a01d to your computer and use it in GitHub Desktop.

Select an option

Save ramonsmits/60e5a728f7bec2a03a02b3bb0053a01d to your computer and use it in GitHub Desktop.
NServiceBus StartWithRetry extension method to retry the start for a number of times
static class StartableEndpointExtensions
{
public static async Task<IEndpointInstance> StartWithRetry(
this IStartableEndpoint startableEndpoint,
int maxAttempts = 10
)
{
var attempts = 0;
while (true)
{
try
{
return await startableEndpoint.Start()
.ConfigureAwait(false);
}
catch (Exception ex)
{
if (attempts > maxAttempts) throw;
var delay = 100 * (int)Math.Pow(2, attempts++); // Exponential back off;
await Console.Out.WriteLineAsync($"Startup failed, retry attempt {attempts} reason: {ex.Message}")
.ConfigureAwait(false);
await Task.Delay(delay)
.ConfigureAwait(false);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment