Skip to content

Commit

Permalink
Merge pull request #352 from crawlab-team/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
tikazyq authored Dec 6, 2019
2 parents 0b2d59f + 2259307 commit 4c4c62f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 28 deletions.
26 changes: 0 additions & 26 deletions .github/workflows/nodejs.yml

This file was deleted.

3 changes: 2 additions & 1 deletion backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {
panic(err)
}
log.Info("初始化定期清理日志配置成功")
}else {
} else {
log.Info("默认未开启定期清理日志配置")
}

Expand Down Expand Up @@ -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_by_status", routes.DeleteTaskByStatus) //删除指定状态的任务
authGroup.POST("/tasks/:id/cancel", routes.CancelTask) // 取消任务
authGroup.GET("/tasks/:id/log", routes.GetTaskLog) // 任务日志
authGroup.GET("/tasks/:id/results", routes.GetTaskResults) // 任务结果
Expand Down
16 changes: 16 additions & 0 deletions backend/model/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ func GetTask(id string) (Task, error) {
return task, nil
}



func AddTask(item Task) error {
s, c := database.GetCol("tasks")
defer s.Close()
Expand Down Expand Up @@ -187,6 +189,20 @@ func RemoveTask(id string) error {
return nil
}

func RemoveTaskByStatus(status string) error {
tasks, err := GetTaskList(bson.M{"status": status}, 0, constants.Infinite, "-create_ts")
if err != nil {
log.Error("get tasks error:" + err.Error())
}
for _, task := range tasks {
if err := RemoveTask(task.Id); err != nil {
log.Error("remove task error:" + err.Error())
continue
}
}
return nil
}

// 删除task by spider_id
func RemoveTaskBySpiderId(id bson.ObjectId) error {
tasks, err := GetTaskList(bson.M{"spider_id": id}, 0, constants.Infinite, "-create_ts")
Expand Down
26 changes: 26 additions & 0 deletions backend/routes/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type TaskListRequestData struct {
PageSize int `form:"page_size"`
NodeId string `form:"node_id"`
SpiderId string `form:"spider_id"`
Status string `form:"status"`
}

type TaskResultsRequestData struct {
Expand Down Expand Up @@ -46,6 +47,10 @@ func GetTaskList(c *gin.Context) {
if data.SpiderId != "" {
query["spider_id"] = bson.ObjectIdHex(data.SpiderId)
}
//新增根据任务状态获取task列表
if data.Status != "" {
query["status"] = data.Status
}

// 获取任务列表
tasks, err := model.GetTaskList(query, (data.PageNum-1)*data.PageSize, data.PageSize, "-create_ts")
Expand Down Expand Up @@ -156,6 +161,27 @@ func PutTask(c *gin.Context) {
})
}

func DeleteTaskByStatus(c *gin.Context) {
status := c.Query("status")

//删除相应的日志文件
if err := services.RemoveLogByTaskStatus(status); err != nil {
HandleError(http.StatusInternalServerError, c, err)
return
}

//删除该状态下的task
if err := model.RemoveTaskByStatus(status); err != nil {
HandleError(http.StatusInternalServerError, c, err)
return
}

c.JSON(http.StatusOK, Response{
Status: "ok",
Message: "success",
})
}

func DeleteTask(c *gin.Context) {
id := c.Param("id")

Expand Down
12 changes: 12 additions & 0 deletions backend/services/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ func RemoveLogByTaskId(id string) error {
return nil
}

func RemoveLogByTaskStatus(status string) error {
tasks, err := model.GetTaskList(bson.M{"status": status}, 0, constants.Infinite, "-create_ts")
if err != nil {
log.Error("get tasks error:" + err.Error())
return err
}
for _, task := range tasks {
RemoveLogByTaskId(task.Id)
}
return nil
}

func removeLog(t model.Task) {
if err := RemoveLocalLog(t.LogPath); err != nil {
log.Errorf("remove local log error: %s", err.Error())
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/Config/ConfigList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ export default {
}
})
},
onCrawl () {
async onCrawl () {
await this.onSave()
this.crawlConfirmDialogVisible = true
this.$st.sendEv('爬虫详情-配置', '点击运行')
},
Expand Down

0 comments on commit 4c4c62f

Please sign in to comment.