Commit a024d621 authored by Boris Kocherov's avatar Boris Kocherov

fix memory monitor

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