X2D1TaskProcessingController.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Web.Http;
  7. using XdCxRhDW.Dto;
  8. using XdCxRhDW.WebApi;
  9. using X2D1TaskServer.Service;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace X2D1TaskServer.Controllers
  13. {
  14. /// <summary>
  15. ///两星一地任务处理接口
  16. /// </summary>
  17. public class X2D1TaskProcessingController : BaseController
  18. {
  19. /*******************
  20. * !!!不要在Controller中放业务逻辑的全局变量
  21. * !!!不要在Controller写太复杂的业务逻辑
  22. * Controller主要就是调用Service层的东西。Service层执行业务逻辑和调用Repository层操作数据库
  23. * ********************/
  24. private readonly TaskHistoryService _tskHservice;
  25. private readonly TaskRealService _tskRservice;
  26. public X2D1TaskProcessingController(TaskHistoryService tskHservice, TaskRealService tskRservice)
  27. {
  28. _tskHservice = tskHservice;
  29. _tskRservice = tskRservice;
  30. }
  31. /// <summary>
  32. /// 开始执行两星一地无参任务
  33. /// </summary>
  34. /// <param name="dto">离线任务信息</param>
  35. /// <returns></returns>
  36. [HttpPost]
  37. public AjaxResult Run(X2D1TaskHandleDto dto)
  38. {
  39. try
  40. {
  41. if (_service.Stop(dto.ID))
  42. {
  43. Thread.Sleep(2000);
  44. }
  45. if (dto.TaskType == EnumTaskTypeDto.History)
  46. _tskHservice.StartAsync(dto);
  47. else
  48. _tskRservice.StartAsync(dto);
  49. return Success();
  50. }
  51. catch (Exception ex)
  52. {
  53. Serilog.Log.Error(ex, $"【任务{dto.ID}】开始执行异常!");
  54. return Error($"【任务{dto.ID}】开始执行异常!");
  55. }
  56. }
  57. /// <summary>
  58. /// 停止执行两星一地无参任务
  59. /// </summary>
  60. /// <param name="dto"></param>
  61. /// <returns></returns>
  62. [HttpPost]
  63. public async Task<AjaxResult> Stop(TaskStopHandleDto dto)
  64. {
  65. try
  66. {
  67. await LogHelper.Warning($"用户停止了任务,ID={dto.ID}");
  68. if (dto.TaskType == EnumTaskTypeDto.History)
  69. _tskHservice.Stop(dto.ID);
  70. else
  71. _tskRservice.Stop(dto.ID);
  72. return Success();
  73. }
  74. catch (Exception ex)
  75. {
  76. Serilog.Log.Error(ex, $"【任务{dto.ID}】停止执行异常!");
  77. return Error($"【任务{dto.ID}】停止执行异常!");
  78. }
  79. }
  80. }
  81. }