80th time's the charm.

This commit is contained in:
Azareal 2021-07-11 09:10:19 +10:00
parent 7c5d55e0b9
commit 0cffbbb3cd
2 changed files with 27 additions and 5 deletions

View File

@ -75,7 +75,10 @@ func DBTimeout() time.Duration {
return -1 return -1
} }
var pint int64
func StartTick() (abort bool) { func StartTick() (abort bool) {
opint := pint
db := qgen.Builder.GetConn() db := qgen.Builder.GetConn()
isDBDown := atomic.LoadInt32(&IsDBDown) isDBDown := atomic.LoadInt32(&IsDBDown)
if e := db.Ping(); e != nil { if e := db.Ping(); e != nil {
@ -92,7 +95,7 @@ func StartTick() (abort bool) {
} }
db.SetConnMaxLifetime(DBTimeout()) db.SetConnMaxLifetime(DBTimeout())
atomic.StoreInt32(&IsDBDown, 0) atomic.StoreInt32(&IsDBDown, 0)
return false return opint != pint
} }
// TODO: Move these into DailyTick() methods? // TODO: Move these into DailyTick() methods?
@ -234,9 +237,10 @@ type TickWatch struct {
Tasks int64 Tasks int64
EndHook int64 EndHook int64
Ticker *time.Ticker Ticker *time.Ticker
Deadline *time.Ticker Deadline *time.Ticker
EndChan chan bool EndChan chan bool
OutEndChan chan bool
} }
func NewTickWatch() *TickWatch { func NewTickWatch() *TickWatch {
@ -297,6 +301,7 @@ func (w *TickWatch) DumpElapsed() {
func (w *TickWatch) Run() { func (w *TickWatch) Run() {
w.EndChan = make(chan bool) w.EndChan = make(chan bool)
// Use a goroutine to circumvent ticks which never end // Use a goroutine to circumvent ticks which never end
// TODO: Reuse goroutines across multiple *TickWatch?
go func() { go func() {
defer w.Ticker.Stop() defer w.Ticker.Stop()
defer close(w.EndChan) defer close(w.EndChan)
@ -333,6 +338,10 @@ func (w *TickWatch) Run() {
Log("tick " + w.Name + " completed in " + dur.String()) Log("tick " + w.Name + " completed in " + dur.String())
w.DumpElapsed() w.DumpElapsed()
} }
if w.OutEndChan != nil {
w.OutEndChan <- true
close(w.OutEndChan)
}
return return
} }
} }

View File

@ -59,13 +59,25 @@ func tickLoop(thumbChan chan bool) error {
return e return e
} }
startTick := func(ch chan bool) (ret bool) {
if c.Dev.HourDBTimeout {
go func() {
defer c.EatPanics()
ch <- c.StartTick()
}()
return <-ch
}
return c.StartTick()
}
tick := func(name string, tasks c.TaskSet, secs int) error { tick := func(name string, tasks c.TaskSet, secs int) error {
tw := c.NewTickWatch() tw := c.NewTickWatch()
tw.Name = name tw.Name = name
tw.Set(&tw.Start, uutils.Nanotime()) tw.Set(&tw.Start, uutils.Nanotime())
tw.Run() tw.Run()
defer tw.Stop() defer tw.Stop()
if c.StartTick() { ch := make(chan bool)
tw.OutEndChan = ch
if startTick(ch) {
return nil return nil
} }
tw.Set(&tw.DBCheck, uutils.Nanotime()) tw.Set(&tw.DBCheck, uutils.Nanotime())
@ -83,6 +95,7 @@ func tickLoop(thumbChan chan bool) error {
return e return e
} }
tw.Set(&tw.EndHook, uutils.Nanotime()) tw.Set(&tw.EndHook, uutils.Nanotime())
//close(tw.OutEndChan)
return nil return nil
} }