adapt code to MR #91

This commit is contained in:
erenJag 2020-07-01 15:40:47 +02:00
parent 07ca331df5
commit a9f5110ff9
3 changed files with 23 additions and 6 deletions

1
go.mod
View file

@ -29,6 +29,7 @@ require (
github.com/opencontainers/image-spec v1.0.1 // indirect github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/oschwald/geoip2-golang v1.4.0 github.com/oschwald/geoip2-golang v1.4.0
github.com/oschwald/maxminddb-golang v1.6.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_golang v1.5.1
github.com/prometheus/client_model v0.2.0 github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.9.1 github.com/prometheus/common v0.9.1

View file

@ -12,8 +12,10 @@ import (
func (c *Context) DeleteExpired() error { func (c *Context) DeleteExpired() error {
//Delete the expired records //Delete the expired records
now := time.Now()
if c.flush { 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 { if retx.RowsAffected > 0 {
log.Infof("Flushed %d expired entries from Ban Application", retx.RowsAffected) log.Infof("Flushed %d expired entries from Ban Application", retx.RowsAffected)
} }
@ -96,8 +98,10 @@ func (c *Context) CleanUpRecordsByCount() error {
} }
sos := []types.BanApplication{} sos := []types.BanApplication{}
now := time.Now()
/*get soft deleted records oldest to youngest*/ /*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 { if records.Error != nil {
return errors.Wrap(records.Error, "failed to list expired bans for flush") return errors.Wrap(records.Error, "failed to list expired bans for flush")
} }

View file

@ -24,6 +24,9 @@ type Context struct {
count int32 count int32
lock sync.Mutex //booboo lock sync.Mutex //booboo
PusherTomb tomb.Tomb 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 { 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" { if val, ok := cfg["debug"]; ok && val == "true" {
log.Infof("Enabling debug for %s", cfg["type"]) log.Infof("Enabling debug for %s", cfg["type"])
c.Db.LogMode(true) c.Db.LogMode(true)
@ -103,9 +119,5 @@ func NewDatabase(cfg map[string]string) (*Context, error) {
if c.tx == nil { if c.tx == nil {
return nil, fmt.Errorf("failed to begin %s transac : %s", cfg["type"], err) 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 return c, nil
} }