Skip to content

Commit

Permalink
Retry database connections (#5124)
Browse files Browse the repository at this point in the history
* Retry database connections

* build: fix pr pipeline

* build: fix docker img

* change the build image to SDK

* update codeql

* v3

* fixy
  • Loading branch information
tidusjar authored Jun 26, 2024
1 parent af8b519 commit 38f7ced
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -62,4 +62,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v3
4 changes: 2 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion src/Ombi/Extensions/DatabaseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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>(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()
Expand Down
5 changes: 2 additions & 3 deletions src/dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 38f7ced

Please sign in to comment.