aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-08-30 08:50:15 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-08-30 08:50:15 -0500
commitac7e86e473e01b6de6c2dab60d5702a7a6ae49f0 (patch)
tree11e1cdc0aea9834f9c0104b7096b871293fd51c9 /src
parent27eeeaa3f21d5f6cae123fd725d7de382a5e27b5 (diff)
downloadslskd-ac7e86e473e01b6de6c2dab60d5702a7a6ae49f0.tar.gz
initialize DbContext instances at startup
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Startup.cs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs
index 0a4133f2..15ae2025 100644
--- a/src/slskd/Startup.cs
+++ b/src/slskd/Startup.cs
@@ -233,6 +233,11 @@ namespace slskd
{
var logger = Log.ForContext<Startup>();
+ // initialize DbContext instances to avoid race conditions when a db needs to be
+ // created from scratch. note that this method needs to be updated to include each
+ // DbContext explicitly.
+ InitializeDbContexts(app);
+
app.UseCors("AllowAll");
if (OptionsAtStartup.Web.Https.Force)
@@ -332,5 +337,27 @@ namespace slskd
app.UseFileServer(fileServerOptions);
}
}
+
+ private void InitializeDbContexts(IApplicationBuilder app)
+ {
+ var logger = Log.ForContext<Startup>();
+
+ IDbContextFactory<T> GetFactory<T>()
+ where T : DbContext
+ => app.ApplicationServices.GetRequiredService<IDbContextFactory<T>>();
+
+ logger.Debug("Initializing database contexts");
+
+ try
+ {
+ using var search = GetFactory<SearchDbContext>().CreateDbContext();
+ using var peer = GetFactory<PeerDbContext>().CreateDbContext();
+ }
+ catch (Exception ex)
+ {
+ logger.Error(ex, "Failed to initialize one or more application databases");
+ throw;
+ }
+ }
}
} \ No newline at end of file