Skip to content

Resilient Startup #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rubberduckvba.Server/GitHubSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public record class GitHubSettings

public record class HangfireSettings
{
public int MaxInitializationAttempts { get; set; } = 5;
public int InitializationRetryDelaySeconds { get; set; } = 10;

public int ServerCheckIntervalMinutes { get; set; } = 15;
public int QueuePollIntervalSeconds { get; set; } = 30;
public int SchedulePollIntervalSeconds { get; set; } = 30;
Expand Down
22 changes: 21 additions & 1 deletion rubberduckvba.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using NLog.Config;
using NLog.Extensions.Logging;
using NLog.Targets;
using Polly;
using Polly.Retry;
using Rubberduck.SmartIndenter;
using RubberduckServices;
using rubberduckvba.Server.Api.Admin;
Expand Down Expand Up @@ -110,7 +112,25 @@ public static void Main(string[] args)
app.MapControllers();
app.MapFallbackToFile("/index.html");

StartHangfire(app);
var logger = app.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("App configuration completed. Starting hangfire...");

var hangfireOptions = app.Services.GetService<IOptions<HangfireSettings>>()?.Value ?? new();
new ResiliencePipelineBuilder().AddRetry(new RetryStrategyOptions
{
Delay = TimeSpan.FromSeconds(10),
MaxRetryAttempts = hangfireOptions.MaxInitializationAttempts,
OnRetry = (context) =>
{
var retryCount = context.AttemptNumber;
var delay = context.RetryDelay;

logger.LogError(context.Outcome.Exception, $"Hangfire failed to start | Retrying storage connection in {delay.TotalSeconds} seconds. Attempt {retryCount} of {hangfireOptions.MaxInitializationAttempts}");
return ValueTask.CompletedTask;
}
}).Build().Execute(() => StartHangfire(app));

logger.LogInformation("Hangfire initialization completed. Starting application...");
app.Run();
}

Expand Down
1 change: 1 addition & 0 deletions rubberduckvba.Server/rubberduckvba.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<PackageReference Include="NLog.Extensions.Logging" Version="5.4.0" />
<PackageReference Include="NLog.WindowsEventLog" Version="5.4.0" />
<PackageReference Include="Octokit" Version="14.0.0" />
<PackageReference Include="Polly" Version="8.5.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.3.1" />
</ItemGroup>
Expand Down