Commit cedd433c authored by Boris Kocherov's avatar Boris Kocherov

improve logging

parent a5157949
...@@ -29,8 +29,7 @@ ...@@ -29,8 +29,7 @@
from zope.interface import implements from zope.interface import implements
from cloudooo.interfaces.application import IApplication from cloudooo.interfaces.application import IApplication
from cloudooo.util import logger from cloudooo.util import logger
from cloudooo.handler.ooo.util import waitStopDaemon from psutil import pid_exists, Process, AccessDenied, TimeoutExpired, NoSuchProcess
from psutil import pid_exists, Process, AccessDenied
class Application(object): class Application(object):
...@@ -47,17 +46,36 @@ class Application(object): ...@@ -47,17 +46,36 @@ class Application(object):
self.getAddress()[-1], self.getAddress()[-1],
self.pid())) self.pid()))
def stop(self): def stopProcess(self, process_pid):
"""Stop the process""" """Stop the process"""
if hasattr(self, 'process') and self.status(): error = False
process_pid = self.process.pid logger.debug("Stop Pid - %s", process_pid)
logger.debug("Stop Pid - %s" % process_pid) returncode = None
try:
process = Process(process_pid)
cmdline = " ".join(process.cmdline())
process.terminate()
returncode = process.wait(self.timeout)
except NoSuchProcess:
pass
except TimeoutExpired:
error = True
logger.error("Process %s survived SIGTERM after %s", process_pid, self.timeout)
try: try:
self.process.terminate() process.kill()
waitStopDaemon(self, self.timeout) returncode = process.wait(self.timeout)
finally: except NoSuchProcess:
if pid_exists(process_pid) or self.status(): pass
Process(process_pid).kill() except TimeoutExpired:
logger.error("Process %s survived SIGKILL after %s", process_pid, self.timeout)
if error and returncode:
logger.error("Process %s cmdline: %s ended with returncode %s", process_pid, cmdline, returncode)
elif returncode != 0:
logger.debug("Process %s ended with returncode %s", process_pid, returncode)
def stop(self):
if hasattr(self, 'process'):
self.stopProcess(self.process.pid)
delattr(self, "process") delattr(self, "process")
def loadSettings(self, hostname, port, path_run_dir, **kwargs): def loadSettings(self, hostname, port, path_run_dir, **kwargs):
......
...@@ -37,7 +37,7 @@ from application import Application ...@@ -37,7 +37,7 @@ from application import Application
from cloudooo.interfaces.lockable import ILockable from cloudooo.interfaces.lockable import ILockable
from cloudooo.util import logger, convertStringToBool from cloudooo.util import logger, convertStringToBool
from cloudooo.handler.ooo.util import waitStartDaemon, \ from cloudooo.handler.ooo.util import waitStartDaemon, \
removeDirectory, waitStopDaemon, \ removeDirectory, \
socketStatus socketStatus
...@@ -105,7 +105,6 @@ class OpenOffice(Application): ...@@ -105,7 +105,6 @@ class OpenOffice(Application):
"""Start OpenOffice.org process""" """Start OpenOffice.org process"""
for i in range(5): for i in range(5):
self.stop() self.stop()
waitStopDaemon(self, self.timeout)
self.process = Popen(command, self.process = Popen(command,
close_fds=True, close_fds=True,
env=env) env=env)
...@@ -121,14 +120,16 @@ class OpenOffice(Application): ...@@ -121,14 +120,16 @@ class OpenOffice(Application):
for connection in process.connections(): for connection in process.connections():
if connection.status == "LISTEN" and \ if connection.status == "LISTEN" and \
connection.laddr[1] == self.port: connection.laddr[1] == self.port:
process.terminate() self.stopProcess(process.pid)
break
except AccessDenied, e: except AccessDenied, e:
pass pass
except TypeError, e: except TypeError, e:
# exception to prevent one psutil issue with zombie processes # exception to prevent one psutil issue with zombie processes
logger.debug(e) logger.error(e)
except NotImplementedError, e: except NotImplementedError, e:
logger.error("lsof isn't installed on this machine: " + str(e)) logger.error("lsof isn't installed on this machine: %s", str(e))
break
def start(self, init=True): def start(self, init=True):
"""Start Instance.""" """Start Instance."""
......
...@@ -119,11 +119,18 @@ class Handler(object): ...@@ -119,11 +119,18 @@ class Handler(object):
self._startTimeout() self._startTimeout()
process = Popen(command_list, stdout=PIPE, stderr=PIPE, close_fds=True, process = Popen(command_list, stdout=PIPE, stderr=PIPE, close_fds=True,
env=openoffice.environment_dict.copy()) env=openoffice.environment_dict.copy())
logger.debug("Process %s running", process.pid)
stdout, stderr = process.communicate() stdout, stderr = process.communicate()
finally: finally:
self._stopTimeout() self._stopTimeout()
if pid_exists(process.pid): if pid_exists(process.pid):
logger.debug("Process %s terminated", process.pid)
process.terminate() process.terminate()
if (process.returncode < 0 and process.returncode != -6) or stderr:
logger.error("Process %s command:%s", process.pid, " ".join(command_list))
logger.error("Process %s stdout:%s", process.pid, stdout)
logger.error("Process %s stderr:%s", process.pid, stderr)
logger.debug("Process %s terminated with returncode %s", process.pid, process.returncode)
return stdout, stderr return stdout, stderr
def _callUnoConverter(self, *feature_list, **kw): def _callUnoConverter(self, *feature_list, **kw):
...@@ -131,7 +138,9 @@ class Handler(object): ...@@ -131,7 +138,9 @@ class Handler(object):
if not openoffice.status(): if not openoffice.status():
openoffice.start() openoffice.start()
command_list = self._getCommand(*feature_list, **kw) command_list = self._getCommand(*feature_list, **kw)
logger.debug("run convert first")
stdout, stderr = self._subprocess(command_list) stdout, stderr = self._subprocess(command_list)
logger.debug("stop convert first")
if not stdout and stderr: if not stdout and stderr:
first_error = stderr first_error = stderr
logger.error(stderr) logger.error(stderr)
...@@ -139,10 +148,11 @@ class Handler(object): ...@@ -139,10 +148,11 @@ class Handler(object):
openoffice.restart() openoffice.restart()
kw['document_url'] = self.document.getUrl() kw['document_url'] = self.document.getUrl()
command = self._getCommand(*feature_list, **kw) command = self._getCommand(*feature_list, **kw)
logger.debug("run convert second")
stdout, stderr = self._subprocess(command) stdout, stderr = self._subprocess(command)
logger.debug("stop convert second")
if not stdout and stderr: if not stdout and stderr:
second_error = "\nerror of the second run: " + stderr second_error = "\nerror of the second run: " + stderr
logger.error(second_error)
raise Exception(first_error + second_error) raise Exception(first_error + second_error)
return stdout, stderr return stdout, stderr
......
...@@ -33,6 +33,7 @@ from subprocess import STDOUT ...@@ -33,6 +33,7 @@ from subprocess import STDOUT
from zope.interface import implements from zope.interface import implements
from filter import Filter from filter import Filter
from os import environ, path from os import environ, path
from cloudooo.util import logger
from cloudooo.interfaces.mimemapper import IMimemapper from cloudooo.interfaces.mimemapper import IMimemapper
from types import InstanceType from types import InstanceType
import json import json
...@@ -124,8 +125,10 @@ class MimeMapper(object): ...@@ -124,8 +125,10 @@ class MimeMapper(object):
"--hostname=%s" % hostname, "--hostname=%s" % hostname,
"--port=%s" % port] "--port=%s" % port]
process = Popen(command, stdout=PIPE, stderr=STDOUT, close_fds=True) process = Popen(command, stdout=PIPE, stderr=PIPE, close_fds=True)
stdout, stderr = process.communicate() stdout, stderr = process.communicate()
if stderr:
logger.error(stderr)
if process.returncode: if process.returncode:
raise ValueError(stdout) raise ValueError(stdout)
filter_dict, type_dict = json.loads(stdout) filter_dict, type_dict = json.loads(stdout)
......
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