From 4d4dfc6cadde3904b35eaf7dcff444aa6720c385 Mon Sep 17 00:00:00 2001 From: nenad0707 Date: Fri, 7 Jun 2024 15:49:28 +0200 Subject: [PATCH] Refactored PingHelper.cs and deleted PingDatabase method --- TodoApi/Helpers/PingHelper.cs | 41 ------------------- .../HangfireMiddlewareExtensions.cs | 6 --- 2 files changed, 47 deletions(-) diff --git a/TodoApi/Helpers/PingHelper.cs b/TodoApi/Helpers/PingHelper.cs index f9fd62d..36db66b 100644 --- a/TodoApi/Helpers/PingHelper.cs +++ b/TodoApi/Helpers/PingHelper.cs @@ -44,27 +44,6 @@ public async Task PingApi() } } - /// - /// Pings the database to check if it is up and running. - /// - /// True if the database is up, otherwise false. - public async Task PingDatabase() - { - try - { - using (var connection = new SqlConnection(_connectionString)) - { - var result = await connection.ExecuteScalarAsync("SELECT 1"); - return result == 1; - } - } - catch (Exception ex) - { - Console.WriteLine($"Error pinging database: {ex.Message}"); - return false; - } - } - /// /// Tries to ping the API with retries in case of failures. /// @@ -84,24 +63,4 @@ public async Task PingApiWithRetry() } Console.WriteLine("API is down after multiple attempts"); } - - /// - /// Tries to ping the database with retries in case of failures. - /// - public async Task PingDatabaseWithRetry() - { - const int maxAttempts = 5; - const int delay = 5000; // 5 seconds delay between retries - - for (int attempt = 0; attempt < maxAttempts; attempt++) - { - if (await PingDatabase()) - { - Console.WriteLine("Database is up"); - return; - } - await Task.Delay(delay); - } - Console.WriteLine("Database is down after multiple attempts"); - } } diff --git a/TodoApi/StartupConfig/HangfireMiddlewareExtensions.cs b/TodoApi/StartupConfig/HangfireMiddlewareExtensions.cs index aa9a431..034e2ec 100644 --- a/TodoApi/StartupConfig/HangfireMiddlewareExtensions.cs +++ b/TodoApi/StartupConfig/HangfireMiddlewareExtensions.cs @@ -23,12 +23,6 @@ public static IApplicationBuilder UseHangfireJobs(this IApplicationBuilder app) () => serviceProvider.CreateScope().ServiceProvider.GetRequiredService().PingApiWithRetry(), "*/15 * * * *"); // Adjust to "*/5 * * * *" if needed - // Adds or updates a recurring job to ping a database every 15 minutes. - RecurringJob.AddOrUpdate( - "PingDatabase", - () => serviceProvider.CreateScope().ServiceProvider.GetRequiredService().PingDatabaseWithRetry(), - "*/15 * * * *"); // Adjust to "*/5 * * * *" if needed - return app; } } \ No newline at end of file