SysJobService.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. namespace Admin.NET.Core.Service;
  2. /// <summary>
  3. /// 系统作业任务服务
  4. /// </summary>
  5. [ApiDescriptionSettings(Order = 188)]
  6. public class SysJobService : IDynamicApiController, ITransient
  7. {
  8. private readonly SqlSugarRepository<SysJobDetail> _sysJobDetailRep;
  9. private readonly SqlSugarRepository<SysJobTrigger> _sysJobTriggerRep;
  10. private readonly SqlSugarRepository<SysJobCluster> _sysJobClusterRep;
  11. private readonly ISchedulerFactory _schedulerFactory;
  12. public SysJobService(SqlSugarRepository<SysJobDetail> sysJobDetailRep,
  13. SqlSugarRepository<SysJobTrigger> sysJobTriggerRep,
  14. SqlSugarRepository<SysJobCluster> sysJobClusterRep,
  15. ISchedulerFactory schedulerFactory)
  16. {
  17. _sysJobDetailRep = sysJobDetailRep;
  18. _sysJobTriggerRep = sysJobTriggerRep;
  19. _sysJobClusterRep = sysJobClusterRep;
  20. _schedulerFactory = schedulerFactory;
  21. }
  22. /// <summary>
  23. /// 获取作业分页列表
  24. /// </summary>
  25. [HttpGet("/sysJob/page")]
  26. public async Task<SqlSugarPagedList<JobOutput>> GetJobPage([FromQuery] PageJobInput input)
  27. {
  28. var jobDetails = await _sysJobDetailRep.AsQueryable()
  29. .WhereIF(!string.IsNullOrWhiteSpace(input.JobId), u => u.JobId.Contains(input.JobId))
  30. .WhereIF(!string.IsNullOrWhiteSpace(input.Description), u => u.Description.Contains(input.Description))
  31. .Select(d => new JobOutput
  32. {
  33. JobDetail = d,
  34. }).ToPagedListAsync(input.Page, input.PageSize);
  35. await _sysJobDetailRep.AsSugarClient().ThenMapperAsync(jobDetails.Items, async u =>
  36. {
  37. u.JobTriggers = await _sysJobTriggerRep.GetListAsync(t => t.JobId == u.JobDetail.JobId);
  38. });
  39. return jobDetails;
  40. }
  41. /// <summary>
  42. /// 添加作业
  43. /// </summary>
  44. /// <returns></returns>
  45. [HttpPost("/sysJob/detailAdd")]
  46. public async Task AddJobDetail(AddJobDetailInput input)
  47. {
  48. var isExist = await _sysJobDetailRep.IsAnyAsync(u => u.JobId == input.JobId && u.Id != input.Id);
  49. if (isExist)
  50. throw Oops.Oh(ErrorCodeEnum.D1006);
  51. await _sysJobDetailRep.UpdateAsync(input.Adapt<SysJobDetail>());
  52. }
  53. /// <summary>
  54. /// 更新作业
  55. /// </summary>
  56. /// <returns></returns>
  57. [HttpPost("/sysJob/detailUpdate")]
  58. public async Task UpdateJobDetail(UpdateJobDetailInput input)
  59. {
  60. var isExist = await _sysJobDetailRep.IsAnyAsync(u => u.JobId == input.JobId && u.Id != input.Id);
  61. if (isExist)
  62. throw Oops.Oh(ErrorCodeEnum.D1006);
  63. await _sysJobDetailRep.UpdateAsync(input.Adapt<SysJobDetail>());
  64. }
  65. /// <summary>
  66. /// 删除作业
  67. /// </summary>
  68. /// <returns></returns>
  69. [HttpPost("/sysJob/detailDelete")]
  70. public async Task DeleteJobDetail(DeleteJobDetailInput input)
  71. {
  72. _schedulerFactory.RemoveJob(input.JobId);
  73. await _sysJobDetailRep.DeleteAsync(u => u.JobId == input.JobId);
  74. await _sysJobTriggerRep.DeleteAsync(u => u.JobId == input.JobId);
  75. }
  76. /// <summary>
  77. /// 获取触发器列表
  78. /// </summary>
  79. [HttpGet("/sysJob/triggerList")]
  80. public async Task<List<SysJobTrigger>> GetJobTriggerList([FromQuery] JobDetailInput input)
  81. {
  82. return await _sysJobTriggerRep.AsQueryable()
  83. .WhereIF(!string.IsNullOrWhiteSpace(input.JobId), u => u.JobId.Contains(input.JobId))
  84. .ToListAsync();
  85. }
  86. /// <summary>
  87. /// 添加触发器
  88. /// </summary>
  89. /// <returns></returns>
  90. [HttpPost("/sysJob/triggerAdd")]
  91. public async Task AddJobTrigger(AddJobTriggerInput input)
  92. {
  93. var isExist = await _sysJobTriggerRep.IsAnyAsync(u => u.TriggerId == input.TriggerId && u.Id != input.Id);
  94. if (isExist)
  95. throw Oops.Oh(ErrorCodeEnum.D1006);
  96. await _sysJobTriggerRep.UpdateAsync(input.Adapt<SysJobTrigger>());
  97. }
  98. /// <summary>
  99. /// 更新触发器
  100. /// </summary>
  101. /// <returns></returns>
  102. [HttpPost("/sysJob/triggerUpdate")]
  103. public async Task UpdateJobTrigger(UpdateJobTriggerInput input)
  104. {
  105. var isExist = await _sysJobTriggerRep.IsAnyAsync(u => u.TriggerId == input.TriggerId && u.Id != input.Id);
  106. if (isExist)
  107. throw Oops.Oh(ErrorCodeEnum.D1006);
  108. await _sysJobTriggerRep.UpdateAsync(input.Adapt<SysJobTrigger>());
  109. }
  110. /// <summary>
  111. /// 删除触发器
  112. /// </summary>
  113. /// <returns></returns>
  114. [HttpPost("/sysJob/triggerDelete")]
  115. public async Task DeleteJobTrigger(DeleteJobTriggerInput input)
  116. {
  117. await _sysJobTriggerRep.DeleteAsync(u => u.TriggerId == input.TriggerId);
  118. }
  119. /// <summary>
  120. /// 暂停所有作业
  121. /// </summary>
  122. /// <returns></returns>
  123. [HttpPost("/sysJob/pauseAll")]
  124. public void PauseAllJob()
  125. {
  126. _schedulerFactory.PauseAll();
  127. }
  128. /// <summary>
  129. /// 启动所有作业
  130. /// </summary>
  131. /// <returns></returns>
  132. [HttpPost("/sysJob/startAll")]
  133. public void StartAllJob()
  134. {
  135. _schedulerFactory.StartAll();
  136. }
  137. /// <summary>
  138. /// 暂停作业
  139. /// </summary>
  140. [HttpPost("/sysJob/pauseJob")]
  141. public void PauseJob(JobDetailInput input)
  142. {
  143. _ = _schedulerFactory.TryGetJob(input.JobId, out var _scheduler);
  144. _scheduler?.Pause();
  145. }
  146. /// <summary>
  147. /// 启动作业
  148. /// </summary>
  149. [HttpPost("/sysJob/startJob")]
  150. public void StartJob(JobDetailInput input)
  151. {
  152. _ = _schedulerFactory.TryGetJob(input.JobId, out var _scheduler);
  153. _scheduler?.Start();
  154. }
  155. /// <summary>
  156. /// 暂停触发器
  157. /// </summary>
  158. [HttpPost("/sysJob/pauseTrigger")]
  159. public void PauseTrigger(JobTriggerInput input)
  160. {
  161. _ = _schedulerFactory.TryGetJob(input.JobId, out var _scheduler);
  162. _scheduler?.PauseTrigger(input.TriggerId);
  163. }
  164. /// <summary>
  165. /// 启动触发器
  166. /// </summary>
  167. [HttpPost("/sysJob/startTrigger")]
  168. public void StartTrigger(JobTriggerInput input)
  169. {
  170. _ = _schedulerFactory.TryGetJob(input.JobId, out var _scheduler);
  171. _scheduler?.StartTrigger(input.TriggerId);
  172. }
  173. /// <summary>
  174. /// 强制唤醒作业调度器
  175. /// </summary>
  176. [HttpPost("/sysJob/cancelSleep")]
  177. public void CancelSleep()
  178. {
  179. _schedulerFactory.CancelSleep();
  180. }
  181. /// <summary>
  182. /// 强制触发所有作业持久化
  183. /// </summary>
  184. [HttpPost("/sysJob/persistAll")]
  185. public void PersistAll()
  186. {
  187. _schedulerFactory.PersistAll();
  188. }
  189. /// <summary>
  190. /// 获取集群列表
  191. /// </summary>
  192. [HttpGet("/sysJob/clusterList")]
  193. public async Task<List<SysJobCluster>> GetJobClusterList()
  194. {
  195. return await _sysJobClusterRep.GetListAsync();
  196. }
  197. }