wait a bit if can't connect to DB

This commit is contained in:
NicolasDorier 2017-09-27 23:56:43 +09:00
parent 171065f45c
commit 4be0b2c932

View file

@ -30,6 +30,7 @@ using BTCPayServer.Services.Mails;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using BTCPayServer.Models; using BTCPayServer.Models;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Threading;
namespace BTCPayServer.Hosting namespace BTCPayServer.Hosting
{ {
@ -166,11 +167,33 @@ namespace BTCPayServer.Hosting
using(var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope()) using(var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{ {
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate(); //Wait the DB is ready
Retry(() =>
{
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
});
} }
app.UseMiddleware<BTCPayMiddleware>(); app.UseMiddleware<BTCPayMiddleware>();
return app; return app;
} }
static void Retry(Action act)
{
CancellationTokenSource cts = new CancellationTokenSource(10000);
while(true)
{
try
{
act();
}
catch
{
if(cts.IsCancellationRequested)
throw;
Thread.Sleep(1000);
}
}
}
} }