task.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { AdminRoute } from '../../../decorator/router_register';
  2. import BaseController from '../../base';
  3. import { PageGetDto } from '../../../dto/comm';
  4. import { CreateTaskDto, UpdateTaskDto, CheckIdTaskDto } from '../../../dto/admin/sys/task';
  5. /**
  6. * 请求追踪控制器
  7. */
  8. export default class SysTaskController extends BaseController {
  9. /**
  10. * @api {get} /admin/sys/task/page 获取任务列表
  11. * @apiGroup 任务调度
  12. * @apiUse Auth
  13. * @apiUse BaseRes
  14. * @apiUse Page
  15. * @apiSuccess {SysTask[]} data.list 任务列表
  16. */
  17. @AdminRoute('/sys/task/page', 'get')
  18. async page() {
  19. const dto = await this.ctx.validate<PageGetDto>(PageGetDto, this.getQuery());
  20. this.res({
  21. data: {
  22. list: await this.service.admin.sys.task.page(dto.current - 1, dto.pageSize),
  23. pagination: {
  24. page: dto.current,
  25. size: dto.pageSize,
  26. total: await this.service.admin.sys.task.count(),
  27. },
  28. },
  29. });
  30. }
  31. /**
  32. * @api {post} /admin/sys/task/add 新增任务
  33. * @apiGroup 任务调度
  34. * @apiUse Auth
  35. * @apiUse BaseRes
  36. * @apiParam {String} name 任务名称
  37. * @apiParam {String} service 调用服务路径
  38. * @apiParam {Number} type 任务类型
  39. * @apiParam {Number} status 任务状态
  40. * @apiParam {String} startTime 启动时间
  41. * @apiParam {String} endTime 启动时间
  42. * @apiParam {Number} limit 最大运行次数,小于等于0则不限次数
  43. * @apiParam {String} cron cron表达式
  44. * @apiParam {Number} every 间隔时间
  45. * @apiParam {String} data 运行参数,JSON格式的字符串
  46. * @apiParam {String} remark 任务备注
  47. */
  48. @AdminRoute('/sys/task/add', 'post')
  49. async add() {
  50. const dto = await this.ctx.validate<CreateTaskDto>(CreateTaskDto);
  51. await this.service.admin.sys.task.addOrUpdate(dto);
  52. this.res();
  53. }
  54. /**
  55. * @api {post} /admin/sys/task/update 更新任务
  56. * @apiGroup 任务调度
  57. * @apiUse Auth
  58. * @apiUse BaseRes
  59. * @apiParam {String} name 任务名称
  60. * @apiParam {String} service 调用服务路径
  61. * @apiParam {Number} type 任务类型
  62. * @apiParam {Number} status 任务状态
  63. * @apiParam {String} startTime 启动时间
  64. * @apiParam {String} endTime 启动时间
  65. * @apiParam {Number} limit 最大运行次数,小于等于0则不限次数
  66. * @apiParam {String} cron cron表达式
  67. * @apiParam {Number} every 间隔时间
  68. * @apiParam {String} data 运行参数,JSON格式的字符串
  69. * @apiParam {String} remark 任务备注
  70. * @apiParam {Number} id 任务编号
  71. */
  72. @AdminRoute('/sys/task/update', 'post')
  73. async update() {
  74. const dto = await this.ctx.validate<UpdateTaskDto>(UpdateTaskDto);
  75. await this.service.admin.sys.task.addOrUpdate(dto);
  76. this.res();
  77. }
  78. /**
  79. * @api {get} /admin/sys/task/info 获取任务信息
  80. * @apiGroup 任务调度
  81. * @apiUse Auth
  82. * @apiUse BaseRes
  83. * @apiParam {Number} id 任务编号
  84. * @apiSuccess {SysTask} data 任务列表
  85. */
  86. @AdminRoute('/sys/task/info', 'post')
  87. async info() {
  88. const dto = await this.ctx.validate<CheckIdTaskDto>(CheckIdTaskDto);
  89. this.res({
  90. data: await this.service.admin.sys.task.info(dto.id),
  91. });
  92. }
  93. /**
  94. * @api {post} /admin/sys/task/once 手动执行一次任务
  95. * @apiGroup 任务调度
  96. * @apiUse Auth
  97. * @apiUse BaseRes
  98. * @apiParam {Number} id 任务编号
  99. */
  100. @AdminRoute('/sys/task/once', 'post')
  101. async once() {
  102. const dto = await this.ctx.validate<CheckIdTaskDto>(CheckIdTaskDto);
  103. const task = await this.service.admin.sys.task.info(dto.id);
  104. await this.service.admin.sys.task.once(task!);
  105. this.res();
  106. }
  107. /**
  108. * @api {post} /admin/sys/task/stop 停止任务
  109. * @apiGroup 任务调度
  110. * @apiUse Auth
  111. * @apiUse BaseRes
  112. * @apiParam {Number} id 任务编号
  113. */
  114. @AdminRoute('/sys/task/stop', 'post')
  115. async stop() {
  116. const dto = await this.ctx.validate<CheckIdTaskDto>(CheckIdTaskDto);
  117. const task = await this.service.admin.sys.task.info(dto.id);
  118. await this.service.admin.sys.task.stop(task!);
  119. this.res();
  120. }
  121. /**
  122. * @api {post} /admin/sys/task/start 启动任务
  123. * @apiGroup 任务调度
  124. * @apiUse Auth
  125. * @apiUse BaseRes
  126. * @apiParam {Number} id 任务编号
  127. */
  128. @AdminRoute('/sys/task/start', 'post')
  129. async start() {
  130. const dto = await this.ctx.validate<CheckIdTaskDto>(CheckIdTaskDto);
  131. const task = await this.service.admin.sys.task.info(dto.id);
  132. await this.service.admin.sys.task.start(task!);
  133. this.res();
  134. }
  135. /**
  136. * @api {post} /admin/sys/task/delete 删除任务
  137. * @apiGroup 任务调度
  138. * @apiUse Auth
  139. * @apiUse BaseRes
  140. * @apiParam {Number} id 任务编号
  141. */
  142. @AdminRoute('/sys/task/delete', 'post')
  143. async delete() {
  144. const dto = await this.ctx.validate<CheckIdTaskDto>(CheckIdTaskDto);
  145. const task = await this.service.admin.sys.task.info(dto.id);
  146. await this.service.admin.sys.task.delete(task!);
  147. this.res();
  148. }
  149. }