yanic/output/all/internal.go

46 lines
766 B
Go
Raw Normal View History

package all
import (
2018-01-03 15:41:40 +01:00
"sync"
"time"
"github.com/FreifunkBremen/yanic/output"
"github.com/FreifunkBremen/yanic/runtime"
)
2018-01-03 15:41:40 +01:00
var quit chan struct{}
var wg = sync.WaitGroup{}
var outputA output.Output
2018-01-03 15:41:40 +01:00
func Start(nodes *runtime.Nodes, config runtime.NodesConfig) (err error) {
outputA, err = Register(config.Output)
if err != nil {
return
}
2018-01-03 15:41:40 +01:00
quit = make(chan struct{})
wg.Add(1)
go saveWorker(nodes, config.SaveInterval.Duration)
return
}
2018-01-03 15:41:40 +01:00
func Close() {
close(quit)
wg.Wait()
quit = nil
}
2018-01-03 15:41:40 +01:00
// save periodically to output
func saveWorker(nodes *runtime.Nodes, saveInterval time.Duration) {
ticker := time.NewTicker(saveInterval)
for {
select {
case <-ticker.C:
outputA.Save(nodes)
case <-quit:
ticker.Stop()
wg.Done()
return
}
}
}