mirror of https://github.com/abpframework/abp.git
committed by
GitHub
4 changed files with 117 additions and 2 deletions
@ -0,0 +1,7 @@ |
|||||
|
namespace Volo.Abp.BackgroundWorkers.Quartz |
||||
|
{ |
||||
|
public interface IQuartzBackgroundWorkerAdapter : IQuartzBackgroundWorker |
||||
|
{ |
||||
|
void BuildWorker(IBackgroundWorker worker); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,88 @@ |
|||||
|
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 |
||||
|
{ |
||||
|
private readonly MethodInfo _doWorkAsyncMethod; |
||||
|
private readonly MethodInfo _doWorkMethod; |
||||
|
|
||||
|
public QuartzPeriodicBackgroundWorkerAdapter() |
||||
|
{ |
||||
|
AutoRegister = false; |
||||
|
|
||||
|
_doWorkAsyncMethod = typeof(TWorker).GetMethod("DoWorkAsync", BindingFlags.Instance | BindingFlags.NonPublic); |
||||
|
_doWorkMethod = typeof(TWorker).GetMethod("DoWork", BindingFlags.Instance | BindingFlags.NonPublic); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
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: |
||||
|
{ |
||||
|
if (_doWorkAsyncMethod != null) |
||||
|
{ |
||||
|
await (Task) _doWorkAsyncMethod.Invoke(asyncWorker, new object[] {workerContext}); |
||||
|
} |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
case PeriodicBackgroundWorkerBase syncWorker: |
||||
|
{ |
||||
|
if (_doWorkMethod != null) |
||||
|
{ |
||||
|
_doWorkMethod.Invoke(syncWorker, new object[] {workerContext}); |
||||
|
} |
||||
|
|
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue