added batch add schedules

This commit is contained in:
marvzhang
2020-07-18 12:28:25 +08:00
parent db02be1585
commit 04757bfe06
2 changed files with 40 additions and 0 deletions

View File

@@ -259,6 +259,7 @@ func main() {
authGroup.GET("/schedules", routes.GetScheduleList) // 定时任务列表
authGroup.GET("/schedules/:id", routes.GetSchedule) // 定时任务详情
authGroup.PUT("/schedules", routes.PutSchedule) // 创建定时任务
authGroup.PUT("/schedules/batch", routes.PutBatchSchedules) // 批量创建定时任务
authGroup.POST("/schedules/:id", routes.PostSchedule) // 修改定时任务
authGroup.DELETE("/schedules/:id", routes.DeleteSchedule) // 删除定时任务
authGroup.POST("/schedules/:id/disable", routes.DisableSchedule) // 禁用定时任务

View File

@@ -3,9 +3,11 @@ package routes
import (
"crawlab/model"
"crawlab/services"
"github.com/apex/log"
"github.com/gin-gonic/gin"
"github.com/globalsign/mgo/bson"
"net/http"
"runtime/debug"
)
// @Summary Get schedule list
@@ -220,3 +222,40 @@ func EnableSchedule(c *gin.Context) {
}
HandleSuccess(c)
}
func PutBatchSchedules(c *gin.Context) {
var schedules []model.Schedule
if err := c.ShouldBindJSON(&schedules); err != nil {
HandleError(http.StatusBadRequest, c, err)
return
}
for _, s := range schedules {
// 验证cron表达式
if err := services.ParserCron(s.Cron); err != nil {
log.Errorf("parse cron error: " + err.Error())
debug.PrintStack()
HandleError(http.StatusInternalServerError, c, err)
return
}
// 添加 UserID
s.UserId = services.GetCurrentUserId(c)
// 添加定时任务
if err := model.AddSchedule(s); err != nil {
log.Errorf("add schedule error: " + err.Error())
debug.PrintStack()
HandleError(http.StatusInternalServerError, c, err)
return
}
}
// 更新定时任务
if err := services.Sched.Update(); err != nil {
HandleError(http.StatusInternalServerError, c, err)
return
}
HandleSuccess(c)
}