|
|
|
@ -12,78 +12,69 @@ using Volo.Abp.Threading; |
|
|
|
namespace Volo.Abp.BackgroundWorkers.Hangfire; |
|
|
|
|
|
|
|
[Dependency(ReplaceServices = true)] |
|
|
|
public class HangfireBackgroundWorkerManager : IBackgroundWorkerManager, ISingletonDependency |
|
|
|
public class HangfireBackgroundWorkerManager : BackgroundWorkerManager, ISingletonDependency |
|
|
|
{ |
|
|
|
private AbpHangfireBackgroundJobServer _backgroundJobServer; |
|
|
|
private readonly IServiceProvider _serviceProvider; |
|
|
|
protected AbpHangfireBackgroundJobServer BackgroundJobServer { get; set; } |
|
|
|
protected IServiceProvider ServiceProvider { get; } |
|
|
|
|
|
|
|
public HangfireBackgroundWorkerManager(IServiceProvider serviceProvider) |
|
|
|
{ |
|
|
|
_serviceProvider = serviceProvider; |
|
|
|
ServiceProvider = serviceProvider; |
|
|
|
} |
|
|
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken = default) |
|
|
|
public async override Task StartAsync(CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
_backgroundJobServer = _serviceProvider.GetRequiredService<AbpHangfireBackgroundJobServer>(); |
|
|
|
return Task.CompletedTask; |
|
|
|
BackgroundJobServer = ServiceProvider.GetRequiredService<AbpHangfireBackgroundJobServer>(); |
|
|
|
await base.StartAsync(cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken = default) |
|
|
|
public async override Task AddAsync(IBackgroundWorker worker, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
return Task.CompletedTask; |
|
|
|
} |
|
|
|
|
|
|
|
public Task AddAsync(IBackgroundWorker worker, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
if (worker is IHangfireBackgroundWorker hangfireBackgroundWorker) |
|
|
|
switch (worker) |
|
|
|
{ |
|
|
|
var unProxyWorker = ProxyHelper.UnProxy(hangfireBackgroundWorker); |
|
|
|
if (hangfireBackgroundWorker.RecurringJobId.IsNullOrWhiteSpace()) |
|
|
|
case IHangfireBackgroundWorker hangfireBackgroundWorker: |
|
|
|
{ |
|
|
|
RecurringJob.AddOrUpdate(() => ((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(cancellationToken), |
|
|
|
hangfireBackgroundWorker.CronExpression, hangfireBackgroundWorker.TimeZone, hangfireBackgroundWorker.Queue); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
RecurringJob.AddOrUpdate(hangfireBackgroundWorker.RecurringJobId, () => ((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(cancellationToken), |
|
|
|
hangfireBackgroundWorker.CronExpression, hangfireBackgroundWorker.TimeZone, hangfireBackgroundWorker.Queue); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
int? period; |
|
|
|
|
|
|
|
if (worker is AsyncPeriodicBackgroundWorkerBase or PeriodicBackgroundWorkerBase) |
|
|
|
{ |
|
|
|
var timer = worker.GetType() |
|
|
|
.GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker); |
|
|
|
|
|
|
|
if (worker is AsyncPeriodicBackgroundWorkerBase) |
|
|
|
var unProxyWorker = ProxyHelper.UnProxy(hangfireBackgroundWorker); |
|
|
|
if (hangfireBackgroundWorker.RecurringJobId.IsNullOrWhiteSpace()) |
|
|
|
{ |
|
|
|
period = ((AbpAsyncTimer)timer)?.Period; |
|
|
|
RecurringJob.AddOrUpdate( |
|
|
|
() => ((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(cancellationToken), |
|
|
|
hangfireBackgroundWorker.CronExpression, hangfireBackgroundWorker.TimeZone, |
|
|
|
hangfireBackgroundWorker.Queue); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
period = ((AbpTimer)timer)?.Period; |
|
|
|
RecurringJob.AddOrUpdate(hangfireBackgroundWorker.RecurringJobId, |
|
|
|
() => ((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(cancellationToken), |
|
|
|
hangfireBackgroundWorker.CronExpression, hangfireBackgroundWorker.TimeZone, |
|
|
|
hangfireBackgroundWorker.Queue); |
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
else |
|
|
|
case AsyncPeriodicBackgroundWorkerBase or PeriodicBackgroundWorkerBase: |
|
|
|
{ |
|
|
|
return Task.CompletedTask; |
|
|
|
} |
|
|
|
var timer = worker.GetType() |
|
|
|
.GetProperty("Timer", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(worker); |
|
|
|
|
|
|
|
if (period == null) |
|
|
|
{ |
|
|
|
return Task.CompletedTask; |
|
|
|
} |
|
|
|
var period = worker is AsyncPeriodicBackgroundWorkerBase ? ((AbpAsyncTimer)timer)?.Period : ((AbpTimer)timer)?.Period; |
|
|
|
|
|
|
|
var adapterType = typeof(HangfirePeriodicBackgroundWorkerAdapter<>).MakeGenericType(ProxyHelper.GetUnProxiedType(worker)); |
|
|
|
var workerAdapter = Activator.CreateInstance(adapterType) as IHangfireBackgroundWorker; |
|
|
|
if (period == null) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
RecurringJob.AddOrUpdate(() => workerAdapter.DoWorkAsync(cancellationToken), GetCron(period.Value), workerAdapter.TimeZone, workerAdapter.Queue); |
|
|
|
} |
|
|
|
var adapterType = typeof(HangfirePeriodicBackgroundWorkerAdapter<>).MakeGenericType(ProxyHelper.GetUnProxiedType(worker)); |
|
|
|
var workerAdapter = Activator.CreateInstance(adapterType) as IHangfireBackgroundWorker; |
|
|
|
|
|
|
|
return Task.CompletedTask; |
|
|
|
RecurringJob.AddOrUpdate(() => workerAdapter.DoWorkAsync(cancellationToken), GetCron(period.Value), workerAdapter.TimeZone, workerAdapter.Queue); |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
default: |
|
|
|
await base.AddAsync(worker, cancellationToken); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual string GetCron(int period) |
|
|
|
@ -105,9 +96,10 @@ public class HangfireBackgroundWorkerManager : IBackgroundWorkerManager, ISingle |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
throw new AbpException($"Cannot convert period: {period} to cron expression, use HangfireBackgroundWorkerBase to define worker"); |
|
|
|
throw new AbpException( |
|
|
|
$"Cannot convert period: {period} to cron expression, use HangfireBackgroundWorkerBase to define worker"); |
|
|
|
} |
|
|
|
|
|
|
|
return cron; |
|
|
|
} |
|
|
|
} |
|
|
|
} |