2017-04-10 18:54:12 +02:00
|
|
|
package all
|
|
|
|
|
|
|
|
import (
|
2018-01-07 21:00:56 +01:00
|
|
|
"sync"
|
2017-04-10 18:54:12 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/database"
|
|
|
|
)
|
|
|
|
|
2018-01-03 15:41:40 +01:00
|
|
|
var conn database.Connection
|
2018-01-07 21:00:56 +01:00
|
|
|
var wg = sync.WaitGroup{}
|
2018-01-03 15:41:40 +01:00
|
|
|
var quit chan struct{}
|
2017-04-18 01:48:38 +02:00
|
|
|
|
2018-01-07 21:00:56 +01:00
|
|
|
func Start(config database.Config) (err error) {
|
2018-01-03 15:41:40 +01:00
|
|
|
conn, err = Connect(config.Connection)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2017-04-10 18:54:12 +02:00
|
|
|
}
|
2018-01-03 15:41:40 +01:00
|
|
|
quit = make(chan struct{})
|
2018-01-07 21:00:56 +01:00
|
|
|
wg.Add(1)
|
2018-01-03 15:41:40 +01:00
|
|
|
go deleteWorker(config.DeleteInterval.Duration, config.DeleteAfter.Duration)
|
|
|
|
return
|
2017-04-10 18:54:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-03 15:41:40 +01:00
|
|
|
func Close() {
|
|
|
|
close(quit)
|
2018-01-07 21:00:56 +01:00
|
|
|
wg.Wait()
|
2018-01-03 15:41:40 +01:00
|
|
|
conn.Close()
|
|
|
|
quit = nil
|
2017-04-10 18:54:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-03 15:41:40 +01:00
|
|
|
// prunes node-specific data periodically
|
|
|
|
func deleteWorker(deleteInterval time.Duration, deleteAfter time.Duration) {
|
|
|
|
ticker := time.NewTicker(deleteInterval)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
conn.PruneNodes(deleteAfter)
|
|
|
|
case <-quit:
|
|
|
|
ticker.Stop()
|
2018-01-07 21:00:56 +01:00
|
|
|
wg.Done()
|
2018-01-03 15:41:40 +01:00
|
|
|
return
|
|
|
|
}
|
2017-04-10 18:54:12 +02:00
|
|
|
}
|
|
|
|
}
|