From: Christian Weiske Date: Thu, 6 Nov 2014 20:19:20 +0000 (+0100) Subject: kill cutycapt if it hangs X-Git-Tag: v0.2.0~3 X-Git-Url: https://git.cweiske.de/phancap.git/commitdiff_plain/ea958e4397b2625fb588faf4e474f8cdbaa10cdc kill cutycapt if it hangs --- diff --git a/src/phancap/Adapter/Cutycapt.php b/src/phancap/Adapter/Cutycapt.php index 19657c8..a006a44 100644 --- a/src/phancap/Adapter/Cutycapt.php +++ b/src/phancap/Adapter/Cutycapt.php @@ -56,6 +56,9 @@ class Adapter_Cutycapt if (\System::which('convert') === false) { $arErrors[] = '"convert" (imagemagick) is not installed'; } + if (\System::which('timeout') === false) { + $arErrors[] = '"timeout" (GNU coreutils) is not installed'; + } error_reporting($old); if (count($arErrors)) { @@ -80,6 +83,7 @@ class Adapter_Cutycapt if ($format == 'jpg') { $format = 'jpeg'; } + $maxWaitTime = 30;//seconds $serverNumber = $this->getServerNumber($options); $tmpPath = $img->getPath() . '-tmp'; @@ -87,7 +91,7 @@ class Adapter_Cutycapt . ' --url=' . escapeshellarg($options->values['url']) . ' --out-format=' . escapeshellarg($format) . ' --out=' . escapeshellarg($tmpPath) - . ' --max-wait=10000' + . ' --max-wait=' . (($maxWaitTime - 1) * 1000) . ' --min-width=' . $options->values['bwidth']; if ($options->values['bheight'] !== null) { $cmd .= ' --min-height=' . $options->values['bheight']; @@ -97,7 +101,9 @@ class Adapter_Cutycapt . ' -e /dev/stdout' . ' --server-args="-screen 0, 1024x768x24"' . ' --server-num=' . $serverNumber; - Executor::run($xvfbcmd . ' ' . $cmd); + //cutycapt hangs sometimes - https://sourceforge.net/p/cutycapt/bugs/8/ + // we kill it if it does not exit itself + Executor::runForSomeTime($xvfbcmd . ' ' . $cmd, $maxWaitTime); $this->resize($tmpPath, $img, $options); } diff --git a/src/phancap/Executor.php b/src/phancap/Executor.php index 5b59094..f9962bb 100644 --- a/src/phancap/Executor.php +++ b/src/phancap/Executor.php @@ -43,5 +43,23 @@ class Executor throw new \Exception('Error running cutycapt', $exitcode); } } + + /** + * Let the command run for some time. Kill it if it did not exit itself. + * + * We use the GNU coreutils "timeout" utility instead of the pcntl + * extension since pcntl is disabled on mod_php. + * + * @param string $cmd Full command including parameters and options + * + * @return void + * @throws \Exception When the exit code is not 0 + */ + public static function runForSomeTime($cmd, $seconds) + { + return static::run( + 'timeout --signal=9 ' . $seconds . 's ' . $cmd + ); + } } ?>