| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package nagios |
| 4 | |
| 5 | import ( |
| 6 | "time" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/plugin/scripts.d/pkg/timeperiod" |
| 9 | ) |
| 10 | |
| 11 | func (s *collectState) scheduleRegular(now, intervalBase time.Time, interval time.Duration) { |
| 12 | s.nextAnniversary = advanceAnniversary(intervalBase, interval, now) |
| 13 | s.nextDue = s.nextAnniversary |
| 14 | } |
| 15 | |
| 16 | func (s *collectState) scheduleRetry(now time.Time, interval time.Duration) { |
| 17 | s.nextAnniversary = advanceAnniversary(now, interval, now) |
| 18 | s.nextDue = s.nextAnniversary |
| 19 | } |
| 20 | |
| 21 | func (s *collectState) scheduleNextAllowed(now time.Time, interval time.Duration, period *timeperiod.Period) { |
| 22 | next := time.Time{} |
| 23 | if period != nil { |
| 24 | next = period.NextAllowed(now) |
| 25 | } |
| 26 | if next.IsZero() { |
| 27 | next = now.Add(intervalOrDefault(interval)) |
| 28 | } |
| 29 | s.nextAnniversary = next |
| 30 | s.nextDue = next |
| 31 | } |
| 32 | |
| 33 | func advanceAnniversary(current time.Time, interval time.Duration, now time.Time) time.Time { |
| 34 | interval = intervalOrDefault(interval) |
| 35 | if current.IsZero() { |
| 36 | current = now |
| 37 | } |
| 38 | next := current.Add(interval) |
| 39 | if !next.After(now) { |
| 40 | diff := now.Sub(next) |
| 41 | steps := diff/interval + 1 |
| 42 | next = next.Add(time.Duration(steps) * interval) |
| 43 | } |
| 44 | return next |
| 45 | } |
| 46 | |
| 47 | func intervalOrDefault(d time.Duration) time.Duration { |
| 48 | if d <= 0 { |
| 49 | return time.Minute |
| 50 | } |
| 51 | return d |
| 52 | } |