Created
November 13, 2019 13:17
-
-
Save tomaustin700/ccc44fef8c313b629ad0b9dbb8c5be83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"fmt" | |
"time" | |
) | |
const INTERVAL_PERIOD time.Duration = 24 * time.Hour | |
const HOUR_TO_TICK int = 23 | |
const MINUTE_TO_TICK int = 00 | |
const SECOND_TO_TICK int = 03 | |
type jobTicker struct { | |
timer *time.Timer | |
} | |
func runningRoutine() { | |
jobTicker := &jobTicker{} | |
jobTicker.updateTimer() | |
for { | |
<-jobTicker.timer.C | |
fmt.Println(time.Now(), "- just ticked") | |
jobTicker.updateTimer() | |
} | |
} | |
func (t *jobTicker) updateTimer() { | |
nextTick := time.Date(time.Now().Year(), time.Now().Month(), | |
time.Now().Day(), HOUR_TO_TICK, MINUTE_TO_TICK, SECOND_TO_TICK, 0, time.Local) | |
if !nextTick.After(time.Now()) { | |
nextTick = nextTick.Add(INTERVAL_PERIOD) | |
} | |
fmt.Println(nextTick, "- next tick") | |
diff := nextTick.Sub(time.Now()) | |
if t.timer == nil { | |
t.timer = time.NewTimer(diff) | |
} else { | |
t.timer.Reset(diff) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment