-
Notifications
You must be signed in to change notification settings - Fork 0
feat(api): Redis distributed cache + durable background job queue (#217 #218) #214
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9593e23
feat(api): Redis-backed IDistributedCache for user-date preferences (…
thomasluizon ef48f93
feat(api): durable Hangfire job queue for recurring schedulers (#218)
thomasluizon 10b39f1
fix(health): record durable-mode health ticks in each scheduler RunAsync
thomasluizon 1a36078
fix(api): monitor CalendarAutoSync + make date-pref invalidation async
thomasluizon 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
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
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
37 changes: 37 additions & 0 deletions
37
src/Orbit.Infrastructure/BackgroundJobs/HangfireRecurringJobRegistrar.cs
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,37 @@ | ||
| using Hangfire; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Orbit.Infrastructure.BackgroundJobs; | ||
|
|
||
| /// <summary> | ||
| /// Registers every <see cref="IScheduledJob"/> as a Hangfire recurring job on startup when the | ||
| /// durable-queue flag is on. Each job is keyed by its stable name, so re-registering on every boot | ||
| /// reconciles cron changes without creating duplicates, and Hangfire's storage keeps the schedule | ||
| /// across restarts while its distributed lock ensures a single instance runs each occurrence. | ||
| /// </summary> | ||
| public sealed partial class HangfireRecurringJobRegistrar( | ||
| IRecurringJobManager recurringJobManager, | ||
| IEnumerable<IScheduledJob> jobs, | ||
| ILogger<HangfireRecurringJobRegistrar> logger) : IHostedService | ||
| { | ||
| public Task StartAsync(CancellationToken cancellationToken) | ||
| { | ||
| foreach (var job in jobs) | ||
| { | ||
| recurringJobManager.AddOrUpdate<ScheduledJobRunner>( | ||
| job.Name, | ||
| runner => runner.RunAsync(job.Name, CancellationToken.None), | ||
| job.CronExpression); | ||
|
|
||
| LogJobRegistered(logger, job.Name, job.CronExpression); | ||
| } | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
|
||
| [LoggerMessage(EventId = 1, Level = LogLevel.Information, Message = "Registered durable recurring job {JobName} with schedule {CronExpression}")] | ||
| private static partial void LogJobRegistered(ILogger logger, string jobName, string cronExpression); | ||
| } |
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,17 @@ | ||
| namespace Orbit.Infrastructure.BackgroundJobs; | ||
|
|
||
| /// <summary> | ||
| /// A recurring background scan that can run either as an in-process polling loop or as a durable | ||
| /// Hangfire recurring job, selected by the <c>BackgroundServices:UseDurableQueue</c> flag. Each | ||
| /// scheduler exposes its stable <see cref="Name"/> (the Hangfire recurring-job id and lock key) and | ||
| /// the <see cref="CronExpression"/> that mirrors its default in-process interval, while | ||
| /// <see cref="RunAsync"/> performs one occurrence of the same work the polling loop runs each tick. | ||
| /// </summary> | ||
| public interface IScheduledJob | ||
| { | ||
| string Name { get; } | ||
|
|
||
| string CronExpression { get; } | ||
|
|
||
| Task RunAsync(CancellationToken cancellationToken); | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/Orbit.Infrastructure/BackgroundJobs/ScheduledJobRunner.cs
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,18 @@ | ||
| namespace Orbit.Infrastructure.BackgroundJobs; | ||
|
|
||
| /// <summary> | ||
| /// Single Hangfire entry point for every <see cref="IScheduledJob"/>. Hangfire persists only the | ||
| /// job's <see cref="IScheduledJob.Name"/> in storage and resolves a fresh runner per execution, so | ||
| /// adding or renaming a job never changes the serialized recurring-job payload. The runner looks up | ||
| /// the matching job by name and executes one occurrence of its work. | ||
| /// </summary> | ||
| public sealed class ScheduledJobRunner(IEnumerable<IScheduledJob> jobs) | ||
| { | ||
| public Task RunAsync(string jobName, CancellationToken cancellationToken) | ||
| { | ||
| var job = jobs.FirstOrDefault(candidate => candidate.Name == jobName) | ||
| ?? throw new InvalidOperationException($"No scheduled job registered with name '{jobName}'."); | ||
|
|
||
| return job.RunAsync(cancellationToken); | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/Orbit.Infrastructure/Configuration/BackgroundJobSettings.cs
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,16 @@ | ||
| namespace Orbit.Infrastructure.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Background-processing rollout settings. When <see cref="UseDurableQueue"/> is false (default), | ||
| /// each recurring scheduler runs as its own in-process <c>BackgroundService</c> polling loop exactly | ||
| /// as before. When true, those recurring scans are registered as Hangfire recurring jobs backed by | ||
| /// PostgreSQL instead: occurrences survive restarts, a distributed lock prevents more than one | ||
| /// instance running the same occurrence, and failed runs retry with exponential backoff. The | ||
| /// one-shot startup data-encryption migration always runs as a hosted service regardless of this flag. | ||
| /// </summary> | ||
| public sealed class BackgroundJobSettings | ||
| { | ||
| public const string SectionName = "BackgroundServices"; | ||
|
|
||
| public bool UseDurableQueue { get; init; } | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/Orbit.Infrastructure/Configuration/RedisCacheSettings.cs
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,18 @@ | ||
| namespace Orbit.Infrastructure.Configuration; | ||
|
|
||
| /// <summary> | ||
| /// Distributed-cache rollout settings. When <see cref="Enabled"/> is false (default), the app | ||
| /// registers an in-process <c>IDistributedCache</c> and behaves exactly as before. When true, | ||
| /// the same <c>IDistributedCache</c> seam is backed by Redis so cached user-date preferences stay | ||
| /// consistent across multiple API instances. <see cref="ConnectionString"/> is required when enabled. | ||
| /// </summary> | ||
| public sealed class RedisCacheSettings | ||
| { | ||
| public const string SectionName = "Redis"; | ||
|
|
||
| public bool Enabled { get; init; } | ||
|
|
||
| public string ConnectionString { get; init; } = ""; | ||
|
|
||
| public string InstanceName { get; init; } = "orbit:"; | ||
| } |
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tick is recorded here, but
"CalendarAutoSync"is absent fromExpectedIntervalsinBackgroundServiceHealthCheck.CheckHealthAsynconly evaluates keys in that dictionary — ticks for unknown keys are stored inLastSuccessfulTicksbut never compared against a threshold. All 9 other migrated schedulers have an entry; this one doesn't.Fix: add
["CalendarAutoSync"] = TimeSpan.FromMinutes(45)(3× the 15-min cron interval) toExpectedIntervalsinBackgroundServiceHealthCheck.cs.