Fix scheduler by adding a wait group

This commit is contained in:
max 2023-04-06 09:55:56 -05:00
parent 8e4c5e3268
commit 53a780343f

View File

@ -1,6 +1,9 @@
package app package app
import "time" import (
"sync"
"time"
)
type Scheduled struct { type Scheduled struct {
EveryReboot []func(app *App) EveryReboot []func(app *App)
@ -35,11 +38,14 @@ func RunScheduledTasks(app *App, poolSize int, stop <-chan struct{}) {
} }
// Set up task runners // Set up task runners
var wg sync.WaitGroup
runners := make([]chan bool, len(tasks)) runners := make([]chan bool, len(tasks))
for i, task := range tasks { for i, task := range tasks {
runner := make(chan bool, poolSize) runner := make(chan bool, poolSize)
runners[i] = runner runners[i] = runner
wg.Add(1)
go func(task Task, runner chan bool) { go func(task Task, runner chan bool) {
defer wg.Done()
ticker := time.NewTicker(task.Interval) ticker := time.NewTicker(task.Interval)
defer ticker.Stop() defer ticker.Stop()
for { for {
@ -59,11 +65,11 @@ func RunScheduledTasks(app *App, poolSize int, stop <-chan struct{}) {
}(task, runner) }(task, runner)
} }
// Wait for termination // Wait for all goroutines to finish
wg.Wait()
// Close channels
for _, runner := range runners { for _, runner := range runners {
for i := 0; i < cap(runner); i++ {
runner <- false
}
close(runner) close(runner)
} }
} }