From 4be0b2c93256db07fad6e8cb5d898bc9abf93039 Mon Sep 17 00:00:00 2001 From: NicolasDorier Date: Wed, 27 Sep 2017 23:56:43 +0900 Subject: [PATCH] wait a bit if can't connect to DB --- BTCPayServer/Hosting/BTCPayServerServices.cs | 25 +++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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); + } + } + } }