Commit 58eface4 authored by Alain Takoudjou's avatar Alain Takoudjou

fixup: move check_free_disk_space promise to promise.plugin

parent 2b5d1afa
...@@ -12,6 +12,33 @@ import psutil ...@@ -12,6 +12,33 @@ import psutil
from slapos.collect.db import Database from slapos.collect.db import Database
def getFreeSpace(disk_partition, database, date, time, logger=None):
database = Database(database, create=False, timeout=10)
try:
# fetch free disk space
database.connect()
where_query = "time between '%s:00' and '%s:30' and partition='%s'" % (time, time, disk_partition)
query_result = database.select("disk", date, "free", where=where_query)
result = zip(*query_result)
if not result or not result[0][0]:
if logger is not None:
logger.info("No result from collector database: disk check skipped")
return 0
disk_free = result[0][0]
except sqlite3.OperationalError, e:
# if database is still locked after timeout expiration (another process is using it)
# we print warning message and try the promise at next run until max warn msg
locked_message = "database is locked"
if locked_message in str(e) and \
not self.raiseOnDatabaseLocked(locked_message):
return 0
raise
finally:
database.close()
pass
return int(disk_free)
class RunPromise(GenericPromise): class RunPromise(GenericPromise):
zope_interface.implements(interface.IPromise) zope_interface.implements(interface.IPromise)
...@@ -48,35 +75,6 @@ class RunPromise(GenericPromise): ...@@ -48,35 +75,6 @@ class RunPromise(GenericPromise):
self.logger.warn("collector database is locked by another process: %s %s" % (warning_count, len(latest_result_list))) self.logger.warn("collector database is locked by another process: %s %s" % (warning_count, len(latest_result_list)))
return False return False
def functio(self):
raise sqlite3.OperationalError("database is locked")
def getFreeSpace(self, disk_partition, database, date, time):
database = Database(database, create=False, timeout=10)
try:
# fetch free disk space
database.connect()
where_query = "time between '%s:00' and '%s:30' and partition='%s'" % (time, time, disk_partition)
query_result = database.select("disk", date, "free", where=where_query)
result = zip(*query_result)
if not result or not result[0][0]:
self.logger.info("No result from collector database: disk check skipped")
return 0
disk_free = result[0][0]
except sqlite3.OperationalError, e:
# if database is still locked after timeout expiration (another process is using it)
# we print warning message and try the promise at next run until max warn msg
locked_message = "database is locked"
if locked_message in str(e) and \
not self.raiseOnDatabaseLocked(locked_message):
return 0
raise
finally:
database.close()
pass
return int(disk_free)
def getInodeUsage(self, path): def getInodeUsage(self, path):
max_inode_usage = 97.99 # < 98% usage max_inode_usage = 97.99 # < 98% usage
...@@ -151,7 +149,8 @@ class RunPromise(GenericPromise): ...@@ -151,7 +149,8 @@ class RunPromise(GenericPromise):
if db_path.endswith("collector.db"): if db_path.endswith("collector.db"):
db_path=db_path[:-len("collector.db")] db_path=db_path[:-len("collector.db")]
free_space = self.getFreeSpace(disk_partition, db_path, currentdate, currenttime) free_space = getFreeSpace(disk_partition, db_path, currentdate,
currenttime, self.logger)
if free_space == 0: if free_space == 0:
return return
elif free_space > min_free_size: elif free_space > min_free_size:
......
...@@ -30,7 +30,7 @@ from slapos.grid.promise import PromiseError ...@@ -30,7 +30,7 @@ from slapos.grid.promise import PromiseError
import os import os
import sqlite3 import sqlite3
from slapos.test.promise import data from slapos.test.promise import data
from slapos.promise.check_free_disk import getFreeSpace from slapos.promise.plugin.check_free_disk_space import getFreeSpace
from slapos.grid.promise import PromiseError from slapos.grid.promise import PromiseError
class TestCheckFreeDiskSpace(TestPromisePluginMixin): class TestCheckFreeDiskSpace(TestPromisePluginMixin):
......
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