From 72c7e65dfa30adbd7e7ff868bf738a8d57e4d03c Mon Sep 17 00:00:00 2001 From: John Date: Wed, 16 Jan 2019 21:06:35 +0800 Subject: [PATCH] gcron updates --- g/os/gcron/gcron.go | 1 + g/os/gcron/gcron_cron.go | 55 +++++++++++++++++++++++++++++--- g/os/gcron/gcron_entry.go | 2 +- g/os/gcron/gcron_jobloop.go | 62 ------------------------------------- 4 files changed, 52 insertions(+), 68 deletions(-) delete mode 100644 g/os/gcron/gcron_jobloop.go diff --git a/g/os/gcron/gcron.go b/g/os/gcron/gcron.go index 6c8eff514..71fc6003d 100644 --- a/g/os/gcron/gcron.go +++ b/g/os/gcron/gcron.go @@ -16,6 +16,7 @@ const ( STATUS_READY = 0 STATUS_RUNNING = 1 + STATUS_STOPPED = 2 STATUS_CLOSED = -1 ) diff --git a/g/os/gcron/gcron_cron.go b/g/os/gcron/gcron_cron.go index 6257b6285..4cf6ca4a9 100644 --- a/g/os/gcron/gcron_cron.go +++ b/g/os/gcron/gcron_cron.go @@ -20,7 +20,7 @@ import ( // 定时任务管理对象 type Cron struct { idgen *gtype.Int // 用于唯一名称生成 - status *gtype.Int // 定时任务状态(0: 未执行; 1: 运行中; -1:删除关闭) + status *gtype.Int // 定时任务状态(0: 未执行; 1: 运行中; 2: 已停止; -1:删除关闭) entries *gmap.StringInterfaceMap // 所有的定时任务项 } @@ -31,7 +31,14 @@ func New() *Cron { status : gtype.NewInt(STATUS_RUNNING), entries : gmap.NewStringInterfaceMap(), } - cron.startLoop() + gtimer.Add(time.Second, func() { + if cron.status.Val() == STATUS_CLOSED { + gtimer.Exit() + } + if cron.status.Val() == STATUS_RUNNING { + go cron.checkEntries(time.Now()) + } + }) return cron } @@ -124,7 +131,7 @@ func (c *Cron) Start(name...string) { } } } else { - c.status.Set(STATUS_RUNNING) + c.status.Set(STATUS_READY) } } @@ -137,7 +144,7 @@ func (c *Cron) Stop(name...string) { } } } else { - c.status.Set(STATUS_READY) + c.status.Set(STATUS_STOPPED) } } @@ -160,7 +167,7 @@ func (c *Cron) Entries() []*Entry { return 1 } return -1 - }, false) + }, true) c.entries.RLockFunc(func(m map[string]interface{}) { for _, v := range m { array.Add(v.(*Entry)) @@ -174,3 +181,41 @@ func (c *Cron) Entries() []*Entry { }) return entries } + +// 遍历检查可执行定时任务,并异步执行 +func (c *Cron) checkEntries(t time.Time) { + c.entries.RLockFunc(func(m map[string]interface{}) { + for _, v := range m { + entry := v.(*Entry) + if entry.schedule.meet(t) { + // 是否已命令停止运行 + if entry.status.Val() == STATUS_STOPPED { + continue + } + switch entry.mode.Val() { + // 是否只允许单例运行 + case MODE_SINGLETON: + if entry.status.Set(STATUS_RUNNING) == STATUS_RUNNING { + continue + } + // 只运行一次的任务 + case MODE_ONCE: + if entry.status.Set(STATUS_CLOSED) == STATUS_CLOSED { + continue + } + } + // 执行异步运行 + go func() { + defer func() { + if entry.status.Val() != STATUS_CLOSED { + entry.status.Set(STATUS_READY) + } else { + c.Remove(entry.Name) + } + }() + entry.Job() + }() + } + } + }) +} diff --git a/g/os/gcron/gcron_entry.go b/g/os/gcron/gcron_entry.go index ed9399a1f..7eed16703 100644 --- a/g/os/gcron/gcron_entry.go +++ b/g/os/gcron/gcron_entry.go @@ -61,5 +61,5 @@ func (entry *Entry) Start() { // 停止定时任务 func (entry *Entry) Stop() { - entry.status.Set(STATUS_CLOSED) + entry.status.Set(STATUS_STOPPED) } diff --git a/g/os/gcron/gcron_jobloop.go b/g/os/gcron/gcron_jobloop.go deleted file mode 100644 index d076bdfaa..000000000 --- a/g/os/gcron/gcron_jobloop.go +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright 2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://gitee.com/johng/gf. - -package gcron - -import ( - "gitee.com/johng/gf/g/os/gtimer" - "time" -) - -// 延迟添加定时任务,delay参数单位为秒 -func (c *Cron) startLoop() { - gtimer.Add(time.Second, func() { - if c.status.Val() == STATUS_CLOSED { - gtimer.Exit() - } - if c.status.Val() == STATUS_RUNNING { - go c.checkEntries(time.Now()) - } - }) -} - -// 遍历检查可执行定时任务,并异步执行 -func (c *Cron) checkEntries(t time.Time) { - c.entries.RLockFunc(func(m map[string]interface{}) { - for _, v := range m { - entry := v.(*Entry) - if entry.schedule.meet(t) { - // 是否已命令停止运行 - if entry.status.Val() == STATUS_CLOSED { - continue - } - switch entry.mode.Val() { - // 是否只允许单例运行 - case MODE_SINGLETON: - if entry.status.Set(STATUS_RUNNING) == STATUS_RUNNING { - continue - } - // 只运行一次的任务 - case MODE_ONCE: - if entry.status.Set(STATUS_CLOSED) == STATUS_CLOSED { - continue - } - } - // 执行异步运行 - go func() { - defer func() { - if entry.status.Val() != STATUS_CLOSED { - entry.status.Set(STATUS_READY) - } else { - c.Remove(entry.Name) - } - }() - entry.Job() - }() - } - } - }) -} \ No newline at end of file