don't fatal is DB is locked. Attempt to fix #60 - but was not able to reproduce
This commit is contained in:
parent
a58df696cf
commit
e7a2f3231e
1 changed files with 10 additions and 1 deletions
|
@ -5,6 +5,8 @@ import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
sqlite3 "github.com/mattn/go-sqlite3"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
@ -15,6 +17,13 @@ func (c *Context) Flush() error {
|
||||||
|
|
||||||
ret := c.tx.Commit()
|
ret := c.tx.Commit()
|
||||||
if ret.Error != nil {
|
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)
|
return fmt.Errorf("failed to commit records : %v", ret.Error)
|
||||||
}
|
}
|
||||||
c.tx = c.Db.Begin()
|
c.tx = c.Db.Begin()
|
||||||
|
@ -37,7 +46,7 @@ func (c *Context) AutoCommit() {
|
||||||
if atomic.LoadInt32(&c.count) != 0 &&
|
if atomic.LoadInt32(&c.count) != 0 &&
|
||||||
(atomic.LoadInt32(&c.count)%100 == 0 || time.Since(c.lastCommit) >= 500*time.Millisecond) {
|
(atomic.LoadInt32(&c.count)%100 == 0 || time.Since(c.lastCommit) >= 500*time.Millisecond) {
|
||||||
if err := c.Flush(); err != nil {
|
if err := c.Flush(); err != nil {
|
||||||
log.Fatalf("failed to flush : %s", err)
|
log.Errorf("failed to flush : %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue