From 9d69b87312284c3f5d0044a4b60acfb86b4b3b10 Mon Sep 17 00:00:00 2001 From: marvzhang Date: Fri, 17 Jul 2020 15:58:55 +0800 Subject: [PATCH] added batch run tasks --- backend/main.go | 1 + backend/model/task.go | 8 ++--- backend/routes/task.go | 79 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/backend/main.go b/backend/main.go index b06e0418..b6fbe795 100644 --- a/backend/main.go +++ b/backend/main.go @@ -240,6 +240,7 @@ func main() { authGroup.GET("/tasks", routes.GetTaskList) // 任务列表 authGroup.GET("/tasks/:id", routes.GetTask) // 任务详情 authGroup.PUT("/tasks", routes.PutTask) // 派发任务 + authGroup.PUT("/tasks/batch", routes.PutBatchTasks) // 批量派发任务 authGroup.DELETE("/tasks/:id", routes.DeleteTask) // 删除任务 authGroup.DELETE("/tasks", routes.DeleteSelectedTask) // 删除多个任务 authGroup.DELETE("/tasks_by_status", routes.DeleteTaskByStatus) // 删除指定状态的任务 diff --git a/backend/model/task.go b/backend/model/task.go index 0b2ed0a9..71948e26 100644 --- a/backend/model/task.go +++ b/backend/model/task.go @@ -31,9 +31,10 @@ type Task struct { ScheduleId bson.ObjectId `json:"schedule_id" bson:"schedule_id"` // 前端数据 - SpiderName string `json:"spider_name"` - NodeName string `json:"node_name"` - Username string `json:"username"` + SpiderName string `json:"spider_name"` + NodeName string `json:"node_name"` + Username string `json:"username"` + NodeIds []string `json:"node_ids"` UserId bson.ObjectId `json:"user_id" bson:"user_id"` CreateTs time.Time `json:"create_ts" bson:"create_ts"` @@ -508,4 +509,3 @@ func UpdateTaskErrorLogs(taskId string, errorRegexPattern string) error { return nil } - diff --git a/backend/routes/task.go b/backend/routes/task.go index ab34e66e..081b156e 100644 --- a/backend/routes/task.go +++ b/backend/routes/task.go @@ -202,6 +202,85 @@ func PutTask(c *gin.Context) { HandleSuccessData(c, taskIds) } +func PutBatchTasks(c *gin.Context) { + var tasks []model.Task + if err := c.ShouldBindJSON(&tasks); err != nil { + HandleError(http.StatusOK, c, err) + return + } + var taskIds []string + for _, t := range tasks { + if t.RunType == constants.RunTypeAllNodes { + // 所有节点 + nodes, err := model.GetNodeList(nil) + if err != nil { + HandleError(http.StatusInternalServerError, c, err) + return + } + for _, node := range nodes { + t := model.Task{ + SpiderId: t.SpiderId, + NodeId: node.Id, + Param: t.Param, + UserId: services.GetCurrentUserId(c), + RunType: constants.RunTypeAllNodes, + ScheduleId: bson.ObjectIdHex(constants.ObjectIdNull), + } + + id, err := services.AddTask(t) + if err != nil { + HandleError(http.StatusInternalServerError, c, err) + return + } + taskIds = append(taskIds, id) + } + } else if t.RunType == constants.RunTypeRandom { + // 随机 + t := model.Task{ + SpiderId: t.SpiderId, + Param: t.Param, + UserId: services.GetCurrentUserId(c), + RunType: constants.RunTypeRandom, + ScheduleId: bson.ObjectIdHex(constants.ObjectIdNull), + } + id, err := services.AddTask(t) + if err != nil { + HandleError(http.StatusInternalServerError, c, err) + return + } + taskIds = append(taskIds, id) + } else if t.RunType == constants.RunTypeSelectedNodes { + // 指定节点 + for _, nodeId := range t.NodeIds { + t := model.Task{ + SpiderId: t.SpiderId, + NodeId: bson.ObjectIdHex(nodeId), + Param: t.Param, + UserId: services.GetCurrentUserId(c), + RunType: constants.RunTypeSelectedNodes, + ScheduleId: bson.ObjectIdHex(constants.ObjectIdNull), + } + + id, err := services.AddTask(t) + if err != nil { + HandleError(http.StatusInternalServerError, c, err) + return + } + taskIds = append(taskIds, id) + } + } else { + HandleErrorF(http.StatusInternalServerError, c, "invalid run_type") + return + } + } + + c.JSON(http.StatusOK, Response{ + Status: "ok", + Message: "success", + Data: taskIds, + }) +} + // @Summary Delete task // @Description Delete task // @Tags task