func_modify.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package cron
  2. import (
  3. "net/http"
  4. "github.com/xinliangnote/go-gin-api/internal/code"
  5. "github.com/xinliangnote/go-gin-api/internal/pkg/core"
  6. "github.com/xinliangnote/go-gin-api/internal/pkg/validation"
  7. "github.com/xinliangnote/go-gin-api/internal/services/cron"
  8. )
  9. type modifyRequest struct {
  10. Id string `form:"id" binding:"required"` // 任务ID
  11. Name string `form:"name" binding:"required"` // 任务名称
  12. Spec string `form:"spec" binding:"required"` // crontab 表达式
  13. Command string `form:"command" binding:"required"` // 执行命令
  14. Protocol int32 `form:"protocol" binding:"required"` // 执行方式 1:shell 2:http
  15. HttpMethod int32 `form:"http_method"` // http 请求方式 1:get 2:post
  16. Timeout int32 `form:"timeout" binding:"required"` // 超时时间(单位:秒)
  17. RetryTimes int32 `form:"retry_times" binding:"required"` // 重试次数
  18. RetryInterval int32 `form:"retry_interval" binding:"required"` // 重试间隔(单位:秒)
  19. NotifyStatus int32 `form:"notify_status" binding:"required"` // 执行结束是否通知 1:不通知 2:失败通知 3:结束通知 4:结果关键字匹配通知
  20. NotifyType int32 `form:"notify_type"` // 通知类型 1:邮件 2:webhook
  21. NotifyReceiverEmail string `form:"notify_receiver_email"` // 通知者邮箱地址(多个用,分割)
  22. NotifyKeyword string `form:"notify_keyword"` // 通知匹配关键字(多个用,分割)
  23. Remark string `form:"remark"` // 备注
  24. IsUsed int32 `form:"is_used" binding:"required"` // 是否启用 1:是 -1:否
  25. }
  26. type modifyResponse struct {
  27. Id int32 `json:"id"` // 主键ID
  28. }
  29. // Modify 编辑任务
  30. // @Summary 编辑任务
  31. // @Description 编辑任务
  32. // @Tags API.cron
  33. // @Accept application/x-www-form-urlencoded
  34. // @Produce json
  35. // @Param id formData string true "hashID"
  36. // @Param name formData string true "任务名称"
  37. // @Param spec formData string true "crontab 表达式"
  38. // @Param command formData string true "执行命令"
  39. // @Param protocol formData int true "执行方式 1:shell 2:http"
  40. // @Param http_method formData int false "http 请求方式 1:get 2:post"
  41. // @Param timeout formData int true "超时时间(单位:秒)"
  42. // @Param retry_times formData int true "重试次数"
  43. // @Param retry_interval formData int true "重试间隔(单位:秒)"
  44. // @Param notify_status formData int true "执行结束是否通知 1:不通知 2:失败通知 3:结束通知 4:结果关键字匹配通知"
  45. // @Param notify_type formData int false "通知类型 1:邮件 2:webhook"
  46. // @Param notify_receiver_email formData string false "通知者邮箱地址(多个用,分割)"
  47. // @Param notify_keyword formData string false "通知匹配关键字(多个用,分割)"
  48. // @Param remark formData string false "备注"
  49. // @Param is_used formData int true "是否启用 1:是 -1:否"
  50. // @Success 200 {object} modifyResponse
  51. // @Failure 400 {object} code.Failure
  52. // @Router /api/cron/{id} [post]
  53. // @Security LoginToken
  54. func (h *handler) Modify() core.HandlerFunc {
  55. return func(ctx core.Context) {
  56. req := new(modifyRequest)
  57. res := new(modifyResponse)
  58. if err := ctx.ShouldBindForm(req); err != nil {
  59. ctx.AbortWithError(core.Error(
  60. http.StatusBadRequest,
  61. code.ParamBindError,
  62. validation.Error(err)).WithError(err),
  63. )
  64. return
  65. }
  66. ids, err := h.hashids.HashidsDecode(req.Id)
  67. if err != nil {
  68. ctx.AbortWithError(core.Error(
  69. http.StatusBadRequest,
  70. code.HashIdsDecodeError,
  71. code.Text(code.HashIdsDecodeError)).WithError(err),
  72. )
  73. return
  74. }
  75. id := int32(ids[0])
  76. modifyData := new(cron.ModifyCronTaskData)
  77. modifyData.Name = req.Name
  78. modifyData.Spec = req.Spec
  79. modifyData.Command = req.Command
  80. modifyData.Protocol = req.Protocol
  81. modifyData.HttpMethod = req.HttpMethod
  82. modifyData.Timeout = req.Timeout
  83. modifyData.RetryTimes = req.RetryTimes
  84. modifyData.RetryInterval = req.RetryInterval
  85. modifyData.NotifyStatus = req.NotifyStatus
  86. modifyData.NotifyType = req.NotifyType
  87. modifyData.NotifyReceiverEmail = req.NotifyReceiverEmail
  88. modifyData.NotifyKeyword = req.NotifyKeyword
  89. modifyData.Remark = req.Remark
  90. modifyData.IsUsed = req.IsUsed
  91. if err := h.cronService.Modify(ctx, id, modifyData); err != nil {
  92. ctx.AbortWithError(core.Error(
  93. http.StatusBadRequest,
  94. code.CronUpdateError,
  95. code.Text(code.CronUpdateError)).WithError(err),
  96. )
  97. return
  98. }
  99. res.Id = id
  100. ctx.Payload(res)
  101. }
  102. }