From 9b1cea53b6c5c98329a8fa3783175ad72ddc1b50 Mon Sep 17 00:00:00 2001 From: marvzhang Date: Tue, 26 May 2020 14:11:05 +0800 Subject: [PATCH] fixed https://github.com/crawlab-team/crawlab/issues/747 --- backend/services/task.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/backend/services/task.go b/backend/services/task.go index 15b9d2bb..e004e8ac 100644 --- a/backend/services/task.go +++ b/backend/services/task.go @@ -15,6 +15,7 @@ import ( "errors" "fmt" "github.com/apex/log" + "github.com/globalsign/mgo" "github.com/globalsign/mgo/bson" "github.com/imroc/req" "github.com/satori/go.uuid" @@ -608,6 +609,12 @@ func ExecuteTask(id int) { // 储存任务 _ = t.Save() + // 创建结果集索引 + go func() { + col := utils.GetSpiderCol(spider.Col, spider.Name) + CreateResultsIndexes(col) + }() + // 起一个cron执行器来统计任务结果数 cronExec := cron.New(cron.WithSeconds()) _, err = cronExec.AddFunc("*/5 * * * * *", SaveTaskResultCount(t.Id)) @@ -971,6 +978,15 @@ func GetTaskMarkdownContent(t model.Task, s model.Spider) string { ) } +func CreateResultsIndexes(col string) { + s, c := database.GetCol(col) + defer s.Close() + + _ = c.EnsureIndex(mgo.Index{ + Key: []string{"task_id"}, + }) +} + func SendTaskEmail(u model.User, t model.Task, s model.Spider) { statusMsg := "has finished" if t.Status == constants.StatusError {