timer

package
v0.12.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2019 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// LoopForever is a constant indicating that timer should loop forever
	LoopForever = -1
)

Variables

View Source
var (
	// Manager manager for all Timers
	Manager = &struct {
		incrementID    int64      // auto increment id
		timers         sync.Map   // all Timers
		ChClosingTimer chan int64 // timer for closing
		ChCreatedTimer chan *Timer
	}{}

	// Precision indicates the precision of timer, default is time.Second
	Precision = time.Second

	// GlobalTicker represents global ticker that all cron job will be executed
	// in globalTicker.
	GlobalTicker *time.Ticker
)

Functions

func AddTimer

func AddTimer(t *Timer)

AddTimer adds a timer to the manager

func Cron

func Cron()

Cron executes scheduled tasks TODO: if closing Timers'count in single cron call more than timerBacklog will case problem.

func RemoveTimer

func RemoveTimer(id int64)

RemoveTimer removes a timer to the manager

func SetTimerBacklog

func SetTimerBacklog(c int)

SetTimerBacklog set the timer created/closing channel backlog, A small backlog may cause the logic to be blocked when call NewTimer/NewCountTimer/timer.Stop in main logic gorontine.

Types

type Condition

type Condition interface {
	Check(now time.Time) bool
}

Condition represents a checker that returns true when cron job needs to execute

type Func

type Func func()

Func represents a function which will be called periodically in main logic gorontine.

type Timer

type Timer struct {
	ID int64 // timer id
	// contains filtered or unexported fields
}

Timer represents a cron job

func NewTimer

func NewTimer(fn Func, interval time.Duration, counter int) *Timer

NewTimer creates a cron job

func (*Timer) SetCondition

func (t *Timer) SetCondition(condition Condition)

SetCondition sets the condition used for verifying when the cron job should run

func (*Timer) Stop

func (t *Timer) Stop()

Stop turns off a timer. After Stop, fn will not be called forever

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL