service_create.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package cron
  2. import (
  3. "github.com/xinliangnote/go-gin-api/internal/pkg/core"
  4. "github.com/xinliangnote/go-gin-api/internal/repository/mysql/cron_task"
  5. )
  6. type CreateCronTaskData struct {
  7. Name string // 任务名称
  8. Spec string // crontab 表达式
  9. Command string // 执行命令
  10. Protocol int32 // 执行方式 1:shell 2:http
  11. HttpMethod int32 // http 请求方式 1:get 2:post
  12. Timeout int32 // 超时时间(单位:秒)
  13. RetryTimes int32 // 重试次数
  14. RetryInterval int32 // 重试间隔(单位:秒)
  15. NotifyStatus int32 // 执行结束是否通知 1:不通知 2:失败通知 3:结束通知 4:结果关键字匹配通知
  16. NotifyType int32 // 通知类型 1:邮件 2:webhook
  17. NotifyReceiverEmail string // 通知者邮箱地址(多个用,分割)
  18. NotifyKeyword string // 通知匹配关键字(多个用,分割)
  19. Remark string // 备注
  20. IsUsed int32 // 是否启用 1:是 -1:否
  21. }
  22. func (s *service) Create(ctx core.Context, createData *CreateCronTaskData) (id int32, err error) {
  23. model := cron_task.NewModel()
  24. model.Name = createData.Name
  25. model.Spec = createData.Spec
  26. model.Command = createData.Command
  27. model.Protocol = createData.Protocol
  28. model.HttpMethod = createData.HttpMethod
  29. model.Timeout = createData.Timeout
  30. model.RetryTimes = createData.RetryTimes
  31. model.RetryInterval = createData.RetryInterval
  32. model.NotifyStatus = createData.NotifyStatus
  33. model.NotifyType = createData.NotifyType
  34. model.NotifyReceiverEmail = createData.NotifyReceiverEmail
  35. model.NotifyKeyword = createData.NotifyKeyword
  36. model.Remark = createData.Remark
  37. model.IsUsed = createData.IsUsed
  38. model.CreatedUser = ctx.SessionUserInfo().UserName
  39. id, err = model.Create(s.db.GetDbW().WithContext(ctx.RequestContext()))
  40. if err != nil {
  41. return 0, err
  42. }
  43. s.cronServer.AddTask(model)
  44. return
  45. }