feat: 支持hangfire内存模式
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
using Hangfire;
|
||||
using System.Linq.Expressions;
|
||||
using Hangfire;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Volo.Abp.BackgroundWorkers;
|
||||
using Volo.Abp.BackgroundWorkers.Hangfire;
|
||||
using Volo.Abp.DynamicProxy;
|
||||
|
||||
namespace Yi.Framework.BackgroundWorkers.Hangfire;
|
||||
|
||||
@@ -19,11 +22,28 @@ public class YiFrameworkBackgroundWorkersHangfireModule : AbpModule
|
||||
var backgroundWorkerManager = context.ServiceProvider.GetRequiredService<IBackgroundWorkerManager>();
|
||||
var works = context.ServiceProvider.GetServices<IHangfireBackgroundWorker>();
|
||||
|
||||
var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
|
||||
//【特殊,为了兼容内存模式,由于内存模式任务,不能使用队列】
|
||||
bool.TryParse(configuration["Redis:IsEnabled"], out var redisEnabled);
|
||||
foreach (var work in works)
|
||||
{
|
||||
//如果为空,默认使用服务器本地utc时间
|
||||
work.TimeZone ??= TimeZoneInfo.Local;
|
||||
await backgroundWorkerManager.AddAsync(work);
|
||||
if (redisEnabled)
|
||||
{
|
||||
await backgroundWorkerManager.AddAsync(work);
|
||||
}
|
||||
else
|
||||
{
|
||||
object unProxyWorker = ProxyHelper.UnProxy((object)work);
|
||||
RecurringJob.AddOrUpdate(work.RecurringJobId,
|
||||
(Expression<Func<Task>>)(() =>
|
||||
((IHangfireBackgroundWorker)unProxyWorker).DoWorkAsync(default(CancellationToken))),
|
||||
work.CronExpression, new RecurringJobOptions()
|
||||
{
|
||||
TimeZone = work.TimeZone
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -188,10 +188,10 @@ namespace Yi.Abp.Web
|
||||
|
||||
//配置Hangfire定时任务存储,开启redis后,优先使用redis
|
||||
var redisConfiguration = configuration["Redis:Configuration"];
|
||||
var redisEnabled = configuration["Redis:IsEnabled"];
|
||||
context.Services.AddHangfire(config=>
|
||||
{
|
||||
if (redisEnabled.IsNullOrEmpty() || bool.Parse(redisEnabled))
|
||||
bool.TryParse( configuration["Redis:IsEnabled"], out var redisEnabled);
|
||||
if (redisEnabled)
|
||||
{
|
||||
config.UseRedisStorage(
|
||||
ConnectionMultiplexer.Connect(redisConfiguration),
|
||||
|
||||
Reference in New Issue
Block a user