2020-07-16 14:05:03 +00:00
|
|
|
package database
|
2020-05-15 09:39:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-07-27 11:42:30 +00:00
|
|
|
/*Flush doesn't do anything here : we are not using transactions or such, nothing to "flush" per se*/
|
2020-05-15 09:39:16 +00:00
|
|
|
func (c *Context) Flush() error {
|
2020-07-01 15:04:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) StartAutoCommit() error {
|
|
|
|
//TBD : we shouldn't start auto-commit if we are in cli mode ?
|
|
|
|
c.PusherTomb.Go(func() error {
|
|
|
|
c.autoCommit()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) autoCommit() {
|
|
|
|
log.Debugf("starting autocommit")
|
|
|
|
cleanUpTicker := time.NewTicker(1 * time.Minute)
|
|
|
|
expireTicker := time.NewTicker(1 * time.Second)
|
|
|
|
if !c.flush {
|
|
|
|
log.Debugf("flush is disabled")
|
|
|
|
}
|
2020-05-15 09:39:16 +00:00
|
|
|
for {
|
|
|
|
select {
|
2020-06-19 11:57:44 +00:00
|
|
|
case <-c.PusherTomb.Dying():
|
|
|
|
//we need to shutdown
|
2020-07-16 14:05:03 +00:00
|
|
|
log.Infof("database routine shutdown")
|
2020-06-19 11:57:44 +00:00
|
|
|
if err := c.Flush(); err != nil {
|
|
|
|
log.Errorf("error while flushing records: %s", err)
|
|
|
|
}
|
|
|
|
if err := c.Db.Close(); err != nil {
|
|
|
|
log.Errorf("error while closing db : %s", err)
|
|
|
|
}
|
|
|
|
return
|
2020-07-01 15:04:29 +00:00
|
|
|
case <-expireTicker.C:
|
2020-07-30 13:58:06 +00:00
|
|
|
if _, err := c.DeleteExpired(); err != nil {
|
2020-07-01 15:04:29 +00:00
|
|
|
log.Errorf("Error while deleting expired records: %s", err)
|
|
|
|
}
|
|
|
|
case <-cleanUpTicker.C:
|
2020-07-30 13:58:06 +00:00
|
|
|
if _, err := c.CleanUpRecordsByCount(); err != nil {
|
2020-07-01 15:04:29 +00:00
|
|
|
log.Errorf("error in max records cleanup : %s", err)
|
|
|
|
}
|
2020-07-30 13:58:06 +00:00
|
|
|
if _, err := c.CleanUpRecordsByAge(); err != nil {
|
2020-07-01 15:04:29 +00:00
|
|
|
log.Errorf("error in old records cleanup : %s", err)
|
|
|
|
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|