فهرست منبع

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

Thibault bui Koechlin 5 سال پیش
والد
کامیت
e7a2f3231e
1فایلهای تغییر یافته به همراه10 افزوده شده و 1 حذف شده
  1. 10 1
      pkg/sqlite/commit.go

+ 10 - 1
pkg/sqlite/commit.go

@@ -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)
 				}
 				}
 			}
 			}
 		}
 		}