TaskController.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. using Microsoft.Owin;
  19. using System.Diagnostics;
  20. namespace XdCxRhDW.App.Controllers
  21. {
  22. /// <summary>
  23. /// 任务相关接口
  24. /// </summary>
  25. public class TaskController : BaseController
  26. {
  27. /// <summary>
  28. /// 获取客户端IP地址
  29. /// </summary>
  30. /// <returns></returns>
  31. [HttpGet]
  32. public async Task<AjaxResult<string>> GetClientIP()
  33. {
  34. try
  35. {
  36. var ip = RemoteIp;
  37. return Success(ip);
  38. }
  39. catch (Exception ex)
  40. {
  41. await LogHelper.Error("获取客户端IP出错", ex);
  42. return Error<string>("获取客户端IP出错");
  43. }
  44. }
  45. /// <summary>
  46. /// 停止任务
  47. /// </summary>
  48. /// <param name="dto">任务停止参数</param>
  49. /// <returns></returns>
  50. [HttpPost]
  51. public async Task<AjaxResult> StopTask(TaskStopHandleDto dto)
  52. {
  53. try
  54. {
  55. using (RHDWContext db = new RHDWContext())
  56. {
  57. var item = await db.TaskInfos.FirstOrDefaultAsync(p => p.ID == dto.ID);
  58. if (item == null)
  59. {
  60. return Error($"任务停止失败,找不到ID={dto.ID}的任务");
  61. }
  62. if (item.TaskState == EnumTaskState.Stopped)
  63. Thread.Sleep(2000);
  64. item.TaskState = EnumTaskState.Stopped;
  65. LogRes res = new LogRes()
  66. {
  67. LogTime = DateTime.Now,
  68. Module = Process.GetCurrentProcess().ProcessName,
  69. LogType = EnumLogType.Info,
  70. Msg = $"任务停止完成,ID={dto.ID},停止原因={dto.StopReason}",
  71. };
  72. db.LogRes.Add(res);
  73. await db.SaveChangesAsync();
  74. }
  75. Messenger.Defalut.Pub("任务状态改变", dto.ID);
  76. return Success();
  77. }
  78. catch (Exception ex)
  79. {
  80. Serilog.Log.Error(ex, $"任务停止异常.ID={dto.ID}");
  81. return Error(ex.Message);
  82. }
  83. }
  84. }
  85. }