Commit 985e5810 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos/collect: Preserve entries at the database for 15 days

  This may case more memory usage and more 'live' data to handle,
  for this reason, I'm making it configurable
parent c9f4ac31
...@@ -90,6 +90,9 @@ def do_collect(conf): ...@@ -90,6 +90,9 @@ def do_collect(conf):
except (KeyboardInterrupt, SystemExit, NoSuchProcess): except (KeyboardInterrupt, SystemExit, NoSuchProcess):
raise raise
days_to_preserve = conf.get("slapos", "collect_cache")
if days_to_preserve is None:
days_to_preserve = 15
log_directory = "%s/var/data-log" % conf.get("slapos", "instance_root") log_directory = "%s/var/data-log" % conf.get("slapos", "instance_root")
mkdir_p(log_directory, 0o755) mkdir_p(log_directory, 0o755)
...@@ -160,7 +163,7 @@ def do_collect(conf): ...@@ -160,7 +163,7 @@ def do_collect(conf):
compressLogFolder(log_directory) compressLogFolder(log_directory)
# Drop older entries already reported # Drop older entries already reported
database.garbageCollect() database.garbageCollect(int(days_to_preserve))
except AccessDenied: except AccessDenied:
print("You HAVE TO execute this script with root permission.") print("You HAVE TO execute this script with root permission.")
......
...@@ -259,7 +259,7 @@ class Database: ...@@ -259,7 +259,7 @@ class Database:
return [i[0] for i in self._execute( return [i[0] for i in self._execute(
"SELECT name FROM sqlite_master WHERE type='table'")] "SELECT name FROM sqlite_master WHERE type='table'")]
def _getGarbageCollectionDateList(self, days_to_preserve=3): def _getGarbageCollectionDateList(self, days_to_preserve):
""" Return the list of dates to Preserve when data collect """ Return the list of dates to Preserve when data collect
""" """
base = datetime.datetime.today() base = datetime.datetime.today()
...@@ -268,11 +268,11 @@ class Database: ...@@ -268,11 +268,11 @@ class Database:
date_list.append((base - datetime.timedelta(days=x)).strftime("%Y-%m-%d")) date_list.append((base - datetime.timedelta(days=x)).strftime("%Y-%m-%d"))
return date_list return date_list
def garbageCollect(self): def garbageCollect(self, days_to_preserve=3):
""" Garbase collect the database, by removing older records already """ Garbase collect the database, by removing older records already
reported. reported.
""" """
date_list = self._getGarbageCollectionDateList() date_list = self._getGarbageCollectionDateList(days_to_preserve)
where_clause = "reported = 1" where_clause = "reported = 1"
for _date in date_list: for _date in date_list:
where_clause += " AND date != '%s' " % _date where_clause += " AND date != '%s' " % _date
......
...@@ -214,7 +214,7 @@ class TestCollectDatabase(unittest.TestCase): ...@@ -214,7 +214,7 @@ class TestCollectDatabase(unittest.TestCase):
def test_garbage_collection_date_list(self): def test_garbage_collection_date_list(self):
database = db.Database(self.instance_root) database = db.Database(self.instance_root)
self.assertEqual(len(database._getGarbageCollectionDateList()), 3) self.assertEqual(len(database._getGarbageCollectionDateList(3)), 3)
self.assertEqual(len(database._getGarbageCollectionDateList(1)), 1) self.assertEqual(len(database._getGarbageCollectionDateList(1)), 1)
self.assertEqual(len(database._getGarbageCollectionDateList(0)), 0) self.assertEqual(len(database._getGarbageCollectionDateList(0)), 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment