diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index 2881d58d1..c19ab5268 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -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 { @@ -166,11 +167,33 @@ namespace BTCPayServer.Hosting using(var scope = app.ApplicationServices.GetService().CreateScope()) { - scope.ServiceProvider.GetRequiredService().Database.Migrate(); + //Wait the DB is ready + Retry(() => + { + scope.ServiceProvider.GetRequiredService().Database.Migrate(); + }); } app.UseMiddleware(); return app; } + + static void Retry(Action act) + { + CancellationTokenSource cts = new CancellationTokenSource(10000); + while(true) + { + try + { + act(); + } + catch + { + if(cts.IsCancellationRequested) + throw; + Thread.Sleep(1000); + } + } + } }