ncjoes / office-converter

PHP Wrapper for LibreOffice
MIT License
197 stars 75 forks source link

Launch multiple LibreOffice profiles for conversion process #54

Closed caiosousaupp closed 4 months ago

trogau commented 4 months ago

@caiosousaupp : can you explain what the libreoffice profiles change in this commit does? I had a quick look in Issues but couldn't find an explainer and I'm not able to discern what this change does just from looking at the code. Any info would be appreciated!

caiosousaupp commented 4 months ago

@trogau First of all, I didn't intend to merge it. I opened the PR just for making the changes available or easily reviewed by someone with a similar issue. At least, it would have several changes before being merged. The most relevant would be to make it opcional.

We have an issue which occurs in our environment, where simultaneous profile usage results in an error from LibreOffice. The changes here add support for multiple profiles, chosen randomly, to avoid the issue with the simultaneous profile usage resulting in an conversion error.

It should be made optional and be used if, only if, you suffer from the issue. In our internal case, it was made by default to avoid making changes to the caller code. But, again, it was not my intention to merge it as is, although a PR was opened.

trogau commented 4 months ago

@caiosousaupp awesome thanks mate, that was exactly what I thought. I actually ended up merging your change in my local setup where I was having the exact same issue hoping that it was such a fix and it ended up working great. We have LibreOffice set up on a (very basic) web API and we found that multiple simultaneous conversions would fail.

We are potentially converting over 20 docs at a time so I think we'll need a much bigger random space to avoid the possibility of two requests getting the same random profile number - which I assume will trigger the error. (I am looking at lazily using flock() somehow to keep track of which profile is currently in use & just trying a new random directory until one is free but haven't thought about it too deeply yet!)

Thanks for the reply & appreciate the PR.

rabol commented 4 months ago

It does not look like the profiles get's deleted, so after a while you will just reuse the profiles. If you reuse the profile, what is the benefit then ?

trogau commented 4 months ago

@rabol : it doesn't matter if the profile is reused later - the change is to try to stop multiple calls to libreoffice from using the same (default) profile directory at the same time.

You are correct that the profiles don't get deleted, but they don't seem to consume a lot of space and from a quick look they don't seem to contain any sensitive information.

I decided to delete them anyway after running - I found 20 random profiles was insufficient for our use as we would have 10-20 calls at the same time. I've increased the random space to 1000 and I delete the profile directories after each time just to clean it up and reduce the chances of clashes.

There's definitely better approaches here but increasing the random space has solved the immediate problem.

rabol commented 4 months ago

maybe a config value for the number ?

If you have the need for may conversion, you should consider running libreoffice as a server :)

trogau commented 4 months ago

Do you mean via LibreOffice Online? It doesn't look like that has been updated for a long time (and/or may have moved to a commercial project at Collabora?).

I'm not a LibreOffice expert but not aware of any other server mode that it has, but would love to hear about it if there is one.

rabol commented 4 months ago

I think this should be working:

soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard

I have just found this myself and is experimenting as I also do a lot of conversion in my project

if you then call your convert method you can see that the execution time much less

rabol commented 4 months ago

btw. --safe-mode

 Starts in a safe mode, i.e. starts temporarily with a fresh user profile and helps to restore a broken  configuration.      

to avoid creating and deleting profiles