经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
.NET Core下开源任务调度框架Hangfire的Api任务拓展(支持秒级任务)
来源:cnblogs  作者:遇见听风  时间:2019/4/9 9:38:12  对本文有异议

HangFire的拓展和使用

看了很多博客,小白第一次写博客。

最近由于之前的任务调度框架总出现问题,因此想寻找一个替代品,之前使用的是Quartz.Net,这个框架方便之处就是支持cron表达式适合复杂日期场景使用,以及秒级任务。但是配置比较复杂,而且管理不方便,自己开发了个web管理页面,不过这个需要额外的单独线程去统一管理工作状态,很容易出现问题。

有考虑过 “FluentScheduler” ,使用简单,但是管理配置也很麻烦,我希望能做到配置简单,管理方便,高性能。最后想到了以前听过的hangfire,它的好处就是自带控制面板,在园子里看了很多相关资料,偶然发现了有人拓展过hangfire通过调用api接口来执行任务,这种方式可以避免依赖本地代码,方便部署,在此基础上,我用空闲时间拓展了一下现在已经基本可以满足需求。

 

所拓展的功能全部属于外部拓展,因此hangfire版本可以一直更新,现在已经更新最新版,支持秒级任务

gitHub地址

 

由于更新到最新版hangfire 1.7支持秒级任务,使用的在线表达式生成部分表达式有问题,注掉了秒级任务表达式生成,有时间需要详细测试更改,可以参考(hangfire官方提供的表达式)

现在已经实现的功能有:

1,部署及调试:只需要配置数据库连接,然后编译即可运行,无需建表,支持(redis,mysql, sqlserver)其他数据库暂时用不到没测试。推荐使用redis集群。项目中直接添加了redis的存储包,已经更新StackExchange.Redis到最新版本方便拓展,调试时可以直接调试。部署,只需要发布项目,运行创建windows服务的bat命令,命令已经包含在项目中,或者发布至Linux。

 

2,周期任务:支持在控制面板页面上添加周期任务,编辑周期任务,删除周期任务,手动触发周期任务,暂停和继续周期任务(暂停实现的原理是通过set中添加属性,在job执行前,过滤掉,直接跳过执行,因为hangfire中job一旦创建就失去了控制权,只能通过过滤器去拦截),任务暂停后会查询状态并渲染面板列表为红色字体方便查找哪个任务被暂停。

3,计划任务在作业选项卡中,计划作业中可以实现添加计划任务,计划任务可以使任务在指定的分钟后执行,只执行一次。

 

4,只读面板通过配置的用户名密码,使用户只具有读取面板的权限,这样可以防止误操作

 

 

  1. 1 //只读面板,只能读取不能操作
  2. 2 app.UseHangfireDashboard("/job-read", new DashboardOptions
  3. 3 {
  4. 4 AppPath = "#",//返回时跳转的地址
  5. 5 DisplayStorageConnectionString = false,//是否显示数据库连接信息
  6. 6 IsReadOnlyFunc = Context =>
  7. 7 {
  8. 8 return true;
  9. 9 },
  10. 10 Authorization = new[] { new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions
  11. 11 {
  12. 12 RequireSsl = false,//是否启用ssl验证,即https
  13. 13 SslRedirect = false,
  14. 14 LoginCaseSensitive = true,
  15. 15 Users = new []
  16. 16 {
  17. 17 new BasicAuthAuthorizationUser
  18. 18 {
  19. 19 Login = "read",
  20. 20 PasswordClear = "only"
  21. 21 },
  22. 22 new BasicAuthAuthorizationUser
  23. 23 {
  24. 24 Login = "test",
  25. 25 PasswordClear = "123456"
  26. 26 },
  27. 27 new BasicAuthAuthorizationUser
  28. 28 {
  29. 29 Login = "guest",
  30. 30 PasswordClear = "123@123"
  31. 31 }
  32. 32 }
  33. 33 })
  34. 34 }
  35. 35 });
