unoconv / unoserver

MIT License
592 stars 81 forks source link

Add `--stop-after` parameter to `unoserver` #144

Open Witiko opened 3 weeks ago

Witiko commented 3 weeks ago

When using unoserver as a long-running service, it is often useful to restart both unoserver and LibreOffice after a certain number of requests (e.g. 1,000), so that any potential bad state is reset and any potential misallocated memory in LibreOffice is freed.

This PR adds the parameter --stop-after to unoserver:

Demonstation

Here is a demonstration of using the new parameter.

First, we spin up a Docker container with Ubuntu, LibreOffice, Python, and the code from this PR:

$ docker run --name unoserver-stop-after --rm -it ubuntu
# apt update
# apt install git libreoffice python3 python3-pip wget
# pip install -U pip wheel setuptools
# pip install git+https://github.com/witiko/unoserver@feat/stop-after

Next, we download an example DOCX file and we launch unoserver with the parameter --stop-after 5:

# wget https://calibre-ebook.com/downloads/demos/demo.docx
# unoserver --stop-after 5
INFO:unoserver:Starting unoserver 3.0.2.dev0.
INFO:unoserver:Command: /usr/bin/soffice --headless --invisible --nocrashreport --nodefault --nologo --nofirststartwizard --norestore -env:UserInstallation=file:///tmp/tmpm2fgj8md --accept=socket,host=127.0.0.1,port=2002,tcpNoDelay=1;urp;StarOffice.ComponentContext
INFO:unoserver:Starting UnoConverter.
INFO:unoserver:Starting UnoComparer.
INFO:unoserver:Server PID: 11762

Finally, from the outside of the container, we launch 5 conversions:

$ for _ in {1..5}; do docker exec unoserver-stop-after unoconvert /demo.docx /demo.odt; done
INFO:unoserver:Connecting.
INFO:unoserver:Converting.
INFO:unoserver:Saved to /demo.odt.
INFO:unoserver:Connecting.
INFO:unoserver:Converting.
INFO:unoserver:Saved to /demo.odt.
INFO:unoserver:Connecting.
INFO:unoserver:Converting.
INFO:unoserver:Saved to /demo.odt.
INFO:unoserver:Connecting.
INFO:unoserver:Converting.
INFO:unoserver:Saved to /demo.odt.
INFO:unoserver:Connecting.
INFO:unoserver:Converting.
INFO:unoserver:Saved to /demo.odt.

Meanwhile, in the container, we can see that unoserver has exited with a zero exit code after the fifth conversion:

127.0.0.1 - - [30/Oct/2024 13:04:56] "POST /RPC2 HTTP/1.1" 200 -
INFO:unoserver:Opening /demo.docx for input
INFO:unoserver:Exporting to /demo.odt
INFO:unoserver:Using writer8 export filter from None to writer8
127.0.0.1 - - [30/Oct/2024 13:04:57] "POST /RPC2 HTTP/1.1" 200 -
127.0.0.1 - - [30/Oct/2024 13:05:16] "POST /RPC2 HTTP/1.1" 200 -
INFO:unoserver:Opening /demo.docx for input
INFO:unoserver:Exporting to /demo.odt
INFO:unoserver:Using writer8 export filter from None to writer8
127.0.0.1 - - [30/Oct/2024 13:05:17] "POST /RPC2 HTTP/1.1" 200 -
127.0.0.1 - - [30/Oct/2024 13:05:17] "POST /RPC2 HTTP/1.1" 200 -
INFO:unoserver:Opening /demo.docx for input
INFO:unoserver:Exporting to /demo.odt
INFO:unoserver:Using writer8 export filter from None to writer8
127.0.0.1 - - [30/Oct/2024 13:05:17] "POST /RPC2 HTTP/1.1" 200 -
127.0.0.1 - - [30/Oct/2024 13:05:17] "POST /RPC2 HTTP/1.1" 200 -
INFO:unoserver:Opening /demo.docx for input
INFO:unoserver:Exporting to /demo.odt
INFO:unoserver:Using writer8 export filter from None to writer8
127.0.0.1 - - [30/Oct/2024 13:05:18] "POST /RPC2 HTTP/1.1" 200 -
127.0.0.1 - - [30/Oct/2024 13:05:18] "POST /RPC2 HTTP/1.1" 200 -
INFO:unoserver:Opening /demo.docx for input
INFO:unoserver:Exporting to /demo.odt
INFO:unoserver:Using writer8 export filter from None to writer8
INFO:unoserver:Processed 5 requests, exiting.
127.0.0.1 - - [30/Oct/2024 13:05:19] "POST /RPC2 HTTP/1.1" 200 -
# echo $?
0
Witiko commented 2 weeks ago

What do you think, @regebro?