Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --stop-after parameter to unoserver #144

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ Unoserver

unoserver [-h] [-v] [--interface INTERFACE] [--uno-interface UNO_INTERFACE] [--port PORT] [--uno-port UNO_PORT]
[--daemon] [--executable EXECUTABLE] [--user-installation USER_INSTALLATION]
[--libreoffice-pid-file LIBREOFFICE_PID_FILE]
[--libreoffice-pid-file LIBREOFFICE_PID_FILE] [--conversion-timeout CONVERSION_TIMEOUT]
[--stop-after STOP_AFTER]

* `--interface`: The interface used by the XMLRPC server, defaults to "127.0.0.1"
* `--port`: The port used by the XMLRPC server, defaults to "2003"
Expand All @@ -110,6 +111,7 @@ Unoserver
* `--libreoffice-pid-file`: If set, unoserver will write the Libreoffice PID to this file.
If started in daemon mode, the file will not be deleted when unoserver exits.
* `--conversion-timeout`: Terminate Libreoffice and exit if a conversion does not complete in the given time (in seconds).
* `--stop-after`: Terminate Libreoffice and exit after the given number of requests.
* `-v, --version`: Display version and exit.

Unoconvert
Expand Down
32 changes: 30 additions & 2 deletions src/unoserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ def __init__(
uno_port="2002",
user_installation=None,
conversion_timeout=None,
stop_after=None,
):
self.interface = interface
self.uno_interface = uno_interface
self.port = port
self.uno_port = uno_port
self.user_installation = user_installation
self.conversion_timeout = conversion_timeout
self.stop_after = stop_after
self.libreoffice_process = None
self.xmlrcp_thread = None
self.xmlrcp_server = None
Expand Down Expand Up @@ -138,6 +140,20 @@ def serve(self):
self.xmlrcp_server = server
server.register_introspection_functions()

self.number_of_requests = 0

def stop_after():
if self.stop_after is None:
return
self.number_of_requests += 1
if self.number_of_requests == self.stop_after:
logger.info(
"Processed %d requests, exiting.",
self.stop_after,
)
self.intentional_exit = True
self.libreoffice_process.terminate()

@server.register_function
def info():
import_filters = self.conv.get_filter_names(
Expand Down Expand Up @@ -180,14 +196,17 @@ def convert(
infiltername,
)
try:
return future.result(timeout=self.conversion_timeout)
result = future.result(timeout=self.conversion_timeout)
except futures.TimeoutError:
logger.error(
"Conversion timeout, terminating conversion and exiting."
)
self.conv.local_context.dispose()
self.libreoffice_process.terminate()
raise
else:
stop_after()
return result

@server.register_function
def compare(
Expand All @@ -214,14 +233,17 @@ def compare(
filetype,
)
try:
return future.result(timeout=self.conversion_timeout)
result = future.result(timeout=self.conversion_timeout)
except futures.TimeoutError:
logger.error(
"Comparison timeout, terminating conversion and exiting."
)
self.conv.local_context.dispose()
self.libreoffice_process.terminate()
raise
else:
stop_after()
return result

server.serve_forever()

Expand Down Expand Up @@ -304,6 +326,11 @@ def main():
help="Terminate Libreoffice and exit if a conversion does not complete in the "
"given time (in seconds).",
)
parser.add_argument(
"--stop-after",
type=int,
help="Terminate Libreoffice and exit after the given number of requests.",
)
args = parser.parse_args()

if args.daemon:
Expand All @@ -328,6 +355,7 @@ def main():
args.uno_port,
user_installation,
args.conversion_timeout,
args.stop_after,
)

if args.executable is not None:
Expand Down