View Code

 

 

 5,邮件推送:目前使用的方式是,任务错误重试达到指定次数后,发送邮件通知,使用的MailKit

  1. 1 catch (Exception ex)
  2. 2 {
  3. 3 //获取重试次数
  4. 4 var count = context.GetJobParameter<string>("RetryCount");
  5. 5 context.SetTextColor(ConsoleTextColor.Red);
  6. 6 //signalR推送
  7. 7 //SendRequest(ConfigSettings.Instance.URL+"/api/Publish/EveryOne", "测试");
  8. 8 if (count == "3")//重试达到三次的时候发邮件通知
  9. 9 {
  10. 10 SendEmail(item.JobName, item.Url, ex.ToString());
  11. 11 }
  12. 12 logger.Error(ex, "HttpJob.Excute");
  13. 13 context.WriteLine($"执行出错:{ex.Message}");
  14. 14 throw;//不抛异常不会执行重试操作
  15. 15 }
View Code
  1. 1 /// <summary>
  2. 2 /// 邮件模板
  3. 3 /// </summary>
  4. 4 /// <param name="jobname"></param>
  5. 5 /// <param name="url"></param>
  6. 6 /// <param name="exception"></param>
  7. 7 /// <returns></returns>
  8. 8 private static string SethtmlBody(string jobname, string url, string exception)
  9. 9 {
  10. 10 var htmlbody = $@"<h3 align='center'>{HangfireHttpJobOptions.SMTPSubject}</h3>
  11. 11 <h3>执行时间:</h3>
  12. 12 <p>
  13. 13 {DateTime.Now}
  14. 14 </p>
  15. 15 <h3>
  16. 16 任务名称:<span> {jobname} </span><br/>
  17. 17 </h3>
  18. 18 <h3>
  19. 19 请求路径:{url}
  20. 20 </h3>
  21. 21 <h3><span></span>
  22. 22 执行结果:<br/>
  23. 23 </h3>
  24. 24 <p>
  25. 25 {exception}
  26. 26 </p> ";
  27. 27 return htmlbody;
  28. 28 }
邮件模板
  1. 1 //使用redis
  2. 2 config.UseRedisStorage(Redis, new Hangfire.Redis.RedisStorageOptions()
  3. 3 {
  4. 4 FetchTimeout=TimeSpan.FromMinutes(5),
  5. 5 Prefix = "{hangfire}:",
  6. 6 //活动服务器超时时间
  7. 7 InvisibilityTimeout = TimeSpan.FromHours(1),
  8. 8 //任务过期检查频率
  9. 9 ExpiryCheckInterval = TimeSpan.FromHours(1),
  10. 10 DeletedListSize = 10000,
  11. 11 SucceededListSize = 10000
  12. 12 })
  13. 13 .UseHangfireHttpJob(new HangfireHttpJobOptions()
  14. 14 {
  15. 15 SendToMailList = HangfireSettings.Instance.SendMailList,
  16. 16 SendMailAddress = HangfireSettings.Instance.SendMailAddress,
  17. 17 SMTPServerAddress = HangfireSettings.Instance.SMTPServerAddress,
  18. 18 SMTPPort = HangfireSettings.Instance.SMTPPort,
  19. 19 SMTPPwd = HangfireSettings.Instance.SMTPPwd,
  20. 20 SMTPSubject = HangfireSettings.Instance.SMTPSubject
  21. 21 })
配置邮件参数

 

