From 3ed64f9f222ffbd0074ecf1c531e9c1e34135b25 Mon Sep 17 00:00:00 2001 From: ouqiang Date: Tue, 30 Jan 2018 21:16:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=96:=20?= =?UTF-8?q?=E5=BD=93=E4=BB=BB=E5=8A=A1=E8=BE=83=E5=A4=9A=E6=97=B6,=20gocro?= =?UTF-8?q?n=E5=90=AF=E5=8A=A8=E6=85=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/task.go | 6 ++++-- routers/task/task.go | 2 +- service/task.go | 38 ++++++++++++++++++++++++++------------ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/models/task.go b/models/task.go index 3baa13a..7b74aae 100644 --- a/models/task.go +++ b/models/task.go @@ -120,9 +120,11 @@ func (task *Task) Enable(id int) (int64, error) { } // 获取所有激活任务 -func (task *Task) ActiveList() ([]Task, error) { +func (task *Task) ActiveList(page, pageSize int) ([]Task, error) { + params := CommonMap{"Page": page, "PageSize": pageSize} + task.parsePageAndPageSize(params) list := make([]Task, 0) - err := Db.Where("status = ? AND level = ?", Enabled, TaskLevelParent). + err := Db.Where("status = ? AND level = ?", Enabled, TaskLevelParent).Limit(task.PageSize, task.pageLimitOffset()). Find(&list) if err != nil { diff --git a/routers/task/task.go b/routers/task/task.go index c520c70..ef37b34 100644 --- a/routers/task/task.go +++ b/routers/task/task.go @@ -299,7 +299,7 @@ func addTaskToTimer(id int) { return } - service.ServiceTask.Add(task) + service.ServiceTask.RemoveAndAdd(task) } // 解析查询参数 diff --git a/service/task.go b/service/task.go index 55de470..7bf1ecb 100644 --- a/service/task.go +++ b/service/task.go @@ -95,26 +95,42 @@ type TaskResult struct { // 初始化任务, 从数据库取出所有任务, 添加到定时任务并运行 func (task Task) Initialize() { + logger.Info("开始初始化定时任务") taskModel := new(models.Task) - taskList, err := taskModel.ActiveList() - if err != nil { - logger.Error("定时任务初始化#获取任务列表错误-", err.Error()) - return + taskNum := 0 + page := 1 + pageSize := 1000 + maxPage := 1000 + for page < maxPage { + taskList, err := taskModel.ActiveList(page, pageSize) + if err != nil { + logger.Fatalf("定时任务初始化#获取任务列表错误-", err.Error()) + } + if len(taskList) == 0 { + break + } + for _, item := range taskList { + task.Add(item) + taskNum++ + } + page++ } - if len(taskList) == 0 { - logger.Debug("任务列表为空") - return - } - task.BatchAdd(taskList) + logger.Infof("定时任务初始化完成, 共%d个定时任务添加到调度器", taskNum) } // 批量添加任务 func (task Task) BatchAdd(tasks []models.Task) { for _, item := range tasks { - task.Add(item) + task.RemoveAndAdd(item) } } +// 删除任务后添加 +func (task Task) RemoveAndAdd(taskModel models.Task) { + task.Remove(taskModel.Id) + task.Add(taskModel) +} + // 添加任务 func (task Task) Add(taskModel models.Task) { if taskModel.Level == models.TaskLevelChild { @@ -128,8 +144,6 @@ func (task Task) Add(taskModel models.Task) { } cronName := strconv.Itoa(taskModel.Id) - // Cron任务采用数组存储, 删除任务需遍历数组, 并对数组重新赋值, 任务较多时,有性能问题 - serviceCron.RemoveJob(cronName) err := serviceCron.AddFunc(taskModel.Spec, taskFunc, cronName) if err != nil { logger.Error("添加任务到调度器失败#", err)