Skip to content

Commit fd697f5

Browse files
authored
Merge pull request #72 from rubberduck-vba/webhook
Resilient Startup
2 parents 748a75e + 9762a0f commit fd697f5

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

rubberduckvba.Server/GitHubSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public record class GitHubSettings
4747

4848
public record class HangfireSettings
4949
{
50+
public int MaxInitializationAttempts { get; set; } = 5;
51+
public int InitializationRetryDelaySeconds { get; set; } = 10;
52+
5053
public int ServerCheckIntervalMinutes { get; set; } = 15;
5154
public int QueuePollIntervalSeconds { get; set; } = 30;
5255
public int SchedulePollIntervalSeconds { get; set; } = 30;

rubberduckvba.Server/Program.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using NLog.Config;
88
using NLog.Extensions.Logging;
99
using NLog.Targets;
10+
using Polly;
11+
using Polly.Retry;
1012
using Rubberduck.SmartIndenter;
1113
using RubberduckServices;
1214
using rubberduckvba.Server.Api.Admin;
@@ -110,7 +112,25 @@ public static void Main(string[] args)
110112
app.MapControllers();
111113
app.MapFallbackToFile("/index.html");
112114

113-
StartHangfire(app);
115+
var logger = app.Services.GetRequiredService<ILogger<Program>>();
116+
logger.LogInformation("App configuration completed. Starting hangfire...");
117+
118+
var hangfireOptions = app.Services.GetService<IOptions<HangfireSettings>>()?.Value ?? new();
119+
new ResiliencePipelineBuilder().AddRetry(new RetryStrategyOptions
120+
{
121+
Delay = TimeSpan.FromSeconds(10),
122+
MaxRetryAttempts = hangfireOptions.MaxInitializationAttempts,
123+
OnRetry = (context) =>
124+
{
125+
var retryCount = context.AttemptNumber;
126+
var delay = context.RetryDelay;
127+
128+
logger.LogError(context.Outcome.Exception, $"Hangfire failed to start | Retrying storage connection in {delay.TotalSeconds} seconds. Attempt {retryCount} of {hangfireOptions.MaxInitializationAttempts}");
129+
return ValueTask.CompletedTask;
130+
}
131+
}).Build().Execute(() => StartHangfire(app));
132+
133+
logger.LogInformation("Hangfire initialization completed. Starting application...");
114134
app.Run();
115135
}
116136

rubberduckvba.Server/rubberduckvba.Server.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<PackageReference Include="NLog.Extensions.Logging" Version="5.4.0" />
3131
<PackageReference Include="NLog.WindowsEventLog" Version="5.4.0" />
3232
<PackageReference Include="Octokit" Version="14.0.0" />
33+
<PackageReference Include="Polly" Version="8.5.2" />
3334
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
3435
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.3.1" />
3536
</ItemGroup>

0 commit comments

Comments
 (0)