From f9f04093d91130d90276df324090724560876ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=99=AF=E9=98=B3?= <1656488874@qq.com> Date: Sun, 8 Dec 2019 10:58:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=89=B9=E9=87=8F=E5=88=A0?= =?UTF-8?q?=E9=99=A4Task=E4=BB=BB=E5=8A=A1=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/main.go | 1 + backend/model/node.go | 2 +- backend/model/task.go | 3 +- backend/routes/task.go | 106 +++++++++---------- frontend/src/api/request.js | 2 +- frontend/src/store/modules/schedule.js | 13 +-- frontend/src/store/modules/task.js | 5 + frontend/src/views/schedule/ScheduleList.vue | 21 +++- frontend/src/views/task/TaskList.vue | 84 ++++++++------- 9 files changed, 124 insertions(+), 113 deletions(-) diff --git a/backend/main.go b/backend/main.go index 07adcb4e..0d7b7cc1 100644 --- a/backend/main.go +++ b/backend/main.go @@ -154,6 +154,7 @@ func main() { authGroup.GET("/tasks/:id", routes.GetTask) // 任务详情 authGroup.PUT("/tasks", routes.PutTask) // 派发任务 authGroup.DELETE("/tasks/:id", routes.DeleteTask) // 删除任务 + authGroup.DELETE("/tasks_multiple", routes.DeleteMultipleTask) // 删除多个任务 authGroup.DELETE("/tasks_by_status", routes.DeleteTaskByStatus) //删除指定状态的任务 authGroup.POST("/tasks/:id/cancel", routes.CancelTask) // 取消任务 authGroup.GET("/tasks/:id/log", routes.GetTaskLog) // 任务日志 diff --git a/backend/model/node.go b/backend/model/node.go index 1c63fc3e..a24b36e3 100644 --- a/backend/model/node.go +++ b/backend/model/node.go @@ -169,7 +169,7 @@ func GetNode(id bson.ObjectId) (Node, error) { defer s.Close() if err := c.FindId(id).One(&node); err != nil { - log.Errorf(err.Error()) + log.Errorf("get node error: %s, id: %s", err.Error(), id.Hex()) debug.PrintStack() return node, err } diff --git a/backend/model/task.go b/backend/model/task.go index 856e8589..299661ed 100644 --- a/backend/model/task.go +++ b/backend/model/task.go @@ -153,14 +153,13 @@ func GetTask(id string) (Task, error) { var task Task if err := c.FindId(id).One(&task); err != nil { + log.Infof("get task error: %s, id: %s", err.Error(), id) debug.PrintStack() return task, err } return task, nil } - - func AddTask(item Task) error { s, c := database.GetCol("tasks") defer s.Close() diff --git a/backend/routes/task.go b/backend/routes/task.go index 3d6edece..c8703ed4 100644 --- a/backend/routes/task.go +++ b/backend/routes/task.go @@ -29,7 +29,7 @@ func GetTaskList(c *gin.Context) { // 绑定数据 data := TaskListRequestData{} if err := c.ShouldBindQuery(&data); err != nil { - HandleError(http.StatusBadRequest, c, err) + HandleError(http.StatusOK, c, err) return } if data.PageNum == 0 { @@ -55,14 +55,14 @@ func GetTaskList(c *gin.Context) { // 获取任务列表 tasks, err := model.GetTaskList(query, (data.PageNum-1)*data.PageSize, data.PageSize, "-create_ts") if err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } // 获取总任务数 total, err := model.GetTaskListTotal(query) if err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } @@ -79,14 +79,10 @@ func GetTask(c *gin.Context) { result, err := model.GetTask(id) if err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } - c.JSON(http.StatusOK, Response{ - Status: "ok", - Message: "success", - Data: result, - }) + HandleSuccessData(c, result) } func PutTask(c *gin.Context) { @@ -100,7 +96,7 @@ func PutTask(c *gin.Context) { // 绑定数据 var reqBody TaskRequestBody if err := c.ShouldBindJSON(&reqBody); err != nil { - HandleError(http.StatusBadRequest, c, err) + HandleError(http.StatusOK, c, err) return } @@ -108,7 +104,7 @@ func PutTask(c *gin.Context) { // 所有节点 nodes, err := model.GetNodeList(nil) if err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } for _, node := range nodes { @@ -119,7 +115,7 @@ func PutTask(c *gin.Context) { } if err := services.AddTask(t); err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } } @@ -131,7 +127,7 @@ func PutTask(c *gin.Context) { Param: reqBody.Param, } if err := services.AddTask(t); err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } @@ -145,20 +141,16 @@ func PutTask(c *gin.Context) { } if err := services.AddTask(t); err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } } } else { - HandleErrorF(http.StatusBadRequest, c, "invalid run_type") + HandleErrorF(http.StatusOK, c, "invalid run_type") return } - - c.JSON(http.StatusOK, Response{ - Status: "ok", - Message: "success", - }) + HandleSuccess(c) } func DeleteTaskByStatus(c *gin.Context) { @@ -166,57 +158,65 @@ func DeleteTaskByStatus(c *gin.Context) { //删除相应的日志文件 if err := services.RemoveLogByTaskStatus(status); err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } //删除该状态下的task if err := model.RemoveTaskByStatus(status); err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } - c.JSON(http.StatusOK, Response{ - Status: "ok", - Message: "success", - }) + HandleSuccess(c) } +// 删除多个任务 +func DeleteMultipleTask(c *gin.Context) { + ids := make(map[string][]string) + if err := c.ShouldBindJSON(&ids); err != nil { + HandleError(http.StatusOK, c, err) + return + } + list := ids["ids"] + for _, id := range list { + if err := services.RemoveLogByTaskId(id); err != nil { + HandleError(http.StatusOK, c, err) + return + } + if err := model.RemoveTask(id); err != nil { + HandleError(http.StatusOK, c, err) + return + } + } + HandleSuccess(c) +} + +// 删除单个任务 func DeleteTask(c *gin.Context) { id := c.Param("id") // 删除日志文件 if err := services.RemoveLogByTaskId(id); err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } - // 删除task if err := model.RemoveTask(id); err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } - - c.JSON(http.StatusOK, Response{ - Status: "ok", - Message: "success", - }) + HandleSuccess(c) } func GetTaskLog(c *gin.Context) { id := c.Param("id") - logStr, err := services.GetTaskLog(id) if err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } - - c.JSON(http.StatusOK, Response{ - Status: "ok", - Message: "success", - Data: logStr, - }) + HandleSuccessData(c, logStr) } func GetTaskResults(c *gin.Context) { @@ -225,21 +225,21 @@ func GetTaskResults(c *gin.Context) { // 绑定数据 data := TaskResultsRequestData{} if err := c.ShouldBindQuery(&data); err != nil { - HandleError(http.StatusBadRequest, c, err) + HandleError(http.StatusOK, c, err) return } // 获取任务 task, err := model.GetTask(id) if err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } // 获取结果 results, total, err := task.GetResults(data.PageNum, data.PageSize) if err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } @@ -257,14 +257,14 @@ func DownloadTaskResultsCsv(c *gin.Context) { // 获取任务 task, err := model.GetTask(id) if err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } // 获取结果 results, _, err := task.GetResults(1, constants.Infinite) if err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } @@ -289,7 +289,7 @@ func DownloadTaskResultsCsv(c *gin.Context) { // 写入表头 if err := writer.Write(columns); err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } @@ -305,7 +305,7 @@ func DownloadTaskResultsCsv(c *gin.Context) { // 写入数据 if err := writer.Write(values); err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } } @@ -324,12 +324,8 @@ func CancelTask(c *gin.Context) { id := c.Param("id") if err := services.CancelTask(id); err != nil { - HandleError(http.StatusInternalServerError, c, err) + HandleError(http.StatusOK, c, err) return } - - c.JSON(http.StatusOK, Response{ - Status: "ok", - Message: "success", - }) + HandleSuccess(c) } diff --git a/frontend/src/api/request.js b/frontend/src/api/request.js index 22707159..9c5943f5 100644 --- a/frontend/src/api/request.js +++ b/frontend/src/api/request.js @@ -63,7 +63,7 @@ const put = (path, data) => { } const del = (path, data) => { - return request('DELETE', path) + return request('DELETE', path, {}, data) } export default { diff --git a/frontend/src/store/modules/schedule.js b/frontend/src/store/modules/schedule.js index caa1857d..7c705ac3 100644 --- a/frontend/src/store/modules/schedule.js +++ b/frontend/src/store/modules/schedule.js @@ -1,5 +1,4 @@ import request from '../../api/request' - const state = { scheduleList: [], scheduleForm: {} @@ -33,18 +32,10 @@ const actions = { request.delete(`/schedules/${id}`) }, stopSchedule ({ state, dispatch }, id) { - request.post(`/schedules/${id}/stop`).then((resp) => { - if (resp.data.status === 'ok') { - dispatch(`getScheduleList`) - } - }) + return request.post(`/schedules/${id}/stop`) }, runSchedule ({ state, dispatch }, id) { - return request.post(`/schedules/${id}/run`).then((resp) => { - if (resp.data.status === 'ok') { - dispatch(`getScheduleList`) - } - }) + return request.post(`/schedules/${id}/run`) } } diff --git a/frontend/src/store/modules/task.js b/frontend/src/store/modules/task.js index bb182706..9de29000 100644 --- a/frontend/src/store/modules/task.js +++ b/frontend/src/store/modules/task.js @@ -102,6 +102,11 @@ const actions = { dispatch('getTaskList') }) }, + deleteTaskMultiple ({ state }, ids) { + return request.delete(`/tasks_multiple`, { + ids: ids + }) + }, getTaskLog ({ state, commit }, id) { commit('SET_TASK_LOG', '') return request.get(`/tasks/${id}/log`) diff --git a/frontend/src/views/schedule/ScheduleList.vue b/frontend/src/views/schedule/ScheduleList.vue index e8ed5cd5..f07de8f9 100644 --- a/frontend/src/views/schedule/ScheduleList.vue +++ b/frontend/src/views/schedule/ScheduleList.vue @@ -249,19 +249,27 @@ export default { this.$st.sendEv('定时任务', '删除', 'id', row._id) }, onCrawl (row) { + // 停止定时任务 if (!row.status || row.status === 'running') { this.$confirm('确定停止定时任务?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => { - // 停止定时任务 this.$store.dispatch('schedule/stopSchedule', row._id) .then((resp) => { - this.$store.dispatch('schedule/getScheduleList') + if (resp.data.status === 'ok') { + this.$store.dispatch('schedule/getScheduleList') + return + } + this.$message({ + type: 'error', + message: resp.data.error + }) }) }).catch(() => {}) } + // 运行定时任务 if (row.status === 'stop') { this.$confirm('确定运行定时任务?', '提示', { confirmButtonText: '确定', @@ -270,7 +278,14 @@ export default { }).then(() => { this.$store.dispatch('schedule/runSchedule', row._id) .then((resp) => { - this.$store.dispatch('schedule/getScheduleList') + if (resp.data.status === 'ok') { + this.$store.dispatch('schedule/getScheduleList') + return + } + this.$message({ + type: 'error', + message: resp.data.error + }) }) }).catch(() => {}) } diff --git a/frontend/src/views/task/TaskList.vue b/frontend/src/views/task/TaskList.vue index 3ab19fee..2c8b73bc 100644 --- a/frontend/src/views/task/TaskList.vue +++ b/frontend/src/views/task/TaskList.vue @@ -4,31 +4,12 @@
- - - - - - - - - - - - - - - - - - - - - -
- - +
+ + 删除任务 + +
@@ -38,7 +19,9 @@ :header-cell-style="{background:'rgb(48, 65, 86)',color:'white'}" border @row-click="onRowClick" + @selection-change="onSelectionChange"> > +