job executes f every getInterval.
(ctx context.Context, f func(context.Context), getInterval func() time.Duration)
| 7 | |
| 8 | // job executes f every getInterval. |
| 9 | func job(ctx context.Context, f func(context.Context), getInterval func() time.Duration) { |
| 10 | currentInterval := getInterval() |
| 11 | ticker := time.NewTicker(currentInterval) |
| 12 | |
| 13 | for { |
| 14 | select { |
| 15 | case <-ctx.Done(): |
| 16 | return |
| 17 | case <-ticker.C: |
| 18 | jobCtx, cancel := context.WithTimeout(ctx, currentInterval) |
| 19 | f(jobCtx) |
| 20 | cancel() |
| 21 | } |
| 22 | |
| 23 | newInterval := getInterval() |
| 24 | if currentInterval != newInterval { |
| 25 | ticker = time.NewTicker(newInterval) |
| 26 | currentInterval = newInterval |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func constantInterval(interval time.Duration) func() time.Duration { |
| 32 | return func() time.Duration { |