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