camfish/cron.go

53 lines
1.0 KiB
Go

package camfish
import "time"
import "context"
// cron is a built-in actor present in every environment that triggers routine
// tasks on time intervals.
type cron struct {
trimFunc func() bool
fastTiming bool
timing struct {
trimInterval time.Duration
}
}
var _ Actor = new(cron)
var _ Runnable = new(cron)
var _ Configurable = new(cron)
func (this *cron) Type () string {
return "cron"
}
func (this *cron) Configure (config Config) error {
if str := config.Get("trim-interval"); str != "" {
value, err := time.ParseDuration(str)
if err != nil {
return NewConfigError(config, "trim-interval", 0, err)
}
this.timing.trimInterval = value
}
if this.fastTiming {
this.timing.trimInterval = time.Second * 10
}
return nil
}
func (this *cron) Run (ctx context.Context) error {
trimTicker := time.NewTicker(defaul(
this.timing.trimInterval,
defaultTrimInterval))
defer trimTicker.Stop()
for {
select {
case <- trimTicker.C:
if this.trimFunc != nil {
this.trimFunc()
}
case <- ctx.Done(): return ctx.Err()
}
}
}