Skip to content

Commit

Permalink
Refactored PingHelper.cs and deleted PingDatabase method
Browse files Browse the repository at this point in the history
  • Loading branch information
nenad0707 committed Jun 7, 2024
1 parent 0ec5f07 commit 4d4dfc6
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 47 deletions.
41 changes: 0 additions & 41 deletions TodoApi/Helpers/PingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,6 @@ public async Task<bool> PingApi()
}
}

/// <summary>
/// Pings the database to check if it is up and running.
/// </summary>
/// <returns>True if the database is up, otherwise false.</returns>
public async Task<bool> PingDatabase()
{
try
{
using (var connection = new SqlConnection(_connectionString))
{
var result = await connection.ExecuteScalarAsync<int>("SELECT 1");
return result == 1;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error pinging database: {ex.Message}");
return false;
}
}

/// <summary>
/// Tries to ping the API with retries in case of failures.
/// </summary>
Expand All @@ -84,24 +63,4 @@ public async Task PingApiWithRetry()
}
Console.WriteLine("API is down after multiple attempts");
}

/// <summary>
/// Tries to ping the database with retries in case of failures.
/// </summary>
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");
}
}
6 changes: 0 additions & 6 deletions TodoApi/StartupConfig/HangfireMiddlewareExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ public static IApplicationBuilder UseHangfireJobs(this IApplicationBuilder app)
() => serviceProvider.CreateScope().ServiceProvider.GetRequiredService<PingHelper>().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<PingHelper>().PingDatabaseWithRetry(),
"*/15 * * * *"); // Adjust to "*/5 * * * *" if needed

return app;
}
}

0 comments on commit 4d4dfc6

Please sign in to comment.