-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Load Balancing: Implement distributed background jobs #20397
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 20 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
a8a47ca
Start work
Zeegaan 54cba3d
Introduce dto
Zeegaan d0cd5bf
Start making repository
Zeegaan 6278ebd
Add migrations
Zeegaan d23a9c6
Implement fetchable first job
Zeegaan db7e648
Fix up to also finish tasks
Zeegaan c40a964
Refactor jobs to distributed background jobs
Zeegaan 9e0dc52
Filter jobs correctly on LastRun
Zeegaan 40b38c7
Hardcode delay
Zeegaan d97fde1
Add settings to configure delay and period
Zeegaan 6890d90
Fix formatting
Zeegaan b674d7a
Add default data
Zeegaan 876c888
Add update on startup, which will update periods on startup
Zeegaan 5206597
Refactor service to return job directly
Zeegaan a6d86e8
Update src/Umbraco.Infrastructure/Services/Implement/DistributedJobSe…
Zeegaan a91a41c
Update src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroun…
Zeegaan f338f05
Update src/Umbraco.Infrastructure/Migrations/Install/DatabaseDataCrea…
Zeegaan 4a350d6
Update src/Umbraco.Infrastructure/Migrations/Install/DatabaseDataCrea…
Zeegaan 47309d7
Update src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroun…
Zeegaan ef6a3d4
Remove unused
Zeegaan e75c9c6
Move jobs and make internal
Zeegaan 21edd63
make OpenIddictCleanupJob.cs public, as it is used elsewhere
Zeegaan 1e9aba9
Minor docstring changes
nikolajlauridsen 45458ae
Update src/Umbraco.Core/Persistence/Constants-Locks.cs
Zeegaan 7372a87
´Throw correct exceptions
Zeegaan 138f250
Update xml doc
Zeegaan 6064a76
Remove business logic from repository
Zeegaan 874a6f9
Remove more business logic from repository into service
Zeegaan 03ddb53
Remove adding jobs from migration
Zeegaan 3d537dd
fix creation
Zeegaan 641ff91
Rename to ExecuteAsync
Zeegaan 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
25 changes: 25 additions & 0 deletions
25
src/Umbraco.Core/Configuration/Models/DistributedJobSettings.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,25 @@ | ||
| using System.ComponentModel; | ||
|
|
||
| namespace Umbraco.Cms.Core.Configuration.Models; | ||
|
|
||
| /// <summary> | ||
| /// Settings for distributed jobs. | ||
| /// </summary> | ||
| [UmbracoOptions(Constants.Configuration.ConfigDistributedJobs)] | ||
| public class DistributedJobSettings | ||
| { | ||
| internal const string StaticPeriod = "00:00:10"; | ||
| internal const string StaticDelay = "00:01:00"; | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a value for the period of checking if there are any runnable distributed jobs. | ||
| /// </summary> | ||
| [DefaultValue(StaticPeriod)] | ||
| public TimeSpan Period { get; set; } = TimeSpan.Parse(StaticPeriod); | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets a value for the delay of when to start checking for distributed jobs. | ||
| /// </summary> | ||
| [DefaultValue(StaticDelay)] | ||
| public TimeSpan Delay { get; set; } = TimeSpan.Parse(StaticDelay); | ||
| } |
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
103 changes: 103 additions & 0 deletions
103
src/Umbraco.Infrastructure/BackgroundJobs/DistributedBackgroundJobHostedService.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,103 @@ | ||
| using System.Diagnostics; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.Configuration.Models; | ||
| using Umbraco.Cms.Core.Services; | ||
| using Umbraco.Cms.Infrastructure.Services; | ||
|
|
||
| namespace Umbraco.Cms.Infrastructure.BackgroundJobs; | ||
|
|
||
| /// <summary> | ||
| /// A hosted service that checks for any runnable distributed background jobs on a timer. | ||
| /// </summary> | ||
| public class DistributedBackgroundJobHostedService : BackgroundService | ||
| { | ||
| private readonly ILogger<DistributedBackgroundJobHostedService> _logger; | ||
| private readonly IRuntimeState _runtimeState; | ||
| private readonly IDistributedJobService _distributedJobService; | ||
| private DistributedJobSettings _distributedJobSettings; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="DistributedBackgroundJobHostedService"/> class. | ||
| /// </summary> | ||
| /// <param name="logger"></param> | ||
| /// <param name="runtimeState"></param> | ||
| /// <param name="distributedJobService"></param> | ||
| /// <param name="distributedJobSettings"></param> | ||
| public DistributedBackgroundJobHostedService( | ||
| ILogger<DistributedBackgroundJobHostedService> logger, | ||
| IRuntimeState runtimeState, | ||
| IDistributedJobService distributedJobService, | ||
| IOptionsMonitor<DistributedJobSettings> distributedJobSettings) | ||
| { | ||
| _logger = logger; | ||
| _runtimeState = runtimeState; | ||
| _distributedJobService = distributedJobService; | ||
| _distributedJobSettings = distributedJobSettings.CurrentValue; | ||
| distributedJobSettings.OnChange(options => | ||
| { | ||
| _distributedJobSettings = options; | ||
| }); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
| { | ||
| await Task.Delay(_distributedJobSettings.Delay, stoppingToken); | ||
| // Update all jobs, periods might have changed when restarting. | ||
| await _distributedJobService.UpdateAllChangedAsync(); | ||
|
|
||
| using PeriodicTimer timer = new(_distributedJobSettings.Period); | ||
|
|
||
| try | ||
| { | ||
| while (await timer.WaitForNextTickAsync(stoppingToken)) | ||
| { | ||
| await RunRunnableJob(); | ||
Zeegaan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| _logger.LogInformation("Timed Hosted Service is stopping."); | ||
| } | ||
| } | ||
|
|
||
| private async Task RunRunnableJob() | ||
| { | ||
| // Do not run distributed jobs if we aren't in Run level, as we might not have booted properly. | ||
| if (_runtimeState.Level != RuntimeLevel.Run) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| IDistributedBackgroundJob? job = await _distributedJobService.TryTakeRunnableAsync(); | ||
|
|
||
| if (job is null) | ||
| { | ||
| // No runnable jobs for now, return | ||
| return; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| await job.RunJobAsync(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, "An exception occurred while running distributed background job '{JobName}'.", job.Name); | ||
| } | ||
| finally | ||
| { | ||
| try | ||
| { | ||
| await _distributedJobService.FinishAsync(job.Name); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, "An exception occurred while finishing distributed background job '{JobName}'.", job.Name); | ||
| } | ||
| } | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
src/Umbraco.Infrastructure/BackgroundJobs/IDistributedBackgroundJob.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,24 @@ | ||
| namespace Umbraco.Cms.Infrastructure.BackgroundJobs; | ||
|
|
||
| /// <summary> | ||
| /// A background job that will be executed by an available server. With a single server setup this will always be the same. | ||
| /// With a load balanced setup, the executing server might change every time this needs to be executed. | ||
| /// </summary> | ||
| public interface IDistributedBackgroundJob | ||
| { | ||
| /// <summary> | ||
| /// Name of the job | ||
| /// </summary> | ||
| string Name { get; } | ||
|
|
||
| /// <summary> | ||
| /// Timespan representing how often the task should recur. | ||
| /// </summary> | ||
| TimeSpan Period { get; } | ||
|
|
||
| /// <summary> | ||
| /// Run the job. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| Task RunJobAsync(); | ||
Zeegaan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
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
11 changes: 4 additions & 7 deletions
11
src/Umbraco.Infrastructure/BackgroundJobs/Jobs/ContentVersionCleanupJob.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
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.
Uh oh!
There was an error while loading. Please reload this page.