From fa4d8ddc5051095d76f84862bf3ab60a22c6dd0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=99=AF=E9=98=B3?= <1656488874@qq.com> Date: Tue, 26 Nov 2019 15:26:01 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E6=89=A7=E8=A1=8C=E7=88=AC=E8=99=AB?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/model/task.go | 1 + backend/services/spider.go | 3 +-- backend/services/task.go | 18 +++++++++++++----- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/backend/model/task.go b/backend/model/task.go index 64f06cd7..b869b506 100644 --- a/backend/model/task.go +++ b/backend/model/task.go @@ -61,6 +61,7 @@ func (t *Task) Save() error { defer s.Close() t.UpdateTs = time.Now() if err := c.UpdateId(t.Id, t); err != nil { + log.Errorf("update task error: %s", err.Error()) debug.PrintStack() return err } diff --git a/backend/services/spider.go b/backend/services/spider.go index 84d218bb..d76f1f9a 100644 --- a/backend/services/spider.go +++ b/backend/services/spider.go @@ -116,10 +116,9 @@ func PublishAllSpiders() { // 发布爬虫 func PublishSpider(spider model.Spider) { - // 查询gf file,不存在则删除 + // 可能爬虫文件不存在,则直接返回 gfFile := model.GetGridFs(spider.FileId) if gfFile == nil { - _ = model.RemoveSpider(spider.Id) return } spiderSync := spider_handler.SpiderSync{ diff --git a/backend/services/task.go b/backend/services/task.go index 67c3396b..3cb1a12c 100644 --- a/backend/services/task.go +++ b/backend/services/task.go @@ -391,15 +391,23 @@ func ExecuteTask(id int) { t.Status = constants.StatusRunning // 任务状态 t.WaitDuration = t.StartTs.Sub(t.CreateTs).Seconds() // 等待时长 + // 判断爬虫文件是否存在 + gfFile := model.GetGridFs(spider.FileId) + if gfFile == nil { + t.Error = "找不到爬虫文件,请重新上传" + t.Status = constants.StatusError + t.FinishTs = time.Now() // 结束时间 + t.RuntimeDuration = t.FinishTs.Sub(t.StartTs).Seconds() // 运行时长 + t.TotalDuration = t.FinishTs.Sub(t.CreateTs).Seconds() // 总时长 + _ = t.Save() + return + } + // 开始执行任务 log.Infof(GetWorkerPrefix(id) + "开始执行任务(ID:" + t.Id + ")") // 储存任务 - if err := t.Save(); err != nil { - log.Errorf(err.Error()) - HandleTaskError(t, err) - return - } + _ = t.Save() // 起一个cron执行器来统计任务结果数 if spider.Col != "" {