Commit ea4bd677 authored by Nicolas Delaby's avatar Nicolas Delaby

Add new monitor which allows to stop OpenOffice daemon,

if after configurable amount of time it doesn't serve any requests.
Any new request will wake up daemon smoothly.

To enable it, put in your cloudooo.conf under section [app:main]:

# Duration in seconds
max_sleeping_duration = 300



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@42001 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent c0456bf6
...@@ -39,6 +39,7 @@ from cloudooo.interfaces.handler import IHandler ...@@ -39,6 +39,7 @@ from cloudooo.interfaces.handler import IHandler
from cloudooo.handler.ooo.mimemapper import mimemapper from cloudooo.handler.ooo.mimemapper import mimemapper
from cloudooo.handler.ooo.document import FileSystemDocument from cloudooo.handler.ooo.document import FileSystemDocument
from cloudooo.handler.ooo.monitor.timeout import MonitorTimeout from cloudooo.handler.ooo.monitor.timeout import MonitorTimeout
from cloudooo.handler.ooo.monitor import monitor_sleeping_time
from cloudooo.utils.utils import logger from cloudooo.utils.utils import logger
from psutil import pid_exists from psutil import pid_exists
...@@ -99,6 +100,8 @@ class OOHandler: ...@@ -99,6 +100,8 @@ class OOHandler:
def _subprocess(self, command): def _subprocess(self, command):
"""Run one procedure""" """Run one procedure"""
if monitor_sleeping_time is not None:
monitor_sleeping_time.touch()
try: try:
self._startTimeout() self._startTimeout()
process = Popen(command, process = Popen(command,
......
from request import MonitorRequest from request import MonitorRequest
from memory import MonitorMemory from memory import MonitorMemory
from sleeping_time import MonitorSpleepingTime
from cloudooo.handler.ooo.application.openoffice import openoffice from cloudooo.handler.ooo.application.openoffice import openoffice
monitor_request = None monitor_request = None
monitor_memory = None monitor_memory = None
monitor_sleeping_time = None
def load(local_config): def load(local_config):
"""Start the monitors""" """Start the monitors"""
monitor_interval = int(local_config.get('monitor_interval'))
global monitor_request global monitor_request
monitor_request = MonitorRequest(openoffice, monitor_request = MonitorRequest(openoffice,
int(local_config.get('monitor_interval')), monitor_interval,
int(local_config.get('limit_number_request'))) int(local_config.get('limit_number_request')))
monitor_request.start() monitor_request.start()
if bool(local_config.get('enable_memory_monitor')): if bool(local_config.get('enable_memory_monitor')):
global monitor_memory global monitor_memory
monitor_memory = MonitorMemory(openoffice, monitor_memory = MonitorMemory(openoffice,
int(local_config.get('monitor_interval')), monitor_interval,
int(local_config.get('limit_memory_used'))) int(local_config.get('limit_memory_used')))
monitor_memory.start() monitor_memory.start()
time_before_sleep = int(local_config.get('max_sleeping_duration', 0))
if time_before_sleep:
global monitor_sleeping_time
monitor_sleeping_time = MonitorSpleepingTime(openoffice,
monitor_interval,
time_before_sleep)
monitor_sleeping_time.start()
return return
...@@ -29,10 +38,13 @@ def stop(): ...@@ -29,10 +38,13 @@ def stop():
monitor_request.terminate() monitor_request.terminate()
if monitor_memory: if monitor_memory:
monitor_memory.terminate() monitor_memory.terminate()
if monitor_sleeping_time is not None:
monitor_sleeping_time.terminate()
clear() clear()
def clear(): def clear():
global monitor_request, monitor_memory global monitor_request, monitor_memory, monitor_sleeping_time
monitor_request = None monitor_request = None
monitor_memory = None monitor_memory = None
monitor_sleeping_time = None
##############################################################################
#
# Copyright (c) 2009-2011 Nexedi SA and Contributors. All Rights Reserved.
# Nicolas Delaby <nicolas@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from monitor import Monitor
from threading import Thread
import psutil
from cloudooo.utils.utils import logger
from time import sleep, time
class MonitorSpleepingTime(Monitor, Thread):
"""Usefull to stop daemon if not beeing used after duration of time
"""
def __init__(self, openoffice, interval, sleeping_time):
"""Expects to receive an object that implements the interfaces IApplication
and ILockable, the limit of memory usage that the openoffice can use and
the interval to check the object."""
Monitor.__init__(self, openoffice, interval)
Thread.__init__(self)
self.sleeping_time = sleeping_time
self._touched_at = time()
def start(self):
self.status_flag = True
Thread.start(self)
def touch(self):
"""Restart countdown
"""
logger.debug("Touch MonitorSpleepingTime")
self._touched_at = time()
def run(self):
"""Start monitoring process.
Stop daemon if running and not touch after sleeping duration
"""
logger.debug("Start MonitorSpleepingTime")
while self.status_flag:
current_time = time()
if self.openoffice.status() and\
(self._touched_at + self.sleeping_time) <= current_time:
logger.debug("Stopping OpenOffice after sleeping time of %is" %\
self.sleeping_time)
self.openoffice.acquire()
self.openoffice.stop()
self.openoffice.release()
sleep(self.interval)
logger.debug("Stop MonitorSpleepingTime")
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