2017-05-20 14:46:29 +02:00
|
|
|
package all
|
|
|
|
|
|
|
|
import (
|
2018-01-03 15:41:40 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-05-20 14:46:29 +02:00
|
|
|
|
|
|
|
"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
|
2017-05-20 14:46:29 +02:00
|
|
|
|
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
|
2017-05-20 14:46:29 +02:00
|
|
|
}
|
2018-01-03 15:41:40 +01:00
|
|
|
quit = make(chan struct{})
|
|
|
|
wg.Add(1)
|
|
|
|
go saveWorker(nodes, config.SaveInterval.Duration)
|
|
|
|
return
|
2017-05-20 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
2018-01-03 15:41:40 +01:00
|
|
|
func Close() {
|
|
|
|
close(quit)
|
|
|
|
wg.Wait()
|
|
|
|
quit = nil
|
|
|
|
}
|
2017-05-20 14:46:29 +02:00
|
|
|
|
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
|
|
|
|
}
|
2017-05-20 14:46:29 +02:00
|
|
|
}
|
|
|
|
}
|