don't fatal is DB is locked. Attempt to fix #60 - but was not able to reproduce

This commit is contained in:
Thibault bui Koechlin 2020-06-03 09:46:08 +02:00
parent a58df696cf
commit e7a2f3231e

View file

@ -5,6 +5,8 @@ import (
"sync/atomic"
"time"
sqlite3 "github.com/mattn/go-sqlite3"
"github.com/crowdsecurity/crowdsec/pkg/types"
log "github.com/sirupsen/logrus"
)
@ -15,6 +17,13 @@ func (c *Context) Flush() error {
ret := c.tx.Commit()
if ret.Error != nil {
/*if the database is locked, don't overwrite the current transaction*/
if ret.Error == sqlite3.ErrLocked {
log.Errorf("sqlite commit : db is locked : %s", ret.Error)
return ret.Error
}
/*if it's another error, create a new transaction to avoid locking ourselves in a bad state ?*/
c.tx = c.Db.Begin()
return fmt.Errorf("failed to commit records : %v", ret.Error)
}
c.tx = c.Db.Begin()
@ -37,7 +46,7 @@ func (c *Context) AutoCommit() {
if atomic.LoadInt32(&c.count) != 0 &&
(atomic.LoadInt32(&c.count)%100 == 0 || time.Since(c.lastCommit) >= 500*time.Millisecond) {
if err := c.Flush(); err != nil {
log.Fatalf("failed to flush : %s", err)
log.Errorf("failed to flush : %s", err)
}
}
}