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 BTCPayServer.Models;
using System.Threading.Tasks;
using System.Threading;
namespace BTCPayServer.Hosting
{
@ -165,12 +166,34 @@ namespace BTCPayServer.Hosting
}
using(var scope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
//Wait the DB is ready
Retry(() =>
{
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>().Database.Migrate();
});
}
app.UseMiddleware<BTCPayMiddleware>();
return app;
}
static void Retry(Action act)
{
CancellationTokenSource cts = new CancellationTokenSource(10000);
while(true)
{
try
{
act();
}
catch
{
if(cts.IsCancellationRequested)
throw;
Thread.Sleep(1000);
}
}
}
}