From b1bc0c353daff4a4ea0428be471b15df515d6beb Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 31 Mar 2020 14:43:17 +0800 Subject: [PATCH] Execute the job by determining IsJobExecutionEnabled --- .../BackgroundJobs/NullBackgroundJobManager.cs | 3 +-- .../Hangfire/HangfireJobExecutionAdapter.cs | 17 ++++++++++++++--- .../Quartz/AbpBackgroundJobsQuartzModule.cs | 14 +++----------- .../Quartz/QuartzJobExecutionAdapter.cs | 12 +++++++++++- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/NullBackgroundJobManager.cs b/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/NullBackgroundJobManager.cs index 6c8c37d183..f7d9fdd88a 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/NullBackgroundJobManager.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.Abstractions/Volo/Abp/BackgroundJobs/NullBackgroundJobManager.cs @@ -19,8 +19,7 @@ namespace Volo.Abp.BackgroundJobs public virtual Task EnqueueAsync(TArgs args, BackgroundJobPriority priority = BackgroundJobPriority.Normal, TimeSpan? delay = null) { - //throw new AbpException("Background job system has not a real implementation. If it's mandatory, use an implementation (either the default provider or a 3rd party implementation). If it's optional, check IBackgroundJobManager.IsAvailable() extension method and act based on it."); - return Task.FromResult(string.Empty); + throw new AbpException("Background job system has not a real implementation. If it's mandatory, use an implementation (either the default provider or a 3rd party implementation). If it's optional, check IBackgroundJobManager.IsAvailable() extension method and act based on it."); } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs b/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs index 676a3eaafc..41398b5bb6 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.HangFire/Volo/Abp/BackgroundJobs/Hangfire/HangfireJobExecutionAdapter.cs @@ -1,4 +1,6 @@ using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Volo.Abp.Threading; @@ -6,22 +8,31 @@ namespace Volo.Abp.BackgroundJobs.Hangfire { public class HangfireJobExecutionAdapter { + public ILogger> Logger { get; set; } + protected AbpBackgroundJobOptions Options { get; } protected IServiceScopeFactory ServiceScopeFactory { get; } protected IBackgroundJobExecuter JobExecuter { get; } public HangfireJobExecutionAdapter( - IOptions options, - IBackgroundJobExecuter jobExecuter, + IOptions options, + IBackgroundJobExecuter jobExecuter, IServiceScopeFactory serviceScopeFactory) { JobExecuter = jobExecuter; ServiceScopeFactory = serviceScopeFactory; Options = options.Value; + Logger = NullLogger>.Instance; } public void Execute(TArgs args) { + if (!Options.IsJobExecutionEnabled) + { + Logger.LogWarning("Background jobs system is disabled"); + return; + } + using (var scope = ServiceScopeFactory.CreateScope()) { var jobType = Options.GetJob(typeof(TArgs)).JobType; @@ -30,4 +41,4 @@ namespace Volo.Abp.BackgroundJobs.Hangfire } } } -} +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.BackgroundJobs.Quartz/Volo/Abp/BackgroundJobs/Quartz/AbpBackgroundJobsQuartzModule.cs b/framework/src/Volo.Abp.BackgroundJobs.Quartz/Volo/Abp/BackgroundJobs/Quartz/AbpBackgroundJobsQuartzModule.cs index 9f3d1b11a0..0e3d079acc 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.Quartz/Volo/Abp/BackgroundJobs/Quartz/AbpBackgroundJobsQuartzModule.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.Quartz/Volo/Abp/BackgroundJobs/Quartz/AbpBackgroundJobsQuartzModule.cs @@ -8,19 +8,11 @@ namespace Volo.Abp.BackgroundJobs.Quartz typeof(AbpBackgroundJobsAbstractionsModule), typeof(AbpQuartzModule) )] - public class AbpBackgroundJobsQuartzModule :AbpModule + public class AbpBackgroundJobsQuartzModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { - var options = context.Services.ExecutePreConfiguredActions(); - if (!options.IsJobExecutionEnabled) - { - context.Services.AddSingleton(x => new NullBackgroundJobManager()); - } - else - { - context.Services.AddTransient(typeof(QuartzJobExecutionAdapter<>)); - } + context.Services.AddTransient(typeof(QuartzJobExecutionAdapter<>)); } } -} +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.BackgroundJobs.Quartz/Volo/Abp/BackgroundJobs/Quartz/QuartzJobExecutionAdapter.cs b/framework/src/Volo.Abp.BackgroundJobs.Quartz/Volo/Abp/BackgroundJobs/Quartz/QuartzJobExecutionAdapter.cs index 85a1ef736f..2efe800c9c 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.Quartz/Volo/Abp/BackgroundJobs/Quartz/QuartzJobExecutionAdapter.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.Quartz/Volo/Abp/BackgroundJobs/Quartz/QuartzJobExecutionAdapter.cs @@ -1,6 +1,8 @@ using System; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Quartz; @@ -8,10 +10,11 @@ namespace Volo.Abp.BackgroundJobs.Quartz { public class QuartzJobExecutionAdapter : IJob { + public ILogger> Logger { get; set; } + protected AbpBackgroundJobOptions Options { get; } protected IServiceScopeFactory ServiceScopeFactory { get; } protected IBackgroundJobExecuter JobExecuter { get; } - public QuartzJobExecutionAdapter( IOptions options, IBackgroundJobExecuter jobExecuter, @@ -20,10 +23,17 @@ namespace Volo.Abp.BackgroundJobs.Quartz JobExecuter = jobExecuter; ServiceScopeFactory = serviceScopeFactory; Options = options.Value; + Logger = NullLogger>.Instance; } public async Task Execute(IJobExecutionContext context) { + if (!Options.IsJobExecutionEnabled) + { + Logger.LogWarning("Background jobs system is disabled"); + return; + } + using (var scope = ServiceScopeFactory.CreateScope()) { var args = (TArgs)context.JobDetail.JobDataMap.Get(nameof(TArgs));