-
Notifications
You must be signed in to change notification settings - Fork 0
VPR-54 feat(scheduler): Hangfire-backed scheduler #182
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
Open
rlorenzo
wants to merge
2
commits into
main
Choose a base branch
from
VPR-54-hangfire
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
| using NSubstitute; | ||
| using Viper.Classes.HealthChecks; | ||
|
|
||
| namespace Viper.test.HealthChecks | ||
| { | ||
| public class HangfireHealthCheckTests | ||
| { | ||
| private static HealthCheckContext CreateContext(HangfireHealthCheck sut) | ||
| { | ||
| return new HealthCheckContext | ||
| { | ||
| Registration = new HealthCheckRegistration("hangfire", sut, null, null) | ||
| }; | ||
| } | ||
|
|
||
| private static (Hangfire.JobStorage storage, Hangfire.Storage.IMonitoringApi monitoring) CreateStorage() | ||
| { | ||
| var monitoring = Substitute.For<Hangfire.Storage.IMonitoringApi>(); | ||
| var storage = Substitute.For<Hangfire.JobStorage>(); | ||
| storage.GetMonitoringApi().Returns(monitoring); | ||
| return (storage, monitoring); | ||
| } | ||
|
|
||
| private static Hangfire.Storage.Monitoring.StatisticsDto SampleStats(long servers = 1) => new() | ||
| { | ||
| Servers = servers, | ||
| Enqueued = 2, | ||
| Scheduled = 3, | ||
| Processing = 4, | ||
| Failed = 5, | ||
| Recurring = 6 | ||
| }; | ||
|
|
||
| [Fact] | ||
| public async Task CheckHealthAsync_HealthyWhenServersHaveRecentHeartbeats() | ||
| { | ||
| var (storage, monitoring) = CreateStorage(); | ||
| monitoring.GetStatistics().Returns(SampleStats()); | ||
| monitoring.Servers().Returns(new List<Hangfire.Storage.Monitoring.ServerDto> | ||
| { | ||
| new() { Name = "srv-1", Heartbeat = DateTime.UtcNow } | ||
| }); | ||
|
|
||
| var sut = new HangfireHealthCheck(storage); | ||
| var result = await sut.CheckHealthAsync(CreateContext(sut)); | ||
|
|
||
| Assert.Equal(HealthStatus.Healthy, result.Status); | ||
| Assert.Contains("Hangfire OK", result.Description); | ||
| Assert.Equal(1L, result.Data["servers"]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CheckHealthAsync_DegradedWhenNoServersRegistered() | ||
| { | ||
| var (storage, monitoring) = CreateStorage(); | ||
| monitoring.GetStatistics().Returns(SampleStats(0)); | ||
| monitoring.Servers().Returns(new List<Hangfire.Storage.Monitoring.ServerDto>()); | ||
|
|
||
| var sut = new HangfireHealthCheck(storage); | ||
| var result = await sut.CheckHealthAsync(CreateContext(sut)); | ||
|
|
||
| Assert.Equal(HealthStatus.Degraded, result.Status); | ||
| Assert.Contains("no servers registered", result.Description); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CheckHealthAsync_UnhealthyWhenAllHeartbeatsStale() | ||
| { | ||
| var (storage, monitoring) = CreateStorage(); | ||
| monitoring.GetStatistics().Returns(SampleStats()); | ||
| monitoring.Servers().Returns(new List<Hangfire.Storage.Monitoring.ServerDto> | ||
| { | ||
| new() { Name = "srv-stale", Heartbeat = DateTime.UtcNow.AddMinutes(-10) } | ||
| }); | ||
|
|
||
| var sut = new HangfireHealthCheck(storage); | ||
| var result = await sut.CheckHealthAsync(CreateContext(sut)); | ||
|
|
||
| Assert.Equal(HealthStatus.Unhealthy, result.Status); | ||
| Assert.Contains("stale", result.Description); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CheckHealthAsync_UnhealthyWhenServerHasNullHeartbeat() | ||
| { | ||
| var (storage, monitoring) = CreateStorage(); | ||
| monitoring.GetStatistics().Returns(SampleStats()); | ||
| monitoring.Servers().Returns(new List<Hangfire.Storage.Monitoring.ServerDto> | ||
| { | ||
| new() { Name = "srv-never", Heartbeat = null } | ||
| }); | ||
|
|
||
| var sut = new HangfireHealthCheck(storage); | ||
| var result = await sut.CheckHealthAsync(CreateContext(sut)); | ||
|
|
||
| Assert.Equal(HealthStatus.Unhealthy, result.Status); | ||
| Assert.Contains("never", result.Description); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CheckHealthAsync_HealthyWhenAtLeastOneHeartbeatIsRecent() | ||
| { | ||
| var (storage, monitoring) = CreateStorage(); | ||
| monitoring.GetStatistics().Returns(SampleStats(2)); | ||
| monitoring.Servers().Returns(new List<Hangfire.Storage.Monitoring.ServerDto> | ||
| { | ||
| new() { Name = "srv-stale", Heartbeat = DateTime.UtcNow.AddMinutes(-10) }, | ||
| new() { Name = "srv-fresh", Heartbeat = DateTime.UtcNow } | ||
| }); | ||
|
|
||
| var sut = new HangfireHealthCheck(storage); | ||
| var result = await sut.CheckHealthAsync(CreateContext(sut)); | ||
|
|
||
| Assert.Equal(HealthStatus.Healthy, result.Status); | ||
| Assert.Contains("Hangfire OK", result.Description); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CheckHealthAsync_UnhealthyWhenStorageThrows() | ||
| { | ||
| var storage = Substitute.For<Hangfire.JobStorage>(); | ||
| var boom = new InvalidOperationException("boom"); | ||
| storage.GetMonitoringApi().Returns(_ => throw boom); | ||
|
|
||
| var sut = new HangfireHealthCheck(storage); | ||
| var result = await sut.CheckHealthAsync(CreateContext(sut)); | ||
|
|
||
| Assert.Equal(HealthStatus.Unhealthy, result.Status); | ||
| Assert.Contains("unreachable", result.Description); | ||
| Assert.Same(boom, result.Exception); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CheckHealthAsync_DataDictionaryContainsAllStatsKeys() | ||
| { | ||
| var (storage, monitoring) = CreateStorage(); | ||
| monitoring.GetStatistics().Returns(new Hangfire.Storage.Monitoring.StatisticsDto | ||
| { | ||
| Servers = 1, | ||
| Enqueued = 7, | ||
| Scheduled = 8, | ||
| Processing = 9, | ||
| Failed = 10, | ||
| Recurring = 11 | ||
| }); | ||
| monitoring.Servers().Returns(new List<Hangfire.Storage.Monitoring.ServerDto> | ||
| { | ||
| new() { Name = "srv-1", Heartbeat = DateTime.UtcNow } | ||
| }); | ||
|
|
||
| var sut = new HangfireHealthCheck(storage); | ||
| var result = await sut.CheckHealthAsync(CreateContext(sut)); | ||
|
|
||
| Assert.Contains("servers", result.Data.Keys); | ||
| Assert.Contains("enqueued", result.Data.Keys); | ||
| Assert.Contains("scheduled", result.Data.Keys); | ||
| Assert.Contains("processing", result.Data.Keys); | ||
| Assert.Contains("failed", result.Data.Keys); | ||
| Assert.Contains("recurring", result.Data.Keys); | ||
| Assert.Equal(1L, result.Data["servers"]); | ||
| Assert.Equal(7L, result.Data["enqueued"]); | ||
| Assert.Equal(11L, result.Data["recurring"]); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| using System.Reflection; | ||
| using Viper.Areas.RAPS.Jobs; | ||
| using Viper.Areas.Scheduler.Services; | ||
|
|
||
| namespace Viper.test.RAPS | ||
| { | ||
| public sealed class RapsRoleRefreshScheduledJobTests | ||
| { | ||
| [Fact] | ||
| public void Class_IsDecoratedWithScheduledJob() | ||
| { | ||
| var attr = typeof(RapsRoleRefreshScheduledJob).GetCustomAttribute<ScheduledJobAttribute>(); | ||
|
|
||
| Assert.NotNull(attr); | ||
| Assert.Equal("raps:role-refresh", attr.Id); | ||
| Assert.Equal("0 0 * * *", attr.Cron); | ||
| Assert.Equal("Pacific Standard Time", attr.TimeZoneId); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.