crowdsec/pkg/database/commit.go

57 lines
1.3 KiB
Go
Raw Normal View History

package database
2020-05-15 09:39:16 +00:00
import (
"time"
log "github.com/sirupsen/logrus"
)
/*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 {
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 {
case <-c.PusherTomb.Dying():
//we need to shutdown
log.Infof("database routine shutdown")
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
case <-expireTicker.C:
2020-07-30 13:58:06 +00:00
if _, err := c.DeleteExpired(); err != nil {
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 {
log.Errorf("error in max records cleanup : %s", err)
}
2020-07-30 13:58:06 +00:00
if _, err := c.CleanUpRecordsByAge(); err != nil {
log.Errorf("error in old records cleanup : %s", err)
2020-05-15 09:39:16 +00:00
}
}
}
}