|
|
|
@ -17,13 +17,21 @@ type Config struct { |
|
|
|
|
// stopping the process is aborted.
|
|
|
|
|
Stop func() error |
|
|
|
|
|
|
|
|
|
// IdleTick is called by Process for every iteration of Tick. If
|
|
|
|
|
// an error is returned, it is sent to IdleProcessError. IdleTick
|
|
|
|
|
// and IdleProcessError must both be set or unset; mixed set-edness
|
|
|
|
|
// will result in a panic.
|
|
|
|
|
IdleTick func() error |
|
|
|
|
IdleProcessError func(error) |
|
|
|
|
Tick time.Duration |
|
|
|
|
// If TickDuration is non-zero, the process will count ticks where
|
|
|
|
|
// the process is active or idle, identified as active by calling
|
|
|
|
|
// the Touch() method. When the process has been idle for more than
|
|
|
|
|
// MaxIdleTicks, it will be stopped. If ticking is enabled, TickError
|
|
|
|
|
// must be set or operations will result in a panic.
|
|
|
|
|
TickDuration time.Duration |
|
|
|
|
TickError func(error) |
|
|
|
|
|
|
|
|
|
// Tick is called by Process for every iteration of Tick. If
|
|
|
|
|
// an error is returned, it is sent to TickError. Additionally,
|
|
|
|
|
// IdleTick and ActiveTick are called for idle and active ticks
|
|
|
|
|
// respectively.
|
|
|
|
|
Tick func() error |
|
|
|
|
IdleTick func() error |
|
|
|
|
ActiveTick func() error |
|
|
|
|
|
|
|
|
|
// MaxIdleTicks, if nonzero and ticking is configured, will cause
|
|
|
|
|
// the process to stop once the maximum number of ticks has been
|
|
|
|
@ -32,8 +40,8 @@ type Config struct { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (cfg Config) validate() { |
|
|
|
|
allSet := cfg.IdleTick == nil && cfg.IdleProcessError == nil && cfg.Tick == 0 |
|
|
|
|
noneSet := cfg.IdleTick != nil && cfg.IdleProcessError != nil && cfg.Tick != 0 |
|
|
|
|
allSet := cfg.TickError == nil && cfg.TickDuration == 0 |
|
|
|
|
noneSet := cfg.TickError != nil && cfg.TickDuration != 0 |
|
|
|
|
|
|
|
|
|
if !allSet && !noneSet { |
|
|
|
|
panic("idleshut: Config.IdleTick, config.IdleProcessError, and config.Tick must all be set or all be unset") |
|
|
|
@ -108,13 +116,13 @@ func (p *Process) generation() uint { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (p *Process) startTicker() { |
|
|
|
|
if p.cfg.Tick == 0 || p.cfg.IdleTick == nil { |
|
|
|
|
if p.cfg.TickDuration == 0 || p.cfg.IdleTick == nil { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
startingGen := p.generation() |
|
|
|
|
|
|
|
|
|
tk := time.NewTicker(p.cfg.Tick) |
|
|
|
|
tk := time.NewTicker(p.cfg.TickDuration) |
|
|
|
|
for { |
|
|
|
|
<-tk.C |
|
|
|
|
// if we stopped running, or we started again and thus the geneneration has advanced, return
|
|
|
|
@ -125,10 +133,16 @@ func (p *Process) startTicker() { |
|
|
|
|
func() { |
|
|
|
|
p.mtx.Lock() |
|
|
|
|
defer p.mtx.Unlock() |
|
|
|
|
if p.cfg.Tick != nil { |
|
|
|
|
p.idleProcessError(p.cfg.Tick()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if p.touched { |
|
|
|
|
p.idleTicks = 0 |
|
|
|
|
p.touched = false //reset
|
|
|
|
|
if p.cfg.ActiveTick != nil { |
|
|
|
|
p.idleProcessError(p.cfg.ActiveTick()) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
p.idleTicks++ |
|
|
|
|
if p.cfg.IdleTick != nil { |
|
|
|
@ -143,8 +157,8 @@ func (p *Process) startTicker() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (p *Process) idleProcessError(err error) { |
|
|
|
|
if err != nil && p.cfg.IdleProcessError != nil { |
|
|
|
|
go p.cfg.IdleProcessError(err) |
|
|
|
|
if err != nil && p.cfg.TickError != nil { |
|
|
|
|
go p.cfg.TickError(err) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|