Skip to content

Instantly share code, notes, and snippets.

@ircnelson
Last active October 1, 2020 03:54
Show Gist options
  • Save ircnelson/870be696c472ae4c123fbfe017608c16 to your computer and use it in GitHub Desktop.
Save ircnelson/870be696c472ae4c123fbfe017608c16 to your computer and use it in GitHub Desktop.
public class Program
{
private static void Main(string[] args)
{
var host = Host.CreateDefaultBuilder(args).Build();
using var scope = host.Services.CreateScope();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
var configuration = scope.ServiceProvider.GetService<IConfiguration>();
const int retries = 10;
var retry = Policy.Handle<SqlException>()
.WaitAndRetry(
retryCount: retries,
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
onRetry: (exception, timeSpan, attemptRetry, ctx) =>
{
logger.LogWarning(exception, "{message} :: [attempt {retry} of {retries}]",
exception.Message,
attemptRetry,
retries);
});
retry.Execute(() =>
{
/*
* Your migration routine
*
* E.g: yourDbContext.Database.Migrate()
*/
});
logger.LogInformation("Database migrations ended without errors.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment