Skip to content

Commit

Permalink
Use IOptions<BackgroundServiceOptions> pattern (#10943)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkech committed Jan 3, 2022
1 parent e6f62ad commit 678770e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OrchardCore.Abstractions.BackgroundTasks
namespace OrchardCore.Modules
{
public class BackgroundServiceOptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Configuration;
Expand Down Expand Up @@ -94,6 +95,11 @@ public static OrchardCoreBuilder AddBackgroundService(this OrchardCoreBuilder bu
{
builder.ApplicationServices.AddHostedService<ModularBackgroundService>();

builder.ApplicationServices
.AddOptions<BackgroundServiceOptions>()
.Configure<IConfiguration>((options, config) => config
.Bind("OrchardCore:OrchardCore_BackgroundService", options));

return builder;
}
}
Expand Down
19 changes: 6 additions & 13 deletions src/OrchardCore/OrchardCore/Modules/ModularBackgroundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using OrchardCore.BackgroundTasks;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Builders;
using OrchardCore.Environment.Shell.Configuration;
using OrchardCore.Environment.Shell.Models;
using OrchardCore.Locking.Distributed;
using OrchardCore.Settings;
using OrchardCore.Abstractions.BackgroundTasks;

namespace OrchardCore.Modules
{
Expand All @@ -35,27 +33,22 @@ internal class ModularBackgroundService : BackgroundService

private readonly IShellHost _shellHost;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly BackgroundServiceOptions _options;
private readonly ILogger _logger;
private readonly IClock _clock;
private readonly BackgroundServiceOptions _options;

public ModularBackgroundService(
IShellHost shellHost,
IHttpContextAccessor httpContextAccessor,
IOptions<BackgroundServiceOptions> options,
ILogger<ModularBackgroundService> logger,
IClock clock,
Microsoft.Extensions.Configuration.IConfiguration configuration)
IClock clock)
{
_shellHost = shellHost;
_httpContextAccessor = httpContextAccessor;
_options = options.Value;
_logger = logger;
_clock = clock;

_options = configuration
.GetSection("OrchardCore")
.GetSectionCompat("OrchardCore_BackgroundService")
.Get<BackgroundServiceOptions>()
?? new BackgroundServiceOptions();
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
Expand Down Expand Up @@ -121,7 +114,7 @@ private async Task RunAsync(IEnumerable<ShellContext> runningShells, Cancellatio
{
break;
}
var shellScope = await _shellHost.GetScopeAsync(shell.Settings);
if (!_options.ShellWarmup && shellScope.ShellContext.Pipeline == null)
Expand Down

0 comments on commit 678770e

Please sign in to comment.