diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-08-30 20:49:00 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-30 20:49:00 -0500 |
| commit | c24404398b86e84c63667abbf6e161e2647d1aad (patch) | |
| tree | 11e1cdc0aea9834f9c0104b7096b871293fd51c9 /src | |
| parent | 27eeeaa3f21d5f6cae123fd725d7de382a5e27b5 (diff) | |
| parent | ac7e86e473e01b6de6c2dab60d5702a7a6ae49f0 (diff) | |
| download | slskd-c24404398b86e84c63667abbf6e161e2647d1aad.tar.gz | |
Merge pull request #175 from slskd/init-db
Initialize DbContext instances at startup
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Startup.cs | 27 |
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 |
