Google Interview Question
Software EngineersCountry: United States
Interview Type: Phone Interview
public static class RetryHelper
{
private static ILog logger = LogManager.GetLogger(); //use a logger or trace of your choice
public static void RetryOnException(int times, TimeSpan delay, Action operation)
{
var attempts = 0;
do
{
try
{
attempts++;
operation();
break; // Sucess! Lets exit the loop!
}
catch (Exception ex)
{
if (attempts == times)
throw;
logger.Error($"Exception caught on attempt {attempts} - will retry after delay {delay}", ex);
Task.Delay(delay).Wait();
}
} while (true);
}
}
j
- Anonymous February 17, 2019