func_execute.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/spf13/cast"
  8. )
  9. type executeRequest struct {
  10. Id string `uri:"id"` // HashID
  11. }
  12. type executeResponse struct {
  13. Id int `json:"id"` // ID
  14. }
  15. // Execute 手动执行单条任务
  16. // @Summary 手动执行单条任务
  17. // @Description 手动执行单条任务
  18. // @Tags API.cron
  19. // @Accept json
  20. // @Produce json
  21. // @Param id path string true "hashId"
  22. // @Success 200 {object} detailResponse
  23. // @Failure 400 {object} code.Failure
  24. // @Router /api/cron/{id} [patch]
  25. // @Security LoginToken
  26. func (h *handler) Execute() core.HandlerFunc {
  27. return func(ctx core.Context) {
  28. req := new(executeRequest)
  29. res := new(executeResponse)
  30. if err := ctx.ShouldBindURI(req); err != nil {
  31. ctx.AbortWithError(core.Error(
  32. http.StatusBadRequest,
  33. code.ParamBindError,
  34. validation.Error(err)).WithError(err),
  35. )
  36. return
  37. }
  38. ids, err := h.hashids.HashidsDecode(req.Id)
  39. if err != nil {
  40. ctx.AbortWithError(core.Error(
  41. http.StatusBadRequest,
  42. code.HashIdsDecodeError,
  43. code.Text(code.HashIdsDecodeError)).WithError(err),
  44. )
  45. return
  46. }
  47. err = h.cronService.Execute(ctx, cast.ToInt32(ids[0]))
  48. if err != nil {
  49. ctx.AbortWithError(core.Error(
  50. http.StatusBadRequest,
  51. code.CronExecuteError,
  52. code.Text(code.CronExecuteError)).WithError(err),
  53. )
  54. return
  55. }
  56. res.Id = ids[0]
  57. ctx.Payload(res)
  58. }
  59. }