From dd6eb11c963d7b38cc97218094bb226426626043 Mon Sep 17 00:00:00 2001 From: jianweie code Date: Thu, 4 Jan 2024 22:28:19 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91hangfire?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E5=A2=9E=E5=8A=A0=E3=80=90?= =?UTF-8?q?=E5=B7=B2=E7=BB=8F=E5=AE=8C=E6=88=90=E7=9A=84Job=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E8=BF=9B=E8=A1=8C=E5=AE=9A=E6=97=B6=E8=BF=87=E6=9C=9F?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=A4=84=E7=90=86=E3=80=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AppSettingsConstVars.cs | 6 ++- .../CoreCms.Net.Web.WebApi.xml | 35 +++++++++++++ .../SucceededStateExpireHandler.cs | 51 +++++++++++++++++++ CoreCms.Net.Web.WebApi/Program.cs | 4 ++ CoreCms.Net.Web.WebApi/appsettings.json | 5 +- 5 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 CoreCms.Net.Web.WebApi/Infrastructure/SucceededStateExpireHandler.cs diff --git a/CoreCms.Net.Configuration/AppSettingsConstVars.cs b/CoreCms.Net.Configuration/AppSettingsConstVars.cs index a9a34ebb..3320ba23 100644 --- a/CoreCms.Net.Configuration/AppSettingsConstVars.cs +++ b/CoreCms.Net.Configuration/AppSettingsConstVars.cs @@ -119,6 +119,10 @@ namespace CoreCms.Net.Configuration /// 登录密码 /// public static readonly string HangFirePassWord = AppSettingsHelper.GetContent("HangFire", "PassWord"); + /// + /// 已经完成的任务过期时间,单位分钟(默认10080,7天时间) + /// + public static readonly int HangFireJobExpirationTimeOut = AppSettingsHelper.GetContent("HangFire", "JobExpirationTimeOut").ObjToInt(10080); #endregion @@ -146,7 +150,7 @@ namespace CoreCms.Net.Configuration #endregion - #region Swagger授权访问设置================================================================================ + #region Swagger授权访问设置 /// /// Swagger文档默认访问路由地址 /// diff --git a/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.xml b/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.xml index 8eae1e85..cce972d5 100644 --- a/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.xml +++ b/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.xml @@ -1571,6 +1571,41 @@ 请求验证错误处理 + + + 已完成的job设置过期,防止数据无限增长 + + + + + 数据过期时间 + + + + + 完成的项目过期状态处理 + + + + + + 状态名称 + + + + + 应用状态 + + + + + + + 不应用状态 + + + + 微信公众号消息推送对接 diff --git a/CoreCms.Net.Web.WebApi/Infrastructure/SucceededStateExpireHandler.cs b/CoreCms.Net.Web.WebApi/Infrastructure/SucceededStateExpireHandler.cs new file mode 100644 index 00000000..3360e5df --- /dev/null +++ b/CoreCms.Net.Web.WebApi/Infrastructure/SucceededStateExpireHandler.cs @@ -0,0 +1,51 @@ +using Hangfire.States; +using Hangfire.Storage; +using System; + +namespace CoreCms.Net.Web.WebApi.Infrastructure +{ + /// + /// 已完成的job设置过期,防止数据无限增长 + /// + public class SucceededStateExpireHandler : IStateHandler + { + /// + /// 数据过期时间 + /// + public TimeSpan JobExpirationTimeout; + + /// + /// 完成的项目过期状态处理 + /// + /// + public SucceededStateExpireHandler(int jobExpirationTimeout) + { + JobExpirationTimeout = TimeSpan.FromMinutes(jobExpirationTimeout); + } + + /// + /// 状态名称 + /// + public string StateName => SucceededState.StateName; + + /// + /// 应用状态 + /// + /// + /// + public void Apply(ApplyStateContext context, IWriteOnlyTransaction transaction) + { + context.JobExpirationTimeout = JobExpirationTimeout; + } + + /// + /// 不应用状态 + /// + /// + /// + public void Unapply(ApplyStateContext context, IWriteOnlyTransaction transaction) + { + } + } + +} diff --git a/CoreCms.Net.Web.WebApi/Program.cs b/CoreCms.Net.Web.WebApi/Program.cs index 95d0192d..217d89e6 100644 --- a/CoreCms.Net.Web.WebApi/Program.cs +++ b/CoreCms.Net.Web.WebApi/Program.cs @@ -244,6 +244,10 @@ var hangfireOptions = new Hangfire.DashboardOptions app.UseHangfireDashboard("/job", hangfireOptions); //可以改变Dashboard的url HangfireDispose.HangfireService(); +//设置hangfire定时任务过期时间 +GlobalStateHandlers.Handlers.Add(new SucceededStateExpireHandler(AppSettingsConstVars.HangFireJobExpirationTimeOut)); + + #endregion diff --git a/CoreCms.Net.Web.WebApi/appsettings.json b/CoreCms.Net.Web.WebApi/appsettings.json index 35a7bb4b..b7be1943 100644 --- a/CoreCms.Net.Web.WebApi/appsettings.json +++ b/CoreCms.Net.Web.WebApi/appsettings.json @@ -10,8 +10,9 @@ }, //定时任务管理面板的账户密码 "HangFire": { - "Login": "CoreShopProfessional", - "PassWord": "uzmp0oq9wfbdeasygj647vr53" + "Login": "CoreShopProfessional", //登录账号 + "PassWord": "uzmp0o3213217vr53", //登录密码 + "JobExpirationTimeOut": "10080" //已经完成的任务过期时间,单位分钟(默认10080,7天时间) }, //Swagger授权访问设置 "SwaggerConfig": {