From 1c34d8be437e5d51c56c7177fb0c8a0844239981 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Sat, 27 Jun 2020 11:57:51 +0800 Subject: [PATCH] Add PeriodicBackgroundworker quartz adapter --- .../AbpBackgroundWorkersQuartzModule.cs | 6 ++ .../Quartz/IQuartzBackgroundWorkerAdapter.cs | 7 ++ .../Quartz/QuartzBackgroundWorkerAdapter.cs | 87 +++++++++++++++++++ .../Quartz/QuartzBackgroundWorkerManager.cs | 18 +++- 4 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/IQuartzBackgroundWorkerAdapter.cs create mode 100644 framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerAdapter.cs diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/AbpBackgroundWorkersQuartzModule.cs b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/AbpBackgroundWorkersQuartzModule.cs index 7823502013..9cfc748c4e 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/AbpBackgroundWorkersQuartzModule.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/AbpBackgroundWorkersQuartzModule.cs @@ -1,6 +1,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using Volo.Abp.Modularity; using Volo.Abp.Quartz; @@ -18,6 +19,11 @@ namespace Volo.Abp.BackgroundWorkers.Quartz context.Services.AddConventionalRegistrar(new AbpQuartzConventionalRegistrar()); } + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.AddSingleton(typeof(QuartzPeriodicBackgroundWorkerAdapter<>)); + } + public override void OnPreApplicationInitialization(ApplicationInitializationContext context) { var options = context.ServiceProvider.GetService>().Value; diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/IQuartzBackgroundWorkerAdapter.cs b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/IQuartzBackgroundWorkerAdapter.cs new file mode 100644 index 0000000000..dd6a3019b2 --- /dev/null +++ b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/IQuartzBackgroundWorkerAdapter.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.BackgroundWorkers.Quartz +{ + public interface IQuartzBackgroundWorkerAdapter : IQuartzBackgroundWorker + { + void BuildWorker(IBackgroundWorker worker); + } +} diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerAdapter.cs b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerAdapter.cs new file mode 100644 index 0000000000..02caffa80c --- /dev/null +++ b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerAdapter.cs @@ -0,0 +1,87 @@ +using System; +using System.Reflection; +using System.Threading.Tasks; +using Quartz; +using Volo.Abp.Threading; + +namespace Volo.Abp.BackgroundWorkers.Quartz +{ + public class QuartzPeriodicBackgroundWorkerAdapter : QuartzBackgroundWorkerBase, + IQuartzBackgroundWorkerAdapter + where TWorker : IBackgroundWorker + { + public QuartzPeriodicBackgroundWorkerAdapter() + { + AutoRegister = false; + } + + public void BuildWorker(IBackgroundWorker worker) + { + int? period; + var workerType = worker.GetType(); + + if (worker is AsyncPeriodicBackgroundWorkerBase || worker is PeriodicBackgroundWorkerBase) + { + if (typeof(TWorker) != worker.GetType()) + { + throw new ArgumentException($"{nameof(worker)} type is different from the generic type"); + } + + var timer = (AbpTimer) worker.GetType().GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker); + period = timer?.Period; + } + else + { + return; + } + + if (period == null) + { + return; + } + + JobDetail = JobBuilder + .Create>() + .WithIdentity(workerType.FullName) + .Build(); + Trigger = TriggerBuilder.Create() + .WithIdentity(workerType.FullName) + .WithSimpleSchedule(builder => builder.WithInterval(TimeSpan.FromMilliseconds(period.Value)).RepeatForever()) + .Build(); + } + + public override async Task Execute(IJobExecutionContext context) + { + var worker = (IBackgroundWorker) ServiceProvider.GetService(typeof(TWorker)); + var workerContext = new PeriodicBackgroundWorkerContext(ServiceProvider); + + switch (worker) + { + case AsyncPeriodicBackgroundWorkerBase asyncWorker: + { + var doWorkAsyncMethod = asyncWorker.GetType() + .GetMethod("DoWorkAsync", BindingFlags.Instance | BindingFlags.NonPublic); + + if (doWorkAsyncMethod != null) + { + await (Task) doWorkAsyncMethod.Invoke(asyncWorker, new object[] {workerContext}); + } + + break; + } + case PeriodicBackgroundWorkerBase syncWorker: + { + var doWorkMethod = syncWorker.GetType() + .GetMethod("DoWork", BindingFlags.Instance | BindingFlags.NonPublic); + + if (doWorkMethod != null) + { + doWorkMethod.Invoke(syncWorker, new object[] {workerContext}); + } + + break; + } + } + } + } +} diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerManager.cs b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerManager.cs index 35359b1d27..d670c1db98 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerManager.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerManager.cs @@ -1,4 +1,5 @@ -using System.Threading; +using System; +using System.Threading; using System.Threading.Tasks; using Quartz; using Volo.Abp.DependencyInjection; @@ -50,6 +51,19 @@ namespace Volo.Abp.BackgroundWorkers.Quartz await DefaultScheduleJobAsync(quartzWork); } } + else + { + var adapterType = typeof(QuartzPeriodicBackgroundWorkerAdapter<>).MakeGenericType(worker.GetType()); + + var workerAdapter = Activator.CreateInstance(adapterType) as IQuartzBackgroundWorkerAdapter; + + workerAdapter?.BuildWorker(worker); + + if (workerAdapter?.Trigger != null) + { + await DefaultScheduleJobAsync(workerAdapter); + } + } } protected virtual async Task DefaultScheduleJobAsync(IQuartzBackgroundWorker quartzWork) @@ -66,4 +80,4 @@ namespace Volo.Abp.BackgroundWorkers.Quartz } } } -} \ No newline at end of file +}