added batch stop tasks

This commit is contained in:
marvzhang
2020-07-18 11:26:09 +08:00
parent 9c0f8be516
commit af420972ed
6 changed files with 119 additions and 24 deletions

View File

@@ -224,6 +224,7 @@ func main() {
authGroup.POST("/spiders/:id/git/reset", routes.PostSpiderResetGit) // 爬虫 Git 重置
authGroup.POST("/spiders-cancel", routes.CancelSelectedSpider) // 停止所选爬虫任务
authGroup.POST("/spiders-run", routes.RunSelectedSpider) // 运行所选爬虫
authGroup.POST("/spiders-set-projects", routes.SetProjectsSelectedSpider) // 批量设置爬虫项目
}
// 可配置爬虫
{
@@ -250,6 +251,7 @@ func main() {
authGroup.GET("/tasks/:id/results", routes.GetTaskResults) // 任务结果
authGroup.GET("/tasks/:id/results/download", routes.DownloadTaskResultsCsv) // 下载任务结果
authGroup.POST("/tasks/:id/restart", routes.RestartTask) // 重新开始任务
authGroup.POST("/tasks-cancel", routes.CancelSelectedTask) // 批量取消任务
}
// 定时任务
{

View File

@@ -869,6 +869,39 @@ func RunSelectedSpider(c *gin.Context) {
})
}
func SetProjectsSelectedSpider(c *gin.Context) {
type ReqBody struct {
ProjectId bson.ObjectId `json:"project_id"`
SpiderIds []bson.ObjectId `json:"spider_ids"`
}
var reqBody ReqBody
if err := c.ShouldBindJSON(&reqBody); err != nil {
HandleErrorF(http.StatusBadRequest, c, "invalid request")
return
}
for _, spiderId := range reqBody.SpiderIds {
spider, err := model.GetSpider(spiderId)
if err != nil {
log.Errorf(err.Error())
debug.PrintStack()
continue
}
spider.ProjectId = reqBody.ProjectId
if err := spider.Save(); err != nil {
log.Errorf(err.Error())
debug.PrintStack()
continue
}
}
c.JSON(http.StatusOK, Response{
Status: "ok",
Message: "success",
})
}
// @Summary Get task list
// @Description Get task list
// @Tags spider

View File

@@ -321,7 +321,7 @@ func DeleteTaskByStatus(c *gin.Context) {
func DeleteSelectedTask(c *gin.Context) {
ids := make(map[string][]string)
if err := c.ShouldBindJSON(&ids); err != nil {
HandleError(http.StatusInternalServerError, c, err)
HandleError(http.StatusBadRequest, c, err)
return
}
list := ids["ids"]
@@ -364,6 +364,22 @@ func DeleteTask(c *gin.Context) {
HandleSuccess(c)
}
func CancelSelectedTask(c *gin.Context) {
ids := make(map[string][]string)
if err := c.ShouldBindJSON(&ids); err != nil {
HandleError(http.StatusBadRequest, c, err)
return
}
list := ids["ids"]
for _, id := range list {
if err := services.CancelTask(id); err != nil {
HandleError(http.StatusInternalServerError, c, err)
return
}
}
HandleSuccess(c)
}
// @Summary Get task log
// @Description Get task log
// @Tags task