Oops, remember to hook up this bit.

Reduce boilerplate in tickloop.
This commit is contained in:
Azareal 2021-03-23 20:21:56 +10:00
parent 20a35fc211
commit 93b6e28cd7
1 changed files with 19 additions and 8 deletions

View File

@ -15,8 +15,8 @@ import (
// TODO: Name the tasks so we can figure out which one it was when something goes wrong? Or maybe toss it up WithStack down there? // TODO: Name the tasks so we can figure out which one it was when something goes wrong? Or maybe toss it up WithStack down there?
func runTasks(tasks []func() error) { func runTasks(tasks []func() error) {
for _, task := range tasks { for _, task := range tasks {
if err := task(); err != nil { if e := task(); e != nil {
c.LogError(err) c.LogError(e)
} }
} }
} }
@ -43,8 +43,8 @@ func startTick() (abort bool) {
} }
func runHook(name string) { func runHook(name string) {
if err := c.RunTaskHook(name); err != nil { if e := c.RunTaskHook(name); e != nil {
c.LogError(err, "Failed at task '"+name+"'") c.LogError(e, "Failed at task '"+name+"'")
} }
} }
@ -67,15 +67,21 @@ func tickLoop(thumbChan chan bool) {
fifteenMinuteTicker := time.NewTicker(15 * time.Minute) fifteenMinuteTicker := time.NewTicker(15 * time.Minute)
hourTicker := time.NewTicker(time.Hour) hourTicker := time.NewTicker(time.Hour)
dailyTicker := time.NewTicker(time.Hour * 24) dailyTicker := time.NewTicker(time.Hour * 24)
tick := func(name string, tasks []func() error) bool {
if startTick() {
return true
}
runHook("before_" + name + "_tick")
runTasks(tasks)
runHook("after_" + name + "_tick")
return false
}
for { for {
select { select {
case <-halfSecondTicker.C: case <-halfSecondTicker.C:
if startTick() { if tick("half_second", c.ScheduledHalfSecondTasks) {
continue continue
} }
runHook("before_half_second_tick")
runTasks(c.ScheduledHalfSecondTasks)
runHook("after_half_second_tick")
case <-secondTicker.C: case <-secondTicker.C:
if startTick() { if startTick() {
continue continue
@ -243,6 +249,11 @@ func dailies() {
} }
} }
err := router.DailyTick()
if err != nil {
c.LogError(err)
}
{ {
err := c.Meta.Set("lastDaily", strconv.FormatInt(time.Now().Unix(), 10)) err := c.Meta.Set("lastDaily", strconv.FormatInt(time.Now().Unix(), 10))
if err != nil { if err != nil {