Commit 0ec77252 authored by Boris Kocherov's avatar Boris Kocherov

fix memory monitor

parent e4ba9a63
......@@ -46,8 +46,11 @@ class Application(object):
self.getAddress()[-1],
self.pid()))
def stop(self):
def stop(self, pid=None):
if hasattr(self, 'process'):
if pid is not None and self.process.pid != pid:
# pid already stopped
return False
error = False
process_pid = self.process.pid
returncode = self.process.poll()
......@@ -85,6 +88,7 @@ class Application(object):
self.daemonOutputLog()
logger.debug("Process %s ended with returncode %s", process_pid, returncode)
delattr(self, "process")
return True
def daemonOutputLog(self):
if hasattr(self, 'process'):
......
......@@ -164,11 +164,11 @@ class OpenOffice(Application):
self._cleanRequest()
Application.start(self)
def stop(self):
def stop(self, pid=None):
"""Stop the instance by pid. By the default
the signal is 15."""
Application.stop(self)
self._cleanRequest()
if Application.stop(self, pid=pid):
self._cleanRequest()
def isLocked(self):
"""Verify if OOo instance is being used."""
......
......@@ -44,15 +44,18 @@ class MonitorMemory(Monitor, Process):
Process.__init__(self)
self.limit = limit_memory_usage
def create_process(self):
self.process = psutil.Process(int(self.openoffice.pid()))
def create_process(self, pid):
if not hasattr(self, 'process') or \
self.process.pid != int(pid):
self.process = psutil.Process(pid)
return self.process
def get_memory_usage(self):
def get_memory_usage(self, pid):
if pid is None:
return 0
try:
if not hasattr(self, 'process') or \
self.process.pid != int(self.openoffice.pid()):
self.create_process()
return self.process.memory_info().rss / (1024 * 1024)
process = self.create_process(pid)
return process.memory_info().rss / (1024 * 1024)
except TypeError:
logger.debug("OpenOffice is stopped")
return 0
......@@ -69,9 +72,10 @@ class MonitorMemory(Monitor, Process):
self.status_flag = True
logger.debug("Start MonitorMemory")
while self.status_flag:
if self.get_memory_usage() > self.limit:
logger.debug("Stopping OpenOffice")
self.openoffice.stop()
pid = self.openoffice.pid()
if self.get_memory_usage(pid) > self.limit:
logger.debug("Stopping OpenOffice on memory limit increase")
self.openoffice.stop(pid=pid)
sleep(self.interval)
logger.debug("Stop MonitorMemory")
......
......@@ -50,7 +50,9 @@ class MonitorTimeout(Monitor, Process):
sleep(self.interval)
if self.openoffice.isLocked():
logger.debug("Stop OpenOffice - Port %s - Pid %s" % (port, pid))
self.openoffice.stop()
# using pid is not necessary here
# but safe if incorrect use
self.openoffice.stop(pid=pid)
def terminate(self):
"""Stop the process"""
......
......@@ -90,7 +90,7 @@ class TestMonitorMemory(unittest.TestCase):
def testCreateProcess(self):
"""Test if the psutil.Process is create correctly"""
self.monitor = MonitorMemory(openoffice, 2, 400)
self.monitor.create_process()
self.monitor.create_process(openoffice.pid())
self.assertTrue(hasattr(self.monitor, 'process'))
self.assertEquals(type(self.monitor.process), Process)
......@@ -98,11 +98,11 @@ class TestMonitorMemory(unittest.TestCase):
"""Test memory usage"""
self.monitor = MonitorMemory(openoffice, 2, 400)
openoffice.stop()
memory_usage_int = self.monitor.get_memory_usage()
memory_usage_int = self.monitor.get_memory_usage(openoffice.pid())
self.assertEquals(memory_usage_int, 0)
if not openoffice.status():
openoffice.start()
memory_usage_int = self.monitor.get_memory_usage()
memory_usage_int = self.monitor.get_memory_usage(openoffice.pid())
self.assertEquals(type(memory_usage_int), IntType)
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