Created
February 13, 2018 18:43
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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