mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
Merge pull request #835 from XWR940711/master
add func gtimer.Entry.Reset()
This commit is contained in:
@ -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.
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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()
|
||||
|
||||
Reference in New Issue
Block a user