From bf8abf64e8f0152d2c2a93c6cb4cc0f81e3981d7 Mon Sep 17 00:00:00 2001 From: Giorgi <740702+giorgi1324@users.noreply.github.com> Date: Fri, 15 Nov 2024 19:04:45 +0000 Subject: [PATCH] Add a new DB COMMAND_TIMEOUT_SEC env variable. Use it in every DB connection consistently, default it to 5 minutes. --- .../Features/Configuration/PostgresConfiguration.cs | 4 +++- .../Features/ImportImdbData/ImdbDbService.cs | 12 +----------- src/shared/Configuration/PostgresConfiguration.cs | 4 +++- .../Features/DataProcessing/PostgresConfiguration.cs | 4 +++- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/metadata/Features/Configuration/PostgresConfiguration.cs b/src/metadata/Features/Configuration/PostgresConfiguration.cs index c7e465c2..826a9d97 100644 --- a/src/metadata/Features/Configuration/PostgresConfiguration.cs +++ b/src/metadata/Features/Configuration/PostgresConfiguration.cs @@ -8,12 +8,14 @@ public class PostgresConfiguration private const string PasswordVariable = "PASSWORD"; private const string DatabaseVariable = "DB"; private const string PortVariable = "PORT"; + private const string CommandTimeoutVariable = "COMMAND_TIMEOUT_SEC"; // Seconds private string Host { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(HostVariable); private string Username { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(UsernameVariable); private string Password { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(PasswordVariable); private string Database { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(DatabaseVariable); private int PORT { get; init; } = Prefix.GetEnvironmentVariableAsInt(PortVariable, 5432); + private int CommandTimeout { get; init; } = Prefix.GetEnvironmentVariableAsInt(CommandTimeoutVariable, 300); - public string StorageConnectionString => $"Host={Host};Port={PORT};Username={Username};Password={Password};Database={Database};"; + public string StorageConnectionString => $"Host={Host};Port={PORT};Username={Username};Password={Password};Database={Database};CommandTimeout={CommandTimeout}"; } diff --git a/src/metadata/Features/ImportImdbData/ImdbDbService.cs b/src/metadata/Features/ImportImdbData/ImdbDbService.cs index 4c584c0c..ce01782f 100644 --- a/src/metadata/Features/ImportImdbData/ImdbDbService.cs +++ b/src/metadata/Features/ImportImdbData/ImdbDbService.cs @@ -134,7 +134,7 @@ private async Task ExecuteCommandAsync(Func operation, s { try { - await using var connection = CreateNpgsqlConnection(); + await using var connection = new NpgsqlConnection(configuration.StorageConnectionString); await connection.OpenAsync(); await operation(connection); @@ -145,16 +145,6 @@ private async Task ExecuteCommandAsync(Func operation, s } } - private NpgsqlConnection CreateNpgsqlConnection() - { - var connectionStringBuilder = new NpgsqlConnectionStringBuilder(configuration.StorageConnectionString) - { - CommandTimeout = 3000, - }; - - return new(connectionStringBuilder.ConnectionString); - } - private async Task ExecuteCommandWithTransactionAsync(Func operation, NpgsqlTransaction transaction, string errorMessage) { try diff --git a/src/shared/Configuration/PostgresConfiguration.cs b/src/shared/Configuration/PostgresConfiguration.cs index ca12d592..069248db 100644 --- a/src/shared/Configuration/PostgresConfiguration.cs +++ b/src/shared/Configuration/PostgresConfiguration.cs @@ -8,12 +8,14 @@ public class PostgresConfiguration private const string PasswordVariable = "PASSWORD"; private const string DatabaseVariable = "DB"; private const string PortVariable = "PORT"; + private const string CommandTimeoutVariable = "COMMAND_TIMEOUT_SEC"; // Seconds private string Host { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(HostVariable); private string Username { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(UsernameVariable); private string Password { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(PasswordVariable); private string Database { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(DatabaseVariable); private int PORT { get; init; } = Prefix.GetEnvironmentVariableAsInt(PortVariable, 5432); + private int CommandTimeout { get; init; } = Prefix.GetEnvironmentVariableAsInt(CommandTimeoutVariable, 300); - public string StorageConnectionString => $"Host={Host};Port={PORT};Username={Username};Password={Password};Database={Database};"; + public string StorageConnectionString => $"Host={Host};Port={PORT};Username={Username};Password={Password};Database={Database};CommandTimeout={CommandTimeout}"; } diff --git a/src/tissue/Features/DataProcessing/PostgresConfiguration.cs b/src/tissue/Features/DataProcessing/PostgresConfiguration.cs index a878c0e2..b37e3d1d 100644 --- a/src/tissue/Features/DataProcessing/PostgresConfiguration.cs +++ b/src/tissue/Features/DataProcessing/PostgresConfiguration.cs @@ -8,12 +8,14 @@ public class PostgresConfiguration private const string PasswordVariable = "PASSWORD"; private const string DatabaseVariable = "DB"; private const string PortVariable = "PORT"; + private const string CommandTimeoutVariable = "COMMAND_TIMEOUT_SEC"; // Seconds private string Host { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(HostVariable); private string Username { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(UsernameVariable); private string Password { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(PasswordVariable); private string Database { get; init; } = Prefix.GetRequiredEnvironmentVariableAsString(DatabaseVariable); private int PORT { get; init; } = Prefix.GetEnvironmentVariableAsInt(PortVariable, 5432); + private int CommandTimeout { get; init; } = Prefix.GetEnvironmentVariableAsInt(CommandTimeoutVariable, 300); - public string StorageConnectionString => $"Host={Host};Port={PORT};Username={Username};Password={Password};Database={Database};"; + public string StorageConnectionString => $"Host={Host};Port={PORT};Username={Username};Password={Password};Database={Database};CommandTimeout={CommandTimeout}"; }