EaCloud Hangfire 后台任务组件,封装基于 Hangfire 后台任务的服务端实现。
$ dotnet add package EaCloud.HangfireEaCloud Hangfire 后台任务组件,封装基于 Hangfire 后台任务的服务端实现。
可按照如下配置方式使用:
EaCloud.Hangfire 程序集Install-Package EaCloud.Hangfire
appsettings.json 中 的 EaCloud 节点下添加如下配置节点{
"Hangfire": {
"Title": "EaCloud任务管理", //显示在仪表板上的标题
"ServerName": "EaCloudHangfire", //服务器名称
"WorkerCount": 20, //并发任务数 --超出并发数。将等待之前任务的完成 (推荐并发线程是cpu 的 5倍)
"StorageType": "Sqlserver", //存储类型:"Sqlserver"、"PostgreSql"、"SQLite"、"MySql"、"Oracle"、"Mongo"、"Redis"、"Memory"
//存储连接字符串:指定的数据库名必须已经存在,可以手动创建空数据库或者使用当前数据库
"StorageConnectionString": "Data Source=127.0.0.1;Initial Catalog=EaCloudDEV.Hangfire;User ID=sa;Password=p@ssw0rd;Pooling=true;Min Pool Size=1;Max Pool Size=60;Integrated Security=false;Connect Timeout=60;Application Name=EaCloud",
"DashboardUrl": "/hangfire", //仪表盘URL路径
"AuthorizationType": "Basic", //授权访问类型:"Anonymous"、"Basic"
//Hangfire基本身份验证的选项,当 AuthorizationType = "Basic" 时生效。
"BasicOptions": {
"SslRedirect": false, //SSL重定向
"RequireSsl": false, //需要SSL连接才能访问Hangfire Dashboard
"LoginCaseSensitive": false, //登录检查是否区分大小写
//访问Hangfire仪表板的用户列表
"Users": [
{
//登录名
"UserId": "用户身份标识",
//登录密码
"Password": "访问密码"
}
]
},
"Enabled": true //是否启用
}
}
Sqlserver、PostgreSql、SQLite、MySql、Oracle、Mongo),则需要按照连接串去新建Hangfire用的空数据库(只要库就可以,表会自动生成)。http(s)://ip:port/(DashboardUrl)即可打开Hangfire仪表盘界面。XXXHangfireJobRunner实现类并添加[Dependency(ServiceLifetime.Singleton)]单例服务特性,继承IHangfireJobRunner接口在Start方法中添加Hangfire任务逻辑#region "Hangfire作业运行器"
/// <summary>
/// Hangfire作业运行器
/// </summary>
[Dependency(ServiceLifetime.Singleton)]
public class HangfireJobRunner : IHangfireJobRunner
{
/// <summary>
/// 启动作业运行器
/// </summary>
public void Start()
{
//队列任务
BackgroundJob.Enqueue(() => Console.WriteLine($@"队列任务"));
//延时任务
var jobId = BackgroundJob.Schedule(() => Console.WriteLine($@"延时任务"), TimeSpan.FromMinutes(1));
//延续性任务执行
BackgroundJob.ContinueJobWith(jobId, () => Console.WriteLine($@"延续性任务执行"));
//定时任务
RecurringJob.AddOrUpdate("JobId1", () => Console.WriteLine($@"定时任务:每年的4月12号15点52分任意秒执行 没加时区 小时+8"), "* 52 15 12 4 *"); //每年的4月12号15点52分任意秒执行 没加时区 小时+8
RecurringJob.AddOrUpdate("JobId2", () => Console.WriteLine($@"定时任务:每分钟执行一次"), Cron.Minutely); //每分钟执行一次。
RecurringJob.AddOrUpdate("JobId3", () => Console.WriteLine($@"定时任务:每天18点36分 当前时区"), Cron.Daily(18, 36), new RecurringJobOptions { TimeZone = TimeZoneInfo.Local }); //每天18点36分 当前时区
//本机示例
RecurringJob.AddOrUpdate<TestHangfireJob>("TestHangfireJob_Test", m => m.Test(), Cron.Minutely, new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
RecurringJob.AddOrUpdate<TestHangfireJob>("TestHangfireJob_TestAsync", m => m.TestAsync(), Cron.Minutely, new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
}
}
#endregion
5.2. 若需要使用注入模式使用,参考上述代码本机示例新建XXXHangfireJob,定义如下结构代码
#region "测试Hangfire作业"
/// <summary>
/// 测试Hangfire作业
/// </summary>
public class XXXHangfireJob
{
#region "字段"
#region "私有只读-身份认证上下文"
/// <summary>
/// 私有只读-身份认证上下文
/// </summary>
private readonly IIdentityContract _identityContract;
#endregion
#region "私有只读-服务提供者"
/// <summary>
/// 私有只读-服务提供者
/// </summary>
private readonly IServiceProvider _provider;
#endregion
#endregion
#region "属性"
#region "获取 日志对象"
/// <summary>
/// 获取 日志对象
/// </summary>
protected ILogger Logger => _provider.GetLogger(GetType());
#endregion
#endregion
#region "构造函数"
#region "初始化一个测试Hangfire作业的新实例"
/// <summary>
/// 初始化一个测试Hangfire作业 <see cref="XXXHangfireJob"/> 的新实例
/// </summary>
/// <param name="identityContract">身份认证上下文</param>
/// <param name="provider">服务提供者</param>
public XXXHangfireJob(IIdentityContract identityContract, IServiceProvider provider)
{
_identityContract = identityContract;
_provider = provider;
}
#endregion
#endregion
#region "方法"
#region "同步测试方法"
/// <summary>
/// 同步测试方法
/// </summary>
/// <returns></returns>
public void Test()
{
//TODO:Your Coding...
Logger.LogDebug(I18N.T("HangfireTest", "Hangfire Test:{0}", false, DateTime.Now.ToString()));
}
#endregion
#region "异步测试方法"
/// <summary>
/// 异步测试方法
/// </summary>
/// <returns></returns>
public async Task TestAsync()
{
//TODO:Your Coding...
Logger.LogDebug(I18N.T("HangfireTestAsync", "Hangfire TestAsync:{0}", false, DateTime.Now.ToString()));
await Task.CompletedTask;
}
#endregion
#endregion
}
#endregion
Enabled: false。![]() | ![]() |
|---|---|
| QQ群号:863605868 | 微信号:SeonHu |