1 Star 0 Fork 0

micro-tools / wf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gtimer_entry.go 3.10 KB
一键复制 编辑 原始数据 按行查看 历史
545403892 提交于 2023-09-27 22:16 . 升级go-ole
package gtimer
import (
"gitee.com/micro-tools/wf/container/gtype"
"math"
)
// Entry is the timing job.
type Entry struct {
job JobFunc // The job function.
timer *Timer // Belonged timer.
ticks int64 // The job runs every ticks.
times *gtype.Int // Limit running times.
status *gtype.Int // Job status.
singleton *gtype.Bool // Singleton mode.
nextTicks *gtype.Int64 // Next run ticks of the job.
}
// JobFunc is the job function.
type JobFunc = func()
// Status returns the status of the job.
func (entry *Entry) Status() int {
return entry.status.Val()
}
// Run runs the timer job asynchronously.
func (entry *Entry) Run() {
leftRunningTimes := entry.times.Add(-1)
// Running times exceeding checks.
if leftRunningTimes < 0 {
entry.status.Set(StatusClosed)
return
}
// This means it does not limit the running times.
if leftRunningTimes == math.MaxInt32-1 {
entry.times.Set(math.MaxInt32)
}
go func() {
defer func() {
if err := recover(); err != nil {
if err != panicExit {
panic(err)
} else {
entry.Close()
return
}
}
if entry.Status() == StatusRunning {
entry.SetStatus(StatusReady)
}
}()
entry.job()
}()
}
// doCheckAndRunByTicks checks the if job can run in given timer ticks,
// it runs asynchronously if the given `currentTimerTicks` meets or else
// it increments its ticks and waits for next running check.
func (entry *Entry) doCheckAndRunByTicks(currentTimerTicks int64) {
// Ticks check.
if currentTimerTicks < entry.nextTicks.Val() {
return
}
entry.nextTicks.Set(currentTimerTicks + entry.ticks)
// Perform job checking.
switch entry.status.Val() {
case StatusRunning:
if entry.IsSingleton() {
return
}
case StatusReady:
if !entry.status.Cas(StatusReady, StatusRunning) {
return
}
case StatusStopped:
return
case StatusClosed:
return
}
// Perform job running.
entry.Run()
}
// SetStatus custom sets the status for the job.
func (entry *Entry) SetStatus(status int) int {
return entry.status.Set(status)
}
// Start starts the job.
func (entry *Entry) Start() {
entry.status.Set(StatusReady)
}
// Stop stops the job.
func (entry *Entry) Stop() {
entry.status.Set(StatusStopped)
}
// Close closes the job, and then it will be removed from the timer.
func (entry *Entry) Close() {
entry.status.Set(StatusClosed)
}
// Reset reset the job, which resets its ticks for next running.
func (entry *Entry) Reset() {
entry.nextTicks.Set(entry.timer.ticks.Val() + entry.ticks)
}
// IsSingleton checks and returns whether the job in singleton mode.
func (entry *Entry) IsSingleton() bool {
return entry.singleton.Val()
}
// SetSingleton sets the job singleton mode.
func (entry *Entry) SetSingleton(enabled bool) {
entry.singleton.Set(enabled)
}
// Job returns the job function of this job.
func (entry *Entry) Job() JobFunc {
return entry.job
}
// SetTimes sets the limit running times for the job.
func (entry *Entry) SetTimes(times int) {
entry.times.Set(times)
}
Go
1
https://gitee.com/micro-tools/wf.git
git@gitee.com:micro-tools/wf.git
micro-tools
wf
wf
v1.0.2

搜索帮助

53164aa7 5694891 3bd8fe86 5694891