|
| 1 | +package scheduler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/evilsocket/opensnitch/daemon/log" |
| 9 | +) |
| 10 | + |
| 11 | +// if the computer enters sleep mode, the duration of the sleep is substracted to time.Sleep() |
| 12 | +// Example: |
| 13 | +// - the timer fires at 18:51; it'll be checked again in 1h and will fire in 24h. |
| 14 | +// - last check at 00:51 |
| 15 | +// - computer put to sleep at 01:51 |
| 16 | +// - it wakes up at 10:31, having slept for 8h. |
| 17 | +// - the timer attempts to fire at 10:31, exactly 8h before the deadline. |
| 18 | +func timeHasDrifted(now, tms *time.Time) bool { |
| 19 | + return now.Minute() != tms.Minute() && now.Second() != tms.Second() |
| 20 | +} |
| 21 | + |
| 22 | +func waitToStart(ctx context.Context, id int, t string, wait time.Duration, tms *time.Time, drifted chan struct{}) (bool, bool) { |
| 23 | + now := time.Now() |
| 24 | + for { |
| 25 | + select { |
| 26 | + case <-ctx.Done(): |
| 27 | + goto Exit |
| 28 | + case <-time.After(wait): |
| 29 | + realNow := time.Now() |
| 30 | + log.Debug("[tasks-scheduler] %d, %s ticker ready: %s - after: %s", id, t, realNow.Format(time.DateTime), now.Format(time.DateTime)) |
| 31 | + goto Continue |
| 32 | + case <-drifted: |
| 33 | + //stopTimer(tck) |
| 34 | + goto Reschedule |
| 35 | + } |
| 36 | + } |
| 37 | +Exit: |
| 38 | + return true, false |
| 39 | +Continue: |
| 40 | + return false, false |
| 41 | +Reschedule: |
| 42 | + return false, true |
| 43 | +} |
| 44 | + |
| 45 | +// calcDailyTicker calculates the amount of time to wait until the timer must start. |
| 46 | +func calcDailyTicker(tm string) (*time.Time, time.Duration) { |
| 47 | + // support 2 formats when specifying times: |
| 48 | + // - 15:04:05 |
| 49 | + // - 15:04 -> assume seconds == 00 |
| 50 | + tms, err := time.Parse("15:04:05", tm) |
| 51 | + if err != nil { |
| 52 | + tms, err = time.Parse("15:04", tm) |
| 53 | + if err != nil { |
| 54 | + log.Error("[tasks-scheduler] invalid daily ticker time: %s", err) |
| 55 | + return nil, time.Millisecond |
| 56 | + } |
| 57 | + } |
| 58 | + wait := time.Millisecond |
| 59 | + now := time.Now().Round(0) |
| 60 | + tmd := time.Date( |
| 61 | + now.Year(), now.Month(), now.Day(), |
| 62 | + tms.Hour(), tms.Minute(), tms.Second(), 0, |
| 63 | + now.Location(), |
| 64 | + ) |
| 65 | + // if the Ticker is created before the time, wait until the ticker |
| 66 | + if tmd.Before(now) { |
| 67 | + wait = (24 * time.Hour) - now.Sub(tmd) |
| 68 | + } else if tmd.After(now) { |
| 69 | + wait = time.Until(tmd) |
| 70 | + } |
| 71 | + log.Debug("[tasks-scheduler] NewDailyTicker scheduled %s, waiting to start: %s", tm, wait) |
| 72 | + |
| 73 | + return &tmd, wait |
| 74 | +} |
| 75 | + |
| 76 | +// NewDailyTicker creates a new ticker. |
| 77 | +func NewDailyTicker(tm string) (*time.Ticker, *time.Time, time.Duration) { |
| 78 | + tms, wait := calcDailyTicker(tm) |
| 79 | + return time.NewTicker(wait), tms, wait |
| 80 | +} |
| 81 | + |
| 82 | +func (s *Scheduler) stopTimer(id int, t *time.Ticker) { |
| 83 | + t.Stop() |
| 84 | + s.mu.Lock() |
| 85 | + delete(s.Tickers, id) |
| 86 | + s.mu.Unlock() |
| 87 | +} |
| 88 | + |
| 89 | +// Instruct the timers to stop. |
| 90 | +// Mainly when the clock has drifted. |
| 91 | +func (s *Scheduler) restartTimers(drifted chan struct{}) { |
| 92 | + timers := len(s.Config.Time) |
| 93 | + for range timers { |
| 94 | + select { |
| 95 | + case drifted <- struct{}{}: |
| 96 | + default: |
| 97 | + log.Trace("[tasks-scheduler] restartTimers() unable to deliver") |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// SetupDailyTimers creates the daily timers that will fire every 24h at the configured hour. |
| 103 | +// We create the timers and wait for the remaining time from now until the configured hour. |
| 104 | +// From that on, the timer will be scheduled to tick every 24h. |
| 105 | +func (s *Scheduler) SetupDailyTimers() { |
| 106 | + var wg sync.WaitGroup |
| 107 | + drifted := make(chan struct{}) |
| 108 | + |
| 109 | + for id, t := range s.Config.Time { |
| 110 | + wg.Add(1) |
| 111 | + go func(drifted chan struct{}) { |
| 112 | + defer wg.Done() |
| 113 | + |
| 114 | + Reschedule: |
| 115 | + tck, tms, wait := NewDailyTicker(t) |
| 116 | + if tck == nil { |
| 117 | + log.Error("[tasks-scheduler] invalid timer %d-%s", id, t) |
| 118 | + return |
| 119 | + } |
| 120 | + // save tickers to stop them later when stopping the scheduler. |
| 121 | + s.mu.Lock() |
| 122 | + s.Tickers[id] = tck |
| 123 | + s.mu.Unlock() |
| 124 | + |
| 125 | + exit, resched := waitToStart(s.Ctx, id, t, wait, tms, drifted) |
| 126 | + if exit { |
| 127 | + goto Exit |
| 128 | + } |
| 129 | + if resched { |
| 130 | + s.stopTimer(id, tck) |
| 131 | + goto Reschedule |
| 132 | + } |
| 133 | + |
| 134 | + log.Debug("[tasks-scheduler] %d, %s daily ticker started", id, t) |
| 135 | + for { |
| 136 | + select { |
| 137 | + case <-s.Ctx.Done(): |
| 138 | + goto Exit |
| 139 | + case <-drifted: |
| 140 | + now := time.Now() |
| 141 | + log.Debug("[tasks-scheduler] %d, %s running ticker drifted, now: %v", id, t, now.Format(time.DateTime)) |
| 142 | + s.stopTimer(id, tck) |
| 143 | + goto Reschedule |
| 144 | + case now := <-tck.C: |
| 145 | + realNow := time.Now() |
| 146 | + //log.Debug("[tasks-scheduler] %d, %s tick now: %s real-now: %s tms: %s", id, t, now.Format(time.DateTime), realNow.Format(time.DateTime), tms.Format(time.DateTime)) |
| 147 | + // these timers are scheduled every hour, so the minute and second should match. |
| 148 | + // If they don't, the clock has drifted. |
| 149 | + if timeHasDrifted(&realNow, tms) { |
| 150 | + log.Debug("[tasks-scheduler] %d, %s tick out-of-sync, rescheduling: %s", id, t, realNow.Format(time.DateTime)) |
| 151 | + s.restartTimers(drifted) |
| 152 | + s.stopTimer(id, tck) |
| 153 | + goto Reschedule |
| 154 | + } |
| 155 | + |
| 156 | + today := int(now.Weekday()) |
| 157 | + for _, wd := range s.Config.Weekday { |
| 158 | + if wd != today { |
| 159 | + continue |
| 160 | + } |
| 161 | + //log.Debug("[tasks-scheduler] %d, %s tick is today %d", id, t, c) |
| 162 | + if realNow.Hour() == tms.Hour() { |
| 163 | + log.Debug("[tasks-scheduler] %d, %s ticker fired", id, t) |
| 164 | + s.TickChan <- now |
| 165 | + tck.Reset(1 * time.Hour) |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + Exit: |
| 172 | + // wait for ticks while the tickers are active. |
| 173 | + // stop the ticker only when stopping the scheduler. |
| 174 | + tck.Stop() |
| 175 | + log.Debug("[tasks-scheduler] scheduler timer %d stopped", id) |
| 176 | + }(drifted) |
| 177 | + } |
| 178 | + wg.Wait() |
| 179 | + |
| 180 | + log.Debug("[tasks-scheduler] SetupDailyTimers() finished") |
| 181 | +} |
0 commit comments