Files
go-gin-api/internal/api/controller/cron_handler/func_execute.go
T
新亮 382c7d3c8f feature(1.2.7): 新增 cron_server - 后台任务
- import jakecoffman/cron ;
- 可在 WEB 界面中进行新增任务、编辑任务、手动执行任务等;
- 新增 cron.log 记录后台任务执行的日志;
- 调整项目初始化,使其支持安装后台任务模块;
2021-08-28 15:09:20 +08:00

69 lines
1.5 KiB
Go
Executable File

package cron_handler
import (
"net/http"
"github.com/xinliangnote/go-gin-api/internal/pkg/code"
"github.com/xinliangnote/go-gin-api/internal/pkg/core"
"github.com/xinliangnote/go-gin-api/internal/pkg/validation"
"github.com/xinliangnote/go-gin-api/pkg/errno"
"github.com/spf13/cast"
)
type executeRequest struct {
Id string `uri:"id"` // HashID
}
type executeResponse struct {
Id int `json:"id"` // ID
}
// Execute 手动执行单条任务
// @Summary 手动执行单条任务
// @Description 手动执行单条任务
// @Tags API.cron
// @Accept json
// @Produce json
// @Param id path string true "hashId"
// @Success 200 {object} detailResponse
// @Failure 400 {object} code.Failure
// @Router /api/cron/:id [patch]
func (h *handler) Execute() core.HandlerFunc {
return func(ctx core.Context) {
req := new(executeRequest)
res := new(executeResponse)
if err := ctx.ShouldBindURI(req); err != nil {
ctx.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.ParamBindError,
validation.Error(err)).WithErr(err),
)
return
}
ids, err := h.hashids.HashidsDecode(req.Id)
if err != nil {
ctx.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.HashIdsDecodeError,
code.Text(code.HashIdsDecodeError)).WithErr(err),
)
return
}
err = h.cronService.Execute(ctx, cast.ToInt32(ids[0]))
if err != nil {
ctx.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.CronExecuteError,
code.Text(code.CronExecuteError)).WithErr(err),
)
return
}
res.Id = ids[0]
ctx.Payload(res)
}
}