Commit a23689c5 authored by Boris Kocherov's avatar Boris Kocherov

support libreoffice lockfile

parent 78c4ce1b
...@@ -34,8 +34,11 @@ from zope.interface import implements ...@@ -34,8 +34,11 @@ from zope.interface import implements
from application import Application from application import Application
from cloudooo.interfaces.lockable import ILockable from cloudooo.interfaces.lockable import ILockable
from cloudooo.util import logger from cloudooo.util import logger
import signal
from cloudooo.handler.ooo.util import waitStartDaemon, \ from cloudooo.handler.ooo.util import waitStartDaemon, \
removeDirectory removeDirectory, \
processUsedFilesInPath, \
kill_procs_tree
try: try:
import json import json
except ImportError: except ImportError:
...@@ -139,6 +142,18 @@ class OpenOffice(Application): ...@@ -139,6 +142,18 @@ class OpenOffice(Application):
"""Start Instance.""" """Start Instance."""
self.path_user_installation = join(self.path_run_dir, \ self.path_user_installation = join(self.path_run_dir, \
"cloudooo_instance_%s" % self.port) "cloudooo_instance_%s" % self.port)
lock_file = join(self.path_user_installation, '.lock')
if exists(lock_file):
pids = processUsedFilesInPath(self.path_user_installation)
if len(pids) == 0:
logger.debug("Stalled lock file: %s", lock_file)
else:
logger.debug("kill process used workdir: %s", self.path_user_installation)
_, alive = kill_procs_tree(pids, sig=signal.SIGKILL, timeout=self.timeout)
if len(alive) > 0:
logger.error("process blocks worked directory and alive after SIGKILL: %s", alive)
removeDirectory(self.path_user_installation)
if init and exists(self.path_user_installation): if init and exists(self.path_user_installation):
removeDirectory(self.path_user_installation) removeDirectory(self.path_user_installation)
# Create command with all parameters to start the instance # Create command with all parameters to start the instance
......
...@@ -26,10 +26,13 @@ ...@@ -26,10 +26,13 @@
# #
############################################################################## ##############################################################################
import os
from time import sleep from time import sleep
from os import remove from os import remove
from shutil import rmtree from shutil import rmtree
from cloudooo.util import logger from cloudooo.util import logger
from psutil import process_iter, Process, NoSuchProcess, wait_procs
import signal
def removeDirectory(path): def removeDirectory(path):
...@@ -58,6 +61,42 @@ def waitStopDaemon(daemon, attempts=5): ...@@ -58,6 +61,42 @@ def waitStopDaemon(daemon, attempts=5):
sleep(1) sleep(1)
return False return False
def processUsedFilesInPath(path):
pids = set()
for p in process_iter(attrs=['open_files', 'memory_maps']):
for f in (p.info['open_files'] or []) + (p.info['memory_maps'] or []):
if f.path.startswith(path):
pids.add(p.pid)
return pids
def kill_procs_tree(pids, sig=signal.SIGTERM,
timeout=3, on_terminate=None):
pids = set(pids)
children_pids = set(pids)
for pid in pids:
parent = None
try:
parent = Process(pid)
except NoSuchProcess:
pass
if parent:
children = parent.children(recursive=True)
for p in children:
children_pids.add(p.pid)
my_pid = os.getpid()
if my_pid in children_pids:
children_pids.remove(my_pid)
pids = []
for pid in children_pids:
try:
p = Process(pid)
p.send_signal(sig)
pids.append(p)
except NoSuchProcess:
pass
gone, alive = wait_procs(pids, timeout=timeout,
callback=on_terminate)
return (gone, alive)
def remove_file(filepath): def remove_file(filepath):
try: try:
......
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