Browse Source

Merge pull request #23207 from abpframework/BackgroundWokrder-HostApplicationLifetime

Add `IHostApplicationLifetime` usage for background worker management
pull/23264/head
Engincan VESKE 1 year ago
committed by GitHub
parent
commit
0ecc5b84b6
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 12
      framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AbpBackgroundWorkersModule.cs
  2. 10
      framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs

12
framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AbpBackgroundWorkersModule.cs

@ -1,5 +1,7 @@
using System.Threading.Tasks; using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.Threading; using Volo.Abp.Threading;
@ -16,9 +18,11 @@ public class AbpBackgroundWorkersModule : AbpModule
var options = context.ServiceProvider.GetRequiredService<IOptions<AbpBackgroundWorkerOptions>>().Value; var options = context.ServiceProvider.GetRequiredService<IOptions<AbpBackgroundWorkerOptions>>().Value;
if (options.IsEnabled) if (options.IsEnabled)
{ {
var hostApplicationLifetime = context.ServiceProvider.GetService<IHostApplicationLifetime>();
var cancellationToken = hostApplicationLifetime?.ApplicationStopping ?? CancellationToken.None;
await context.ServiceProvider await context.ServiceProvider
.GetRequiredService<IBackgroundWorkerManager>() .GetRequiredService<IBackgroundWorkerManager>()
.StartAsync(); .StartAsync(cancellationToken);
} }
} }
@ -27,9 +31,11 @@ public class AbpBackgroundWorkersModule : AbpModule
var options = context.ServiceProvider.GetRequiredService<IOptions<AbpBackgroundWorkerOptions>>().Value; var options = context.ServiceProvider.GetRequiredService<IOptions<AbpBackgroundWorkerOptions>>().Value;
if (options.IsEnabled) if (options.IsEnabled)
{ {
var hostApplicationLifetime = context.ServiceProvider.GetService<IHostApplicationLifetime>();
var cancellationToken = hostApplicationLifetime?.ApplicationStopping ?? CancellationToken.None;
await context.ServiceProvider await context.ServiceProvider
.GetRequiredService<IBackgroundWorkerManager>() .GetRequiredService<IBackgroundWorkerManager>()
.StopAsync(); .StopAsync(cancellationToken);
} }
} }

10
framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs

@ -3,6 +3,7 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations; using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Volo.Abp.BackgroundWorkers; namespace Volo.Abp.BackgroundWorkers;
@ -28,6 +29,15 @@ public static class BackgroundWorkersApplicationInitializationContextExtensions
throw new AbpException($"Given type ({workerType.AssemblyQualifiedName}) must implement the {typeof(IBackgroundWorker).AssemblyQualifiedName} interface, but it doesn't!"); throw new AbpException($"Given type ({workerType.AssemblyQualifiedName}) must implement the {typeof(IBackgroundWorker).AssemblyQualifiedName} interface, but it doesn't!");
} }
if (cancellationToken == default)
{
var hostApplicationLifetime = context.ServiceProvider.GetService<IHostApplicationLifetime>();
if (hostApplicationLifetime != null)
{
cancellationToken = hostApplicationLifetime.ApplicationStopping;
}
}
await context.ServiceProvider await context.ServiceProvider
.GetRequiredService<IBackgroundWorkerManager>() .GetRequiredService<IBackgroundWorkerManager>()
.AddAsync((IBackgroundWorker)context.ServiceProvider.GetRequiredService(workerType), cancellationToken); .AddAsync((IBackgroundWorker)context.ServiceProvider.GetRequiredService(workerType), cancellationToken);

Loading…
Cancel
Save