完成批量删除Task任务列表

This commit is contained in:
陈景阳
2019-12-08 10:58:02 +08:00
parent 18f34f1703
commit f9f04093d9
9 changed files with 124 additions and 113 deletions

View File

@@ -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) // 任务日志

View File

@@ -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
}

View File

@@ -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()

View File

@@ -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)
}

View File

@@ -63,7 +63,7 @@ const put = (path, data) => {
}
const del = (path, data) => {
return request('DELETE', path)
return request('DELETE', path, {}, data)
}
export default {

View File

@@ -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`)
}
}

View File

@@ -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`)

View File

@@ -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(() => {})
}

View File

@@ -4,31 +4,12 @@
<!--filter-->
<div class="filter">
<div class="left">
<!--<el-select size="small" class="filter-select"-->
<!--v-model="filter.node_id"-->
<!--:placeholder="$t('Node')"-->
<!--filterable-->
<!--clearable-->
<!--@change="onSelectNode">-->
<!--<el-option v-for="op in nodeList" :key="op._id" :value="op._id" :label="op.name"></el-option>-->
<!--</el-select>-->
<!--<el-select size="small" class="filter-select"-->
<!--v-model="filter.spider_id"-->
<!--:placeholder="$t('Spider')"-->
<!--filterable-->
<!--clearable-->
<!--@change="onSelectSpider">-->
<!--<el-option v-for="op in spiderList" :key="op._id" :value="op._id" :label="op.name"></el-option>-->
<!--</el-select>-->
<!--<el-button size="small" type="success"-->
<!--icon="el-icon-search"-->
<!--class="refresh"-->
<!--@click="onRefresh">-->
<!--{{$t('Search')}}-->
<!--</el-button>-->
</div>
<!--<div class="right">-->
<!--</div>-->
<div class="right">
<el-button @click="onRemoveMultipleTask" size="small" type="danger">
删除任务
</el-button>
</div>
</div>
<!--./filter-->
@@ -38,7 +19,9 @@
:header-cell-style="{background:'rgb(48, 65, 86)',color:'white'}"
border
@row-click="onRowClick"
@selection-change="onSelectionChange">
>
<el-table-column type="selection" width="55"/>
<template v-for="col in columns">
<el-table-column v-if="col.name === 'spider_name'"
:key="col.name"
@@ -181,7 +164,9 @@ export default {
{ name: 'total_duration', label: 'Total Duration (sec)', width: '80', align: 'right' },
{ name: 'result_count', label: 'Results Count', width: '80' }
// { name: 'avg_num_results', label: 'Average Results Count per Second', width: '80' }
]
],
multipleSelection: []
}
},
computed: {
@@ -228,12 +213,6 @@ export default {
}
return false
})
// .filter((d, index) => {
// // pagination
// const pageNum = this.pageNum
// const pageSize = this.pageSize
// return (pageSize * (pageNum - 1) <= index) && (index < pageSize * pageNum)
// })
}
},
methods: {
@@ -244,11 +223,34 @@ export default {
this.$store.dispatch('task/getTaskList')
this.$st.sendEv('任务', '搜索')
},
onSelectNode () {
this.$st.sendEv('任务', '选择节点')
},
onSelectSpider () {
this.$st.sendEv('任务', '选择爬虫')
onRemoveMultipleTask () {
if (this.multipleSelection.length === 0) {
this.$message({
type: 'error',
message: '选择要删除的任务'
})
}
this.$confirm('确定删除任务', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
let ids = this.multipleSelection.map(item => item._id)
this.$store.dispatch('task/deleteTaskMultiple', ids).then((resp) => {
if (resp.data.status === 'ok') {
this.$message({
type: 'success',
message: '删除任务成功'
})
this.$store.dispatch('task/getTaskList')
return
}
this.$message({
type: 'error',
message: resp.data.error
})
})
}).catch(() => {})
},
onRemove (row, ev) {
ev.stopPropagation()
@@ -304,6 +306,9 @@ export default {
if (column.label !== this.$t('Action')) {
this.onView(row)
}
},
onSelectionChange (val) {
this.multipleSelection = val
}
},
created () {
@@ -312,10 +317,9 @@ export default {
this.$store.dispatch('node/getNodeList')
},
mounted () {
// request task list every 5 seconds
this.handle = setInterval(() => {
this.$store.dispatch('task/getTaskList')
}, 5000)
// this.handle = setInterval(() => {
// this.$store.dispatch('task/getTaskList')
// }, 5000)
},
destroyed () {
clearInterval(this.handle)