'Libreoffice's 'convert to pdf' command hangs for a long time untill it kills the server. Suspect n°1 for executing the command is Imagick

First knowledge of the issue was when the server kept going down randomly. Investigation shows the following command as having caused the issue after hanging for about 1h 48 mins :

enter image description here

A bit of research on Google showed that the --outdir path was coming from Imagick :

When IM can't do its work in main memory, it uses disk. If the process completes normally, the temporary disk files are deleted. If the process fails, these files are often not deleted.

source : https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=23649

This, I believe, is the only place in the code base That Imagick is being used :

public function capturePreview($sFileName, $sFilepath) 
{        
    // removes file name from file path

$sPreviewFolderPath = pathinfo($sFilepath, PATHINFO_DIRNAME);

foreach ($this->aDimensionsArray as $aDimensions) {
    $iWidth = $aDimensions["width"];
    $iHeight = $aDimensions["height"];
    $sImageMagickOutput = shell_exec("convert ". $sFilepath . "[0] -gravity north -quality 80 -resize " . $iWidth . "x" . $iHeight . "^ -extent " . $iWidth . "x" . $iHeight . " " . $sPreviewFolderPath . "/" . $sFileName . "_preview_" . $iWidth . "_" . $iHeight . ".jpeg 2>&1");
        if ($sImageMagickOutput !== null) {
            return ["error" => $sImageMagickOutput, "dimension" => $iWidth . "x" . $iHeight];
        }
    }
}

This bit of code's job is only to resize and crop images before storing them. I'm unsure why Imagick needs to use LibreOffice for that or why soffice command hangs, but I am out of ideas on how to go about fixing this. My first thought is to avoid the worse case scenario, which is Imagick bringing down the server.

1 - Would adding a timeout 60s at the start like below do the job or would soffice not be subjugated to the timeout :

$sImageMagickOutput = shell_exec("timeout 60s convert etc ...")

2 - How Should I go about debugging this so that the issue does not happen in the first place ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source