From 38f7ced86058e96d8d470790547d2bd5d2815226 Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Wed, 26 Jun 2024 17:40:40 +0100 Subject: [PATCH] Retry database connections (#5124) * Retry database connections * build: fix pr pipeline * build: fix docker img * change the build image to SDK * update codeql * v3 * fixy --- .github/workflows/codeql-analysis.yml | 6 ++--- .github/workflows/pr.yml | 4 +-- src/Ombi/Extensions/DatabaseExtensions.cs | 32 ++++++++++++++++++++++- src/dockerfile | 5 ++-- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 8c870059c..b67002361 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -37,7 +37,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v1 + uses: github/codeql-action/init@v3 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -48,7 +48,7 @@ jobs: # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild - uses: github/codeql-action/autobuild@v1 + uses: github/codeql-action/autobuild@v3 # ℹī¸ Command-line programs to run using the OS shell. # 📚 https://git.io/JvXDl @@ -62,4 +62,4 @@ jobs: # make release - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + uses: github/codeql-action/analyze@v3 diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 16951f371..ea65cfaa6 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -76,10 +76,10 @@ jobs: strategy: matrix: include: - - os: win10-x64 + - os: win-x64 format: zip compression: zip - - os: win10-x86 + - os: win-x86 format: zip compression: zip - os: linux-x64 diff --git a/src/Ombi/Extensions/DatabaseExtensions.cs b/src/Ombi/Extensions/DatabaseExtensions.cs index a96011f1c..4697b1daa 100644 --- a/src/Ombi/Extensions/DatabaseExtensions.cs +++ b/src/Ombi/Extensions/DatabaseExtensions.cs @@ -3,11 +3,14 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; +using MySqlConnector; using Newtonsoft.Json; using Ombi.Helpers; using Ombi.Store.Context; using Ombi.Store.Context.MySql; using Ombi.Store.Context.Sqlite; +using Polly; +using Pomelo.EntityFrameworkCore.MySql.Storage.Internal; using SQLitePCL; namespace Ombi.Extensions @@ -120,13 +123,40 @@ public static void ConfigureSqlite(DbContextOptionsBuilder options, PerDatabaseC public static void ConfigureMySql(DbContextOptionsBuilder options, PerDatabaseConfiguration config) { - options.UseMySql(config.ConnectionString, ServerVersion.AutoDetect(config.ConnectionString), b => + if (string.IsNullOrEmpty(config.ConnectionString)) + { + throw new ArgumentNullException("ConnectionString for the MySql/Mariadb database is empty"); + } + + options.UseMySql(config.ConnectionString, GetServerVersion(config.ConnectionString), b => { //b.CharSetBehavior(Pomelo.EntityFrameworkCore.MySql.Infrastructure.CharSetBehavior.NeverAppend); // ##ISSUE, link to migrations? b.EnableRetryOnFailure(); }); } + private static ServerVersion GetServerVersion(string connectionString) + { + // Workaround Windows bug, that can lead to the following exception: + // + // MySqlConnector.MySqlException (0x80004005): SSL Authentication Error + // ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception. + // ---> System.ComponentModel.Win32Exception (0x8009030F): The message or signature supplied for verification has been altered + // + // See https://github.com/dotnet/runtime/issues/17005#issuecomment-305848835 + // + // Also workaround for the fact, that ServerVersion.AutoDetect() does not use any retrying strategy. + ServerVersion serverVersion = null; +#pragma warning disable EF1001 + var retryPolicy = Policy.Handle(exception => MySqlTransientExceptionDetector.ShouldRetryOn(exception)) +#pragma warning restore EF1001 + .WaitAndRetry(3, (count, context) => TimeSpan.FromMilliseconds(count * 250)); + + serverVersion = retryPolicy.Execute(() => serverVersion = ServerVersion.AutoDetect(connectionString)); + + return serverVersion; + } + public class DatabaseConfiguration { public DatabaseConfiguration() diff --git a/src/dockerfile b/src/dockerfile index c65db8670..772f99293 100644 --- a/src/dockerfile +++ b/src/dockerfile @@ -1,6 +1,5 @@ # build stage -FROM mcr.microsoft.com/dotnet/runtime-deps:8.0-jammy-chiseled AS build -LABEL exclaimer-signature-analytics-build=true +FROM mcr.microsoft.com/dotnet/sdk:8.0-jammy AS build ARG VERSION=1.0.0 WORKDIR /source @@ -63,7 +62,7 @@ FROM build AS publish RUN dotnet publish "Ombi.csproj" -c release --no-restore --no-build -o /app/publish -FROM mcr.microsoft.com/dotnet/aspnet:8.0-jammy-chiseled as base +FROM mcr.microsoft.com/dotnet/aspnet:8.0-jammy as base WORKDIR /src/Ombi EXPOSE 5000