diff --git a/go.mod b/go.mod index 95bf7db39..12ca23037 100644 --- a/go.mod +++ b/go.mod @@ -29,6 +29,7 @@ require ( github.com/opencontainers/image-spec v1.0.1 // indirect github.com/oschwald/geoip2-golang v1.4.0 github.com/oschwald/maxminddb-golang v1.6.0 + github.com/pkg/errors v0.8.1 github.com/prometheus/client_golang v1.5.1 github.com/prometheus/client_model v0.2.0 github.com/prometheus/common v0.9.1 diff --git a/pkg/database/commit.go b/pkg/database/commit.go index 179eed01d..a2efbe982 100644 --- a/pkg/database/commit.go +++ b/pkg/database/commit.go @@ -12,8 +12,10 @@ import ( func (c *Context) DeleteExpired() error { //Delete the expired records + now := time.Now() if c.flush { - retx := c.Db.Where(`strftime("%s", until) < strftime("%s", "now")`).Delete(types.BanApplication{}) + //retx := c.Db.Where(`strftime("%s", until) < strftime("%s", "now")`).Delete(types.BanApplication{}) + retx := c.Db.Delete(types.BanApplication{}, "until < ?", now) if retx.RowsAffected > 0 { log.Infof("Flushed %d expired entries from Ban Application", retx.RowsAffected) } @@ -96,8 +98,10 @@ func (c *Context) CleanUpRecordsByCount() error { } sos := []types.BanApplication{} + now := time.Now() /*get soft deleted records oldest to youngest*/ - records := c.Db.Unscoped().Table("ban_applications").Where("deleted_at is not NULL").Where(`strftime("%s", deleted_at) < strftime("%s", "now")`).Find(&sos) + //records := c.Db.Unscoped().Table("ban_applications").Where("deleted_at is not NULL").Where(`strftime("%s", deleted_at) < strftime("%s", "now")`).Find(&sos) + records := c.Db.Unscoped().Table("ban_applications").Where("deleted_at is not NULL").Where("deleted_at < ?", now).Find(&sos) if records.Error != nil { return errors.Wrap(records.Error, "failed to list expired bans for flush") } diff --git a/pkg/database/database.go b/pkg/database/database.go index 1067cb338..8bca7bdc4 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -24,6 +24,9 @@ type Context struct { count int32 lock sync.Mutex //booboo PusherTomb tomb.Tomb + //to manage auto cleanup : max number of records *or* oldest + maxEventRetention int + maxDurationRetention time.Duration } func checkConfig(cfg map[string]string) error { @@ -78,6 +81,19 @@ func NewDatabase(cfg map[string]string) (*Context, error) { } } + if v, ok := cfg["max_records"]; ok { + c.maxEventRetention, err = strconv.Atoi(v) + if err != nil { + log.Errorf("Ignoring invalid max_records '%s' : %s", v, err) + } + } + if v, ok := cfg["max_records_age"]; ok { + c.maxDurationRetention, err = time.ParseDuration(v) + if err != nil { + log.Errorf("Ignoring invalid duration '%s' : %s", v, err) + } + } + if val, ok := cfg["debug"]; ok && val == "true" { log.Infof("Enabling debug for %s", cfg["type"]) c.Db.LogMode(true) @@ -103,9 +119,5 @@ func NewDatabase(cfg map[string]string) (*Context, error) { if c.tx == nil { return nil, fmt.Errorf("failed to begin %s transac : %s", cfg["type"], err) } - c.PusherTomb.Go(func() error { - c.AutoCommit() - return nil - }) return c, nil }