diff --git a/README.rst b/README.rst index 715dafd..e2d9437 100644 --- a/README.rst +++ b/README.rst @@ -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" @@ -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 diff --git a/src/unoserver/server.py b/src/unoserver/server.py index 3748c4f..263afec 100644 --- a/src/unoserver/server.py +++ b/src/unoserver/server.py @@ -52,6 +52,7 @@ def __init__( uno_port="2002", user_installation=None, conversion_timeout=None, + stop_after=None, ): self.interface = interface self.uno_interface = uno_interface @@ -59,6 +60,7 @@ def __init__( 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 @@ -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( @@ -180,7 +196,7 @@ 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." @@ -188,6 +204,9 @@ def convert( self.conv.local_context.dispose() self.libreoffice_process.terminate() raise + else: + stop_after() + return result @server.register_function def compare( @@ -214,7 +233,7 @@ 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." @@ -222,6 +241,9 @@ def compare( self.conv.local_context.dispose() self.libreoffice_process.terminate() raise + else: + stop_after() + return result server.serve_forever() @@ -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: @@ -328,6 +355,7 @@ def main(): args.uno_port, user_installation, args.conversion_timeout, + args.stop_after, ) if args.executable is not None: