Browse Source

Add IHostApplicationLifetime usage for background worker management

pull/23207/head
maliming 7 months ago
parent
commit
f0abaf648b
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 7
      framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AbpBackgroundWorkersModule.cs
  2. 7
      framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs

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

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

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

@ -3,6 +3,7 @@ using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Volo.Abp.BackgroundWorkers;
@ -28,6 +29,12 @@ public static class BackgroundWorkersApplicationInitializationContextExtensions
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.GetRequiredService<IHostApplicationLifetime>();
cancellationToken = hostApplicationLifetime.ApplicationStopping;
}
await context.ServiceProvider
.GetRequiredService<IBackgroundWorkerManager>()
.AddAsync((IBackgroundWorker)context.ServiceProvider.GetRequiredService(workerType), cancellationToken);

Loading…
Cancel
Save