From 6cccc3c4017bc98ac17aebed95d6230fa740e366 Mon Sep 17 00:00:00 2001 From: jianweie code Date: Wed, 19 Jun 2024 09:47:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0hangfire=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=A2=9E=E9=95=BF=E9=99=90=E5=88=B6=EF=BC=8C=E7=A7=BB=E9=99=A4?= =?UTF-8?q?hangfire=E9=BB=98=E8=AE=A4=E8=B4=A6=E5=8F=B7=E5=AF=86=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E7=94=B1=E7=94=A8=E6=88=B7=E8=87=AA=E8=A1=8C=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AppSettingsConstVars.cs | 8 +++ .../SucceededStateExpireHandler.cs | 51 +++++++++++++++++++ CoreCms.Net.Web.WebApi/Program.cs | 5 +- CoreCms.Net.Web.WebApi/appsettings.json | 6 ++- CoreCms.Net.Web.WebApi/wwwroot/index.html | 5 +- 5 files changed, 69 insertions(+), 6 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 71eee4b..e571648 100644 --- a/CoreCms.Net.Configuration/AppSettingsConstVars.cs +++ b/CoreCms.Net.Configuration/AppSettingsConstVars.cs @@ -149,6 +149,10 @@ namespace CoreCms.Net.Configuration #region HangFire定时任务================================================================================ /// + /// HangFire默认访问路由地址 + /// + public static readonly string HangFireRoutePrefix = AppSettingsHelper.GetContent("HangFire", "RoutePrefix"); + /// /// 登录账号 /// public static readonly string HangFireLogin = AppSettingsHelper.GetContent("HangFire", "Login"); @@ -156,6 +160,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 diff --git a/CoreCms.Net.Web.WebApi/Infrastructure/SucceededStateExpireHandler.cs b/CoreCms.Net.Web.WebApi/Infrastructure/SucceededStateExpireHandler.cs new file mode 100644 index 0000000..3360e5d --- /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 60c0691..c9e8d8c 100644 --- a/CoreCms.Net.Web.WebApi/Program.cs +++ b/CoreCms.Net.Web.WebApi/Program.cs @@ -198,9 +198,12 @@ var hangfireOptions = new Hangfire.DashboardOptions IsReadOnlyFunc = _ => false }; -app.UseHangfireDashboard("/job", hangfireOptions); //ԸıDashboardurl +app.UseHangfireDashboard(AppSettingsConstVars.HangFireRoutePrefix, hangfireOptions); //ԸıDashboardurl HangfireDispose.HangfireService(); +//hangfireʱʱ +GlobalStateHandlers.Handlers.Add(new SucceededStateExpireHandler(AppSettingsConstVars.HangFireJobExpirationTimeOut)); + #endregion //ʹ Session diff --git a/CoreCms.Net.Web.WebApi/appsettings.json b/CoreCms.Net.Web.WebApi/appsettings.json index 4c5599f..9074434 100644 --- a/CoreCms.Net.Web.WebApi/appsettings.json +++ b/CoreCms.Net.Web.WebApi/appsettings.json @@ -10,8 +10,10 @@ }, //定时任务管理面板的账户密码 "HangFire": { - "Login": "CoreShop", - "PassWord": "oq9wfbdeasygj647v" + "RoutePrefix": "/job", //路由地址,默认'/job' + "Login": "", //登录账号 + "PassWord": "", //登录密码 + "JobExpirationTimeOut": "10080" //已经完成的任务过期时间,单位分钟(默认10080,7天时间) }, //Swagger授权访问设置 "SwaggerConfig": { diff --git a/CoreCms.Net.Web.WebApi/wwwroot/index.html b/CoreCms.Net.Web.WebApi/wwwroot/index.html index b2755e0..df44ca2 100644 --- a/CoreCms.Net.Web.WebApi/wwwroot/index.html +++ b/CoreCms.Net.Web.WebApi/wwwroot/index.html @@ -31,9 +31,8 @@
注意事项:

1、以上组件地址支持自定义调整,投产环境下请及时修改或屏蔽;投产环境下可删除此展示页面。

-

2、『Swagger接口文档』访问路径修改位置为:【Startup.cs】文件第206行左右【代码:c.RoutePrefix = "doc"】,请将doc修改为其他虚拟路径。

-

3、『HangFire定时任务』面板路径修改位置为:【Startup.cs】文件第256行左右【代码:app.UseHangfireDashboard("/job", options);】,请将job修改为其他虚拟路径。

-

4、『HangFire定时任务』初始登录账号密码均为:【CoreShop】,修改位置为:【appsettings.json】文件,代码中【HangFire】节点。

+

2、『Swagger接口文档』访问路径,登录账号密码均自行设置,修改位置为:【appsettings.json】文件,代码中【SwaggerConfig】节点。

+

4、『HangFire定时任务』访问路径,登录账号密码均自行设置,修改位置为:【appsettings.json】文件,代码中【HangFire】节点。

5、如果无法访问接口,请检查【1、数据库配置是否正常】【2、Redis是否配置正常并可正常链接使用】【3、Mysql数据库请保持5.7+版本】【4、站点日志文件夹写入权限】。

6、如果第三方请求接口服务获取不到数据,Debug提示【Access-Control-Allow-Origin】的错误,表示为跨域问题,请打开【appsettings.json】文件在【Cors】节点设置请求白名单。