6,signalR 推送:宿主程序使用的weapi,因此可以通过webapi推送,这样做的好处是可以将服务当作推送服务使用,第三方接口也可以利用此来推送,

 

  1. 1 /// <summary>
  2. 2 ///用户加入组处理
  3. 3 /// </summary>
  4. 4 /// <param name="userid">用户唯一标识</param>
  5. 5 /// <param name="GroupName">组名称</param>
  6. 6 /// <returns></returns>
  7. 7 public Task InitUsers(string userid,string GroupName)
  8. 8 {
  9. 9 Console.WriteLine($"{userid}加入用户组");
  10. 10 Groups.AddToGroupAsync(Context.ConnectionId, GroupName);
  11. 11 SignalrGroups.UserGroups.Add(new SignalrGroups()
  12. 12 {
  13. 13 ConnectionId = Context.ConnectionId,
  14. 14 GroupName = GroupName,
  15. 15 UserId = userid
  16. 16 });
  17. 17 return Clients.All.SendAsync("UserJoin", "用户组数据更新,新增id为:" + Context.ConnectionId + " pid:" + userid);
  18. 18 }
  19. 19 /// <summary>
  20. 20 /// 断线的时候处理
  21. 21 /// </summary>
  22. 22 /// <param name="exception"></param>
  23. 23 /// <returns></returns>
  24. 24 public override Task OnDisconnectedAsync(Exception exception)
  25. 25 {
  26. 26 //掉线移除用户,不给其推送
  27. 27 var user = SignalrGroups.UserGroups.FirstOrDefault(c => c.ConnectionId == Context.ConnectionId);
  28. 28
  29. 29 if (user != null)
  30. 30 {
  31. 31 Console.WriteLine($"用户:{user.UserId}已离线");
  32. 32 SignalrGroups.UserGroups.Remove(user);
  33. 33 Groups.RemoveFromGroupAsync(Context.ConnectionId, user.GroupName);
  34. 34 }
  35. 35 return base.OnDisconnectedAsync(exception);
  36. 36 }
Hub定义
  1. 1 /// <summary>
  2. 2 /// 单个connectionid推送
  3. 3 /// </summary>
  4. 4 /// <param name="groups"></param>
  5. 5 /// <returns></returns>
  6. 6 [HttpPost, Route("AnyOne")]
  7. 7 public IActionResult AnyOne([FromBody]IEnumerable<SignalrGroups> groups)
  8. 8 {
  9. 9 if (groups != null && groups.Any())
  10. 10 {
  11. 11 var ids = groups.Select(c => c.UserId);
  12. 12 var list = SignalrGroups.UserGroups.Where(c => ids.Contains(c.UserId));
  13. 13 foreach (var item in list)
  14. 14 hubContext.Clients.Client(item.ConnectionId).SendAsync("AnyOne", $"{item.ConnectionId}: {item.Content}");
  15. 15 }
  16. 16 return Ok();
  17. 17 }
  18. 18
  19. 19 /// <summary>
  20. 20 /// 全部推送
  21. 21 /// </summary>
  22. 22 /// <param name="message"></param>
  23. 23 /// <returns></returns>
  24. 24 [HttpPost, Route("EveryOne")]
  25. 25 public IActionResult EveryOne([FromBody] MSG body)
  26. 26 {
  27. 27 var data = HttpContext.Response.Body;
  28. 28 hubContext.Clients.All.SendAsync("EveryOne", $"{body.message}");
  29. 29 return Ok();
  30. 30 }
  31. 31
  32. 32 /// <summary>
  33. 33 /// 单个组推送
  34. 34 /// </summary>
  35. 35 /// <param name="group"></param>
  36. 36 /// <returns></returns>
  37. 37 [HttpPost, Route("AnyGroups")]
  38. 38 public IActionResult AnyGroups([FromBody]SignalrGroups group)
  39. 39 {
  40. 40 if (group != null)
  41. 41 {
  42. 42 hubContext.Clients.Group(group.GroupName).SendAsync("AnyGroups", $"{group.Content}");
  43. 43 }
  44. 44 return Ok();
  45. 45 }
推送接口定义

 

