"""
return self.reporter.config
+ # Global write lock for the database
+ # Since only one members can write to the database at the same time, we will
+ # have to serialize those writes in order to avoid any OperationalErrors from
+ # the sqlite3 driver, if an operation is waiting too long for a lock.
+ _write = threading.Lock()
+
def _open_database(self):
"""
Opens the database
t = (now - retention_days).timestamp()
# Remove everything
- self.db.execute(
- "DELETE FROM alerts WHERE timestamp <= ?", (t,),
- )
+ with self._write:
+ self.db.execute(
+ "DELETE FROM alerts WHERE timestamp <= ?", (t,),
+ )
# Release the lock
finally:
"""
log.debug("Optimizing the database")
- for i in range(5):
+ with self._write:
self.db.execute("PRAGMA optimize")
-
- try:
- self.db.execute("PRAGMA wal_checkpoint = TRUNCATE")
-
- # Try again if this has failed (probably because of the database being locked)
- except sqlite3.OperationalError:
- continue
-
- # Terminate the loop if the operation was successful
- else:
- break
+ self.db.execute("PRAGMA wal_checkpoint = TRUNCATE")
def process(self, event):
"""
log.debug("Received alert: %s" % event)
# Write the event to the database
- self.db.execute("INSERT INTO alerts(timestamp, event) VALUES(?, ?)",
- (event.timestamp.timestamp(), event.json))
- self.db.commit()
+ with self._write:
+ self.db.execute("INSERT INTO alerts(timestamp, event) VALUES(?, ?)",
+ (event.timestamp.timestamp(), event.json))
+ self.db.commit()
# Send to syslog
if self.config.getboolean("syslog", "enabled", fallback=False):