kill cutycapt if it hangs
authorChristian Weiske <cweiske@cweiske.de>
Thu, 6 Nov 2014 20:19:20 +0000 (21:19 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 6 Nov 2014 20:19:20 +0000 (21:19 +0100)
src/phancap/Adapter/Cutycapt.php
src/phancap/Executor.php

index 19657c8e778d0b52a1665700d4b557f7f4592bf0..a006a44574955ec4f3f6191b2e3e7097977a898c 100644 (file)
@@ -56,6 +56,9 @@ class Adapter_Cutycapt
         if (\System::which('convert') === false) {
             $arErrors[] = '"convert" (imagemagick) is not installed';
         }
         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)) {
 
         error_reporting($old);
         if (count($arErrors)) {
@@ -80,6 +83,7 @@ class Adapter_Cutycapt
         if ($format == 'jpg') {
             $format = 'jpeg';
         }
         if ($format == 'jpg') {
             $format = 'jpeg';
         }
+        $maxWaitTime = 30;//seconds
 
         $serverNumber = $this->getServerNumber($options);
         $tmpPath = $img->getPath() . '-tmp';
 
         $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)
             . ' --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'];
             . ' --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;
             . ' -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);
     }
 
         $this->resize($tmpPath, $img, $options);
     }
index 5b59094fd5419defae8da3cedc275b7403c759bd..f9962bbf7d5bde6f28abf5561b2929bb51b57288 100644 (file)
@@ -43,5 +43,23 @@ class Executor
             throw new \Exception('Error running cutycapt', $exitcode);
         }
     }
             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
+        );
+    }
 }
 ?>
 }
 ?>