Browse Source
Add IHostApplicationLifetime usage for background worker management
pull/23207/head
maliming
7 months ago
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4
2 changed files with
12 additions and
2 deletions
framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AbpBackgroundWorkersModule.cs
framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.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 ) ;
}
}
@ -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 ) ;