7,接口健康检查:因为主要用来调用api接口,因此集成接口健康检查还是很有必要的,目前使用的方式是配置文件中添加需要检查的地址

  1. 1 /*健康检查配置项*/
  2. 2 "HealthChecks-UI": {
  3. 3 /*检查地址,可以配置当前程序和外部程序*/
  4. 4 "HealthChecks": [
  5. 5 {
  6. 6 "Name": "Hangfire Api 健康检查",
  7. 7 "Uri": "http://localhost:9006/healthz"
  8. 8 }
  9. 9 ],
  10. 10 /*需要检查的Api地址*/
  11. 11 "CheckUrls": [
  12. 12 {
  13. 13 "Uri": "http://localhost:17600/CityService.svc/HealthyCheck",
  14. 14 "httpMethod": "Get"
  15. 15 },
  16. 16 {
  17. 17 "Uri": "http://localhost:9098/CheckHelath",
  18. 18 "httpMethod": "Post"
  19. 19 },
  20. 20 {
  21. 21 "Uri": "http://localhost:9067/GrtHelathCheck",
  22. 22 "httpMethod": "Get"
  23. 23 },
  24. 24 {
  25. 25 "Uri": "http://localhost:9043/GrtHelathCheck",
  26. 26 "httpMethod": "Get"
  27. 27 }
  28. 28 ],
  29. 29 "Webhooks": [], //钩子配置
  30. 30 "EvaluationTimeOnSeconds": 10, //检测频率
  31. 31 "MinimumSecondsBetweenFailureNotifications": 60, //推送间隔时间
  32. 32 "HealthCheckDatabaseConnectionString": "Data Source=\\healthchecksdb" //-> sqlite库存储检查配置及日志信息
  33. 33 }
健康检查相关配置

后台会根据配置的指定间隔去检查服务接口是否可以正常访问,(这个中间件可以实现很多检查功能,包括网络,数据库,mq等,支持webhook推送等丰富功能,系统用不到因此没有添加)

健康检查的配置

  1. 1 //添加健康检查地址
  2. 2 HangfireSettings.Instance.HostServers.ForEach(s =>
  3. 3 {
  4. 4 services.AddHealthChecks().AddUrlGroup(new Uri(s.Uri), s.httpMethod.ToLower() == "post" ? HttpMethod.Post : HttpMethod.Get, $"{s.Uri}");
  5. 5 });
健康检查地址添加
  1. 1 app.UseHealthChecks("/healthz", new HealthCheckOptions()
  2. 2 {
  3. 3 Predicate = _ => true,
  4. 4 ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
  5. 5 });
  6. 6 app.UseHealthChecks("/health", options);//获取自定义格式的json数据
  7. 7 app.UseHealthChecksUI(setup =>
  8. 8 {
  9. 9 setup.UIPath = "/hc"; // 健康检查的UI面板地址
  10. 10 setup.ApiPath = "/hc-api"; // 用于api获取json的检查数据
  11. 11 });
健康检查中间件配置

其中,ui配置路径是在面板中展示检查结果需要使用的地址

api地址,可以通过接口的方式来调用检查结果,方便在第三方系统中使用,其数据格式可以自定义

 通过接口调用

  1. 1 [{
  2. 2 "id": 1,
  3. 3 "status": "Unhealthy",
  4. 4 "onStateFrom": "2019-04-07T18:00:09.6996751+08:00",
  5. 5 "lastExecuted": "2019-04-07T18:05:03.4761739+08:00",
  6. 6 "uri": "http://localhost:53583/healthz",
  7. 7 "name": "Hangfire Api 健康检查",
  8. 8 "discoveryService": null,
  9. 9 "entries": [{
  10. 10 "id": 1,
  11. 11 "name": "http://localhost:17600/CityService.svc/HealthyCheck",
  12. 12 "status": "Unhealthy",
  13. 13 "description": "An error occurred while sending the request.",
  14. 14 "duration": "00:00:04.3907375"
  15. 15 }, {
  16. 16 "id": 2,
  17. 17 "name": "http://localhost:9098/CheckHelath",
  18. 18 "status": "Unhealthy",
  19. 19 "description": "An error occurred while sending the request.",
  20. 20 "duration": "00:00:04.4140310"
  21. 21 }, {
  22. 22 "id": 3,
  23. 23 "name": "http://localhost:9067/GrtHelathCheck",
  24. 24 "status": "Unhealthy",
  25. 25 "description": "An error occurred while sending the request.",
  26. 26 "duration": "00:00:04.3847367"
  27. 27 }, {
  28. 28 "id": 4,
  29. 29 "name": "http://localhost:9043/GrtHelathCheck",
  30. 30 "status": "Unhealthy",
  31. 31 "description": "An error occurred while sending the request.",
  32. 32 "duration": "00:00:04.4499007"
  33. 33 }],
  34. 34 "history": []
  35. 35 }]
