Add PeriodicBackgroundworker quartz adapter

pull/4505/head
liangshiwei 5 years ago
parent fff0a6f555
commit 1c34d8be43

@ -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<IOptions<AbpBackgroundWorkerOptions>>().Value;

@ -0,0 +1,7 @@
namespace Volo.Abp.BackgroundWorkers.Quartz
{
public interface IQuartzBackgroundWorkerAdapter : IQuartzBackgroundWorker
{
void BuildWorker(IBackgroundWorker worker);
}
}

@ -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<TWorker> : 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<QuartzPeriodicBackgroundWorkerAdapter<TWorker>>()
.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;
}
}
}
}
}

@ -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
}
}
}
}
}

Loading…
Cancel
Save