From f6d5dd916c71fcdaf6c6d7973f2671f7fd5b4180 Mon Sep 17 00:00:00 2001 From: JianWeie Date: Tue, 25 Jan 2022 01:44:03 +0800 Subject: [PATCH] =?UTF-8?q?#=202022-01-24=20###=201.3.4=20=E5=BC=80?= =?UTF-8?q?=E6=BA=90=E7=A4=BE=E5=8C=BA=E7=89=88=EF=BC=88=E4=BC=9A=E5=91=98?= =?UTF-8?q?=E4=B8=93=E4=B8=9A=E7=89=88=E5=90=8C=E6=AD=A5=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=20=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dredis=E9=95=BF=E6=9C=9Fping=E5=92=8Cinfo?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E8=BF=9E=E6=8E=A5=E6=95=B0=E6=97=A0=E9=99=90?= =?UTF-8?q?=E5=A2=9E=E9=95=BF=EF=BC=8C=E8=BF=9B=E8=80=8C=E5=86=85=E5=AD=98?= =?UTF-8?q?=E8=A2=AB=E8=80=97=E5=B0=BD=E7=9A=84bug=E3=80=82=20=E3=80=90?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E3=80=91=E4=BF=AE=E5=A4=8D=E6=9C=AA=E5=A1=AB?= =?UTF-8?q?=E5=86=99redis=E9=93=BE=E6=8E=A5=E6=88=96=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E6=8A=A5=E9=94=99=E6=8F=90=E7=A4=BA=E7=AB=AF?= =?UTF-8?q?=E5=8F=A3=E9=94=99=E8=AF=AF=E3=80=82=20=E3=80=90=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E3=80=91=E4=BF=AE=E6=94=B9hangfire=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=85=A8=E9=83=A8=E8=A7=84=E5=88=99=E4=B8=BA?= =?UTF-8?q?cron=E8=A1=A8=E8=BE=BE=E5=BC=8F=E3=80=82=20###=200.1.3=20?= =?UTF-8?q?=E4=BC=9A=E5=91=98=E4=B8=93=E4=B8=9A=E7=89=88=EF=BC=9A=20?= =?UTF-8?q?=E5=90=8C=E4=B8=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CoreCms.Net.Core/Config/HangFireSetup.cs | 32 +++++++++++++++++++++--- CoreCms.Net.Core/Config/SqlSugarSetup.cs | 10 ++++---- CoreCms.Net.Task/HangfireDispose.cs | 11 ++++---- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/CoreCms.Net.Core/Config/HangFireSetup.cs b/CoreCms.Net.Core/Config/HangFireSetup.cs index 57af0e7..1f81ef4 100644 --- a/CoreCms.Net.Core/Config/HangFireSetup.cs +++ b/CoreCms.Net.Core/Config/HangFireSetup.cs @@ -16,8 +16,11 @@ using CoreCms.Net.Configuration; using CoreCms.Net.Utility.Extensions; using Hangfire; using Hangfire.MySql; +using Hangfire.Redis; +using Hangfire.SqlServer; using Microsoft.Extensions.DependencyInjection; using SqlSugar; +using StackExchange.Redis; namespace CoreCms.Net.Core.Config { @@ -26,6 +29,9 @@ namespace CoreCms.Net.Core.Config /// public static class HangFireSetup { + private static ConnectionMultiplexer _redis; + + public static void AddHangFireSetup(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); @@ -34,7 +40,17 @@ namespace CoreCms.Net.Core.Config var isEnabledRedis = AppSettingsConstVars.RedisUseTimedTask; if (isEnabledRedis) { - services.AddHangfire(x => x.UseRedisStorage(AppSettingsConstVars.RedisConfigConnectionString)); + var configuration = ConfigurationOptions.Parse(AppSettingsConstVars.RedisConfigConnectionString, true); + _redis = ConnectionMultiplexer.Connect(configuration); + var db = _redis.GetDatabase(); + + services.AddHangfire(x => x.UseRedisStorage(_redis, new RedisStorageOptions() + { + Db = db.Database, //建议根据 + SucceededListSize = 500,//后续列表中的最大可见后台作业,以防止它无限增长。 + DeletedListSize = 500,//删除列表中的最大可见后台作业,以防止其无限增长。 + InvisibilityTimeout = TimeSpan.FromMinutes(30), + })); } else { @@ -48,14 +64,22 @@ namespace CoreCms.Net.Core.Config JobExpirationCheckInterval = TimeSpan.FromHours(1), //- 作业到期检查间隔(管理过期记录)。默认值为1小时。 CountersAggregateInterval = TimeSpan.FromMinutes(5), //- 聚合计数器的间隔。默认为5分钟。 PrepareSchemaIfNecessary = true, //- 如果设置为true,则创建数据库表。默认是true。 - DashboardJobListLimit = 50000, //- 仪表板作业列表限制。默认值为50000。 + DashboardJobListLimit = 500, //- 仪表板作业列表限制。默认值为50000。 TransactionTimeout = TimeSpan.FromMinutes(1), //- 交易超时。默认为1分钟。 TablesPrefix = "Hangfire" //- 数据库中表的前缀。默认为none }))); } else if (dbTypeString == DbType.SqlServer.ToString()) { - services.AddHangfire(x => x.UseSqlServerStorage(AppSettingsConstVars.DbSqlConnection)); + services.AddHangfire(x => x.UseSqlServerStorage(AppSettingsConstVars.DbSqlConnection, new SqlServerStorageOptions() + { + QueuePollInterval = TimeSpan.FromSeconds(15), //- 作业队列轮询间隔。默认值为15秒。 + JobExpirationCheckInterval = TimeSpan.FromHours(1), //- 作业到期检查间隔(管理过期记录)。默认值为1小时。 + CountersAggregateInterval = TimeSpan.FromMinutes(5), //- 聚合计数器的间隔。默认为5分钟。 + PrepareSchemaIfNecessary = true, //- 如果设置为true,则创建数据库表。默认是true。 + DashboardJobListLimit = 500, //- 仪表板作业列表限制。默认值为50000。 + TransactionTimeout = TimeSpan.FromMinutes(1), //- 交易超时。默认为1分钟。 + })); } } @@ -64,7 +88,7 @@ namespace CoreCms.Net.Core.Config options.Queues = new[] { GlobalEnumVars.HangFireQueuesConfig.@default.ToString(), GlobalEnumVars.HangFireQueuesConfig.apis.ToString(), GlobalEnumVars.HangFireQueuesConfig.web.ToString(), GlobalEnumVars.HangFireQueuesConfig.recurring.ToString() }; options.ServerTimeout = TimeSpan.FromMinutes(4); options.SchedulePollingInterval = TimeSpan.FromSeconds(15);//秒级任务需要配置短点,一般任务可以配置默认时间,默认15秒 - options.ShutdownTimeout = TimeSpan.FromMinutes(30); //超时时间 + options.ShutdownTimeout = TimeSpan.FromMinutes(5); //超时时间 options.WorkerCount = Math.Max(Environment.ProcessorCount, 20); //工作线程数,当前允许的最大线程,默认20 }); diff --git a/CoreCms.Net.Core/Config/SqlSugarSetup.cs b/CoreCms.Net.Core/Config/SqlSugarSetup.cs index 52a2ff5..6128ba4 100644 --- a/CoreCms.Net.Core/Config/SqlSugarSetup.cs +++ b/CoreCms.Net.Core/Config/SqlSugarSetup.cs @@ -46,11 +46,11 @@ namespace CoreCms.Net.Core.Config services.ConfigurationSugar(db => { db.CurrentConnectionConfig.InitKeyType = InitKeyType.Attribute; - db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices() - { - //判断是否开启redis设置二级缓存方式 - DataInfoCacheService = AppSettingsConstVars.RedisUseCache ? (ICacheService)new SqlSugarRedisCache() : new SqlSugarMemoryCache() - }; + //db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices() + //{ + // //判断是否开启redis设置二级缓存方式 + // DataInfoCacheService = AppSettingsConstVars.RedisUseCache ? (ICacheService)new SqlSugarRedisCache() : new SqlSugarMemoryCache() + //}; //执行SQL 错误事件,可监控sql(暂时屏蔽,需要可开启) //db.Aop.OnLogExecuting = (sql, p) => diff --git a/CoreCms.Net.Task/HangfireDispose.cs b/CoreCms.Net.Task/HangfireDispose.cs index 69ad424..c354708 100644 --- a/CoreCms.Net.Task/HangfireDispose.cs +++ b/CoreCms.Net.Task/HangfireDispose.cs @@ -45,26 +45,25 @@ namespace CoreCms.Net.Task RecurringJob.AddOrUpdate(s => s.Execute(), "0 0/5 * * * ? ", TimeZoneInfo.Utc); // 每5分钟取消一次订单 //自动完成订单任务 - RecurringJob.AddOrUpdate(s => s.Execute(), Cron.Hourly, TimeZoneInfo.Utc); // 每小时自动完成订单 + RecurringJob.AddOrUpdate(s => s.Execute(), "0 0 0/1 * * ? ", TimeZoneInfo.Utc); // 每小时自动完成订单 //自动评价订单任务 - RecurringJob.AddOrUpdate(s => s.Execute(), Cron.Hourly, TimeZoneInfo.Utc); // 每小时自动完成订单 + RecurringJob.AddOrUpdate(s => s.Execute(), "0 0 0/1 * * ? ", TimeZoneInfo.Utc); // 每小时自动完成订单 //自动签收订单任务 - RecurringJob.AddOrUpdate(s => s.Execute(), Cron.Hourly, TimeZoneInfo.Utc); // 每小时自动完成订单 + RecurringJob.AddOrUpdate(s => s.Execute(), "0 0 0/1 * * ? ", TimeZoneInfo.Utc); // 每小时自动完成订单 //催付款订单 RecurringJob.AddOrUpdate(s => s.Execute(), "0 0/5 * * * ? ", TimeZoneInfo.Utc); // 每5分钟催付款订单 //拼团自动取消到期团(每分钟执行一次) - RecurringJob.AddOrUpdate(s => s.Execute(), Cron.Minutely, TimeZoneInfo.Utc); // 每分钟取消一次订单 + RecurringJob.AddOrUpdate(s => s.Execute(), "0 0/2 * * * ? ", TimeZoneInfo.Utc); // 每分钟取消一次订单 //每天凌晨5点定期清理7天前操作日志 RecurringJob.AddOrUpdate(s => s.Execute(), "0 0 5 * * ? ", TimeZoneInfo.Utc); // 每天5点固定时间清理一次 - //定时刷新获取微信AccessToken - RecurringJob.AddOrUpdate(s => s.Execute(), "0 0/2 * * * ? ", TimeZoneInfo.Utc); // 每2分钟刷新获取微信AccessToken + RecurringJob.AddOrUpdate(s => s.Execute(), "0 0/4 * * * ? ", TimeZoneInfo.Utc); // 每2分钟刷新获取微信AccessToken }