接口返回数据原始格式
  1. 1 {
  2. 2 "status": "Unhealthy",
  3. 3 "errors": [{
  4. 4 "key": "http://localhost:17600/CityService.svc/HealthyCheck",
  5. 5 "value": "Unhealthy"
  6. 6 }, {
  7. 7 "key": "http://localhost:9098/CheckHelath",
  8. 8 "value": "Unhealthy"
  9. 9 }, {
  10. 10 "key": "http://localhost:9067/GrtHelathCheck",
  11. 11 "value": "Unhealthy"
  12. 12 }, {
  13. 13 "key": "http://localhost:9043/GrtHelathCheck",
  14. 14 "value": "Unhealthy"
  15. 15 }]
  16. 16 }
接口返回数据处理后格式
  1. 1 //重写json报告数据,可用于远程调用获取健康检查结果
  2. 2 var options = new HealthCheckOptions
  3. 3 {
  4. 4 ResponseWriter = async (c, r) =>
  5. 5 {
  6. 6 c.Response.ContentType = "application/json";
  7. 7
  8. 8 var result = JsonConvert.SerializeObject(new
  9. 9 {
  10. 10 status = r.Status.ToString(),
  11. 11 errors = r.Entries.Select(e => new { key = e.Key, value = e.Value.Status.ToString() })
  12. 12 });
  13. 13 await c.Response.WriteAsync(result);
  14. 14 }
  15. 15 };
处理方式

 

