fix(web): retry Hangfire startup lock contention
Heimdall Gitea / Detect CI scope (pull_request) Successful in 6s
Heimdall Gitea / Build and tests (pull_request) Successful in 20m51s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan server image (pull_request) Successful in 8m34s
Heimdall Gitea / Build and scan agent image (pull_request) Successful in 9m53s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Successful in 27m0s
Heimdall Gitea / CI policy (pull_request) Successful in 5s
Heimdall Gitea / Detect CI scope (pull_request) Successful in 6s
Heimdall Gitea / Build and tests (pull_request) Successful in 20m51s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan server image (pull_request) Successful in 8m34s
Heimdall Gitea / Build and scan agent image (pull_request) Successful in 9m53s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Successful in 27m0s
Heimdall Gitea / CI policy (pull_request) Successful in 5s
This commit is contained in:
1 parent
4498e39d34
commit
6381bbd0b5
3 files changed
+83
-5
No files matched your search
@@ -61,11 +61,7 @@ var isTesting = app.Environment.IsEnvironment(TestingEnvironment);
|
||||
var pgConnStr = Environment.GetEnvironmentVariable("DATABASE_URL") ?? app.Configuration.GetConnectionString("Default");
|
||||
if (!isTesting && !string.IsNullOrEmpty(pgConnStr))
|
||||
{
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var manager = scope.ServiceProvider.GetRequiredService<IRecurringJobManager>();
|
||||
Heimdall.Infrastructure.BackgroundJobs.JobSetup.ConfigureRecurringJobs(manager);
|
||||
}
|
||||
await Program.ConfigureRecurringJobsAsync(app.Services, app.Lifetime.ApplicationStopping);
|
||||
}
|
||||
|
||||
// ── Seed Identity roles (required in ALL environments, including Testing) ─
|
||||
@@ -85,10 +81,37 @@ internal sealed record SignInRequest(
|
||||
public partial class Program
|
||||
{
|
||||
private const string TestingEnvironment = "Testing";
|
||||
private const int RecurringJobRegistrationAttempts = 5;
|
||||
|
||||
// SonarQube S1118: partial class used by WebApplicationFactory needs a constructor.
|
||||
protected Program() { }
|
||||
|
||||
internal static async Task ConfigureRecurringJobsAsync(IServiceProvider services, CancellationToken cancellationToken)
|
||||
{
|
||||
var retryDelay = TimeSpan.FromSeconds(2);
|
||||
var logger = services.GetRequiredService<ILogger<Program>>();
|
||||
for (var attempt = 1; attempt <= RecurringJobRegistrationAttempts; attempt++)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
try
|
||||
{
|
||||
using var scope = services.CreateScope();
|
||||
var manager = scope.ServiceProvider.GetRequiredService<IRecurringJobManager>();
|
||||
Heimdall.Infrastructure.BackgroundJobs.JobSetup.ConfigureRecurringJobs(manager);
|
||||
return;
|
||||
}
|
||||
catch (Hangfire.PostgreSql.PostgreSqlDistributedLockException ex)
|
||||
when (attempt < RecurringJobRegistrationAttempts)
|
||||
{
|
||||
logger.LogWarning(ex,
|
||||
"Recurring Hangfire job registration hit a distributed lock (attempt {Attempt}/{MaxAttempts}); retrying in {DelaySeconds}s.",
|
||||
attempt, RecurringJobRegistrationAttempts, retryDelay.TotalSeconds);
|
||||
await Task.Delay(retryDelay, cancellationToken);
|
||||
retryDelay = TimeSpan.FromSeconds(Math.Min(retryDelay.TotalSeconds * 2, 16));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string ResolveCategory(string key)
|
||||
{
|
||||
if (key.StartsWith("ntfy.", StringComparison.Ordinal) || key.StartsWith("email.", StringComparison.Ordinal))
|
||||
|
||||
Reference in new issue
Block a user