diff --git a/os/gtimer/gtimer.go b/os/gtimer/gtimer.go index c938b946f..0f146be3c 100644 --- a/os/gtimer/gtimer.go +++ b/os/gtimer/gtimer.go @@ -31,6 +31,7 @@ const ( STATUS_READY = 0 // Job is ready for running. STATUS_RUNNING = 1 // Job is already running. STATUS_STOPPED = 2 // Job is stopped. + STATUS_RESET = 3 // Job is reset. STATUS_CLOSED = -1 // Job is closed and waiting to be deleted. gPANIC_EXIT = "exit" // Internal usage for custom job exit function with panic. gDEFAULT_TIMES = math.MaxInt32 // Default limit running times, a big number. diff --git a/os/gtimer/gtimer_entry.go b/os/gtimer/gtimer_entry.go index 3d1cfe763..3d296f012 100644 --- a/os/gtimer/gtimer_entry.go +++ b/os/gtimer/gtimer_entry.go @@ -106,6 +106,11 @@ func (entry *Entry) Stop() { entry.status.Set(STATUS_STOPPED) } +//Reset reset the job. +func (entry *Entry) Reset() { + entry.status.Set(STATUS_RESET) +} + // Close closes the job, and then it will be removed from the timer. func (entry *Entry) Close() { entry.status.Set(STATUS_CLOSED) @@ -138,6 +143,8 @@ func (entry *Entry) check(nowTicks int64, nowMs int64) (runnable, addable bool) return false, true case STATUS_CLOSED: return false, false + case STATUS_RESET: + return false, true } // Firstly checks using the ticks, this may be low precision as one tick is a little bit long. if diff := nowTicks - entry.create; diff > 0 && diff%entry.interval == 0 { diff --git a/os/gtimer/gtimer_loop.go b/os/gtimer/gtimer_loop.go index 5dc652fc2..9fac8add0 100644 --- a/os/gtimer/gtimer_loop.go +++ b/os/gtimer/gtimer_loop.go @@ -74,6 +74,10 @@ func (w *wheel) proceed() { } // If rolls on the job. if addable { + //If STATUS_RESET , reset to runnable state. + if entry.Status() == STATUS_RESET { + entry.SetStatus(STATUS_READY) + } entry.wheel.timer.doAddEntryByParent(entry.rawIntervalMs, entry) } } diff --git a/os/gtimer/gtimer_z_unit_1_test.go b/os/gtimer/gtimer_z_unit_1_test.go index 1aadd161e..00010dc05 100644 --- a/os/gtimer/gtimer_z_unit_1_test.go +++ b/os/gtimer/gtimer_z_unit_1_test.go @@ -9,6 +9,7 @@ package gtimer_test import ( + "github.com/gogf/gf/os/glog" "testing" "time" @@ -73,6 +74,24 @@ func TestTimer_Start_Stop_Close(t *testing.T) { }) } +func TestTimer_Reset(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + timer := New() + array := garray.New(true) + glog.Printf("start time:%d", time.Now().Unix()) + singleton := timer.AddSingleton(2*time.Second, func() { + timestamp := time.Now().Unix() + glog.Println(timestamp) + array.Append(timestamp) + }) + time.Sleep(5 * time.Second) + glog.Printf("reset time:%d", time.Now().Unix()) + singleton.Reset() + time.Sleep(10 * time.Second) + t.Assert(array.Len(), 6) + }) +} + func TestTimer_AddSingleton(t *testing.T) { gtest.C(t, func(t *gtest.T) { timer := New()