TaskController.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Data.Entity;
  5. using System.Data.Entity.Migrations;
  6. using System.Data.SqlClient;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Web.Http;
  12. using XdCxRhDW.Dto;
  13. using XdCxRhDW.Api;
  14. using XdCxRhDW.Entity;
  15. using XdCxRhDW.Repostory;
  16. using XdCxRhDW.WebApi;
  17. using System.Threading;
  18. namespace XdCxRhDW.App.Controllers
  19. {
  20. /// <summary>
  21. /// 任务相关接口
  22. /// </summary>
  23. public class TaskController : BaseController
  24. {
  25. /// <summary>
  26. /// 停止任务
  27. /// </summary>
  28. /// <param name="dto">任务停止参数</param>
  29. /// <returns></returns>
  30. [HttpPost]
  31. public async Task<AjaxResult> StopTask(TaskStopHandleDto dto)
  32. {
  33. try
  34. {
  35. using (RHDWContext db = new RHDWContext())
  36. {
  37. var item = await db.TaskInfos.FirstOrDefaultAsync(p => p.ID == dto.ID);
  38. if (item == null)
  39. {
  40. return Error($"任务停止失败,找不到ID={dto.ID}的任务");
  41. }
  42. if (item.TaskState == EnumTaskState.Stopped)
  43. Thread.Sleep(2000);
  44. item.TaskState = EnumTaskState.Stopped;
  45. await db.SaveChangesAsync();
  46. }
  47. Messenger.Defalut.Pub("任务状态改变", dto.ID);
  48. if (dto.StopType == EnumTaskStopType.Properly)
  49. Serilog.Log.Information($"任务停止完成,ID={dto.ID},停止原因={dto.StopReason}");
  50. else
  51. Serilog.Log.Error($"任务停止完成,ID={dto.ID},停止原因={dto.StopReason}");
  52. return Success();
  53. }
  54. catch (Exception ex)
  55. {
  56. Serilog.Log.Error(ex, $"任务停止异常.ID={dto.ID}");
  57. return Error(ex.Message);
  58. }
  59. }
  60. }
  61. }