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 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 } 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() } } }