func_create.go 4.2 KB

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