8,通过接口添加任务:添加编辑周期任务,添加计划任务,触发周期任务,删除周期任务,多个任务连续一次执行的任务

  1. 1 /// <summary>
  2. 2 /// 添加一个队列任务立即被执行
  3. 3 /// </summary>
  4. 4 /// <param name="httpJob"></param>
  5. 5 /// <returns></returns>
  6. 6 [HttpPost, Route("AddBackGroundJob")]
  7. 7 public JsonResult AddBackGroundJob([FromBody] Hangfire.HttpJob.Server.HttpJobItem httpJob)
  8. 8 {
  9. 9 var addreslut = string.Empty;
  10. 10 try
  11. 11 {
  12. 12 addreslut = BackgroundJob.Enqueue(() => Hangfire.HttpJob.Server.HttpJob.Excute(httpJob, httpJob.JobName, null));
  13. 13 }
  14. 14 catch (Exception ec)
  15. 15 {
  16. 16 return Json(new Message() { Code = false, ErrorMessage = ec.ToString() });
  17. 17 }
  18. 18 return Json(new Message() { Code = true, ErrorMessage = "" });
  19. 19 }
  20. 20
  21. 21 /// <summary>
  22. 22 /// 添加一个周期任务
  23. 23 /// </summary>
  24. 24 /// <param name="httpJob"></param>
  25. 25 /// <returns></returns>
  26. 26 [HttpPost, Route("AddOrUpdateRecurringJob")]
  27. 27 public JsonResult AddOrUpdateRecurringJob([FromBody] Hangfire.HttpJob.Server.HttpJobItem httpJob)
  28. 28 {
  29. 29 try
  30. 30 {
  31. 31 RecurringJob.AddOrUpdate(httpJob.JobName, () => Hangfire.HttpJob.Server.HttpJob.Excute(httpJob, httpJob.JobName, null), httpJob.Corn, TimeZoneInfo.Local);
  32. 32 }
  33. 33 catch (Exception ec)
  34. 34 {
  35. 35 return Json(new Message() { Code = false, ErrorMessage = ec.ToString() });
  36. 36 }
  37. 37 return Json(new Message() { Code = true, ErrorMessage = "" });
  38. 38 }
  39. 39
  40. 40 /// <summary>
  41. 41 /// 删除一个周期任务
  42. 42 /// </summary>
  43. 43 /// <param name="jobname"></param>
  44. 44 /// <returns></returns>
  45. 45 [HttpGet,Route("DeleteJob")]
  46. 46 public JsonResult DeleteJob(string jobname)
  47. 47 {
  48. 48 try
  49. 49 {
  50. 50 RecurringJob.RemoveIfExists(jobname);
  51. 51 }
  52. 52 catch (Exception ec)
  53. 53 {
  54. 54 return Json(new Message() { Code = false, ErrorMessage = ec.ToString() });
  55. 55 }
  56. 56 return Json(new Message() { Code = true, ErrorMessage = "" });
  57. 57 }
  58. 58 /// <summary>
  59. 59 /// 手动触发一个任务
  60. 60 /// </summary>
  61. 61 /// <param name="jobname"></param>
  62. 62 /// <returns></returns>
  63. 63 [HttpGet, Route("TriggerRecurringJob")]
  64. 64 public JsonResult TriggerRecurringJob(string jobname)
  65. 65 {
  66. 66 try
  67. 67 {
  68. 68 RecurringJob.Trigger(jobname);
  69. 69 }
  70. 70 catch (Exception ec)
  71. 71 {
  72. 72 return Json(new Message() { Code = false, ErrorMessage = ec.ToString() });
  73. 73 }
  74. 74 return Json(new Message() { Code = true, ErrorMessage = "" });
  75. 75 }
  76. 76 /// <summary>
  77. 77 /// 添加一个延迟任务
  78. 78 /// </summary>
  79. 79 /// <param name="httpJob">httpJob.DelayFromMinutes(延迟多少分钟执行)</param>
  80. 80 /// <returns></returns>
  81. 81 [HttpPost, Route("AddScheduleJob")]
  82. 82 public JsonResult AddScheduleJob([FromBody] Hangfire.HttpJob.Server.HttpJobItem httpJob)
  83. 83 {
  84. 84 var reslut = string.Empty;
  85. 85 try
  86. 86 {
  87. 87 reslut = BackgroundJob.Schedule(() => Hangfire.HttpJob.Server.HttpJob.Excute(httpJob, httpJob.JobName, null), TimeSpan.FromMinutes(httpJob.DelayFromMinutes));
  88. 88 }
  89. 89 catch (Exception ec)
  90. 90 {
  91. 91 return Json(new Message() { Code = false, ErrorMessage = ec.ToString() });
  92. 92 }
  93. 93 return Json(new Message() { Code = true, ErrorMessage = "" });
  94. 94 }
  95. 95 /// <summary>
  96. 96 /// 添加连续任务,多个任务依次执行,只执行一次
  97. 97 /// </summary>
  98. 98 /// <param name="httpJob"></param>
  99. 99 /// <returns></returns>
  100. 100 [HttpPost, Route("AddContinueJob")]
  101. 101 public JsonResult AddContinueJob([FromBody] List<Hangfire.HttpJob.Server.HttpJobItem> httpJobItems)
  102. 102 {
  103. 103 var reslut = string.Empty;
  104. 104 var jobid = string.Empty;
  105. 105 try
  106. 106 {
  107. 107 httpJobItems.ForEach(k =>
  108. 108 {
  109. 109 if (!string.IsNullOrEmpty(jobid))
  110. 110 {
  111. 111 jobid = BackgroundJob.ContinueJobWith(jobid, () => RunContinueJob(k));
  112. 112 }
  113. 113 else
  114. 114 {
  115. 115 jobid = BackgroundJob.Enqueue(() => Hangfire.HttpJob.Server.HttpJob.Excute(k, k.JobName, null));
  116. 116 }
  117. 117 });
  118. 118 reslut = "true";
  119. 119 }
  120. 120 catch (Exception ec)
  121. 121 {
  122. 122 return Json(new Message() { Code = false, ErrorMessage = ec.ToString() });
  123. 123 }
  124. 124 return Json(new Message() { Code = true, ErrorMessage = "" });
  125. 125 }
