NewTimerService runs iteration function on every interval tick. When iteration returns error, service fails.
(interval time.Duration, start StartingFn, iter OneIteration, stop StoppingFn)
| 23 | |
| 24 | // NewTimerService runs iteration function on every interval tick. When iteration returns error, service fails. |
| 25 | func NewTimerService(interval time.Duration, start StartingFn, iter OneIteration, stop StoppingFn) *BasicService { |
| 26 | run := func(ctx context.Context) error { |
| 27 | t := time.NewTicker(interval) |
| 28 | defer t.Stop() |
| 29 | |
| 30 | for { |
| 31 | select { |
| 32 | case <-t.C: |
| 33 | err := iter(ctx) |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | case <-ctx.Done(): |
| 39 | return nil |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return NewBasicService(start, run, stop) |
| 45 | } |
| 46 | |
| 47 | // NewListener provides a simple way to build service listener from supplied functions. |
| 48 | // Functions are only called when not nil. |