通过接口添加任务

这样做的好处是有效利用了宿主的webapi,而且无需登录控制面板操作就能实现任务管理,方便集成管理到其他系统中

 

防止多个实例的任务并行执行,即一个任务未执行完成,另一个相同的任务开始执行,可以使用分布式锁来解决

 通过特性来添加任务重试时间间隔(hangfire 1.7 新增,单位/秒),重试次数,队列名称,任务名称,以及分布式锁超时时间

  1. 1 /// <summary>
  2. 2 /// 执行任务,DelaysInSeconds(重试时间间隔/单位秒)
  3. 3 /// </summary>
  4. 4 /// <param name="item"></param>
  5. 5 /// <param name="jobName"></param>
  6. 6 /// <param name="context"></param>
  7. 7 [AutomaticRetry(Attempts = 3, DelaysInSeconds = new[] { 30, 60, 90 }, LogEvents = true, OnAttemptsExceeded = AttemptsExceededAction.Fail)]
  8. 8 [DisplayName("Api任务:{1}")]
  9. 9 [Queue("apis")]
  10. 10 [JobFilter(timeoutInSeconds: 3600)]
配置分布式锁超时时间

 

  1. 1 //设置分布式锁,分布式锁会阻止两个相同的任务并发执行,用任务名称和方法名称作为锁
  2. 2 var jobresource = $"{filterContext.BackgroundJob.Job.Args[1]}.{filterContext.BackgroundJob.Job.Method.Name}";
  3. 3 var locktimeout = TimeSpan.FromSeconds(_timeoutInSeconds);
  4. 4 try
  5. 5 {
  6. 6 //判断任务是否被暂停
  7. 7 using (var connection = JobStorage.Current.GetConnection())
  8. 8 {
  9. 9 var conts = connection.GetAllItemsFromSet($"JobPauseOf:{filterContext.BackgroundJob.Job.Args[1]}");
  10. 10 if (conts.Contains("true"))
  11. 11 {
  12. 12 filterContext.Canceled = true;//任务被暂停不执行直接跳过
  13. 13 return;
  14. 14 }
  15. 15 }
  16. 16 //申请分布式锁
  17. 17 var distributedLock = filterContext.Connection.AcquireDistributedLock(jobresource, locktimeout);
  18. 18 filterContext.Items["DistributedLock"] = distributedLock;
  19. 19 }
  20. 20 catch (Exception ec)
  21. 21 {
  22. 22 //获取锁超时,取消任务,任务会默认置为成功
  23. 23 filterContext.Canceled = true;
  24. 24 logger.Info($"任务{filterContext.BackgroundJob.Job.Args[1]}超时,任务id{filterContext.BackgroundJob.Id}");
  25. 25 }
过滤器添加分布式锁

 

  1. 1 if (!filterContext.Items.ContainsKey("DistributedLock"))
  2. 2 {
  3. 3 throw new InvalidOperationException("找不到分布式锁,没有为该任务申请分布式锁.");
  4. 4 }
  5. 5 //释放分布式锁
  6. 6 var distributedLock = (IDisposable)filterContext.Items["DistributedLock"];
  7. 7 distributedLock.Dispose();
释放分布式锁

 

通过过滤器来设置任务过期时间,过期后自动在数据库删除历史记录

 

  1. 1 public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
  2. 2 {
  3. 3 //设置过期时间,任务将在三天后过期,过期的任务会自动被扫描并删除
  4. 4 context.JobExpirationTimeout = TimeSpan.FromDays(3);
  5. 5 }
设置任务过期时间

 

redis集群下,测试秒级任务

集群为windws环境下,一个主节点四个从节点,(使用时需要在redis连接中配置全部集群连接,主节点和从节点),目前用不到linux环境,没有进行测试。

原文链接:http://www.cnblogs.com/gnsilence/p/10665951.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号