support concurrent requests with cutycapt
authorChristian Weiske <cweiske@cweiske.de>
Wed, 9 Apr 2014 05:34:27 +0000 (07:34 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 9 Apr 2014 05:34:27 +0000 (07:34 +0200)
src/phancap/Adapter/Cutycapt.php
src/phancap/Repository.php
tools/multiple.sh [new file with mode: 0755]

index 909c90f..863c732 100644 (file)
@@ -3,6 +3,9 @@ namespace phancap;
 
 class Adapter_Cutycapt
 {
+    protected $lockHdl;
+    protected $lockFile = null;
+
     public function isAvailable()
     {
         //FIXME: setup check for xvfbrun, cutycapt, convert
@@ -14,6 +17,8 @@ class Adapter_Cutycapt
         if ($format == 'jpg') {
             $format = 'jpeg';
         }
+
+        $serverNumber = $this->getServerNumber($options);
         $tmpPath = $img->getPath() . '-tmp';
         $cmd = 'cutycapt'
             . ' --url=' . escapeshellarg($options->values['url'])
@@ -27,12 +32,61 @@ class Adapter_Cutycapt
 
         $xvfbcmd = 'xvfb-run'
             . ' -e /dev/stdout'
-            . ' --server-args="-screen 0, 1024x768x24"';
+            . ' --server-args="-screen 0, 1024x768x24"'
+            . ' --server-num=' . $serverNumber;
         Executor::run($xvfbcmd . ' ' . $cmd);
 
         $this->resize($tmpPath, $img, $options);
     }
 
+    protected function getServerNumber()
+    {
+        //clean stale lock files
+        $this->cleanup();
+
+        $num = 100;
+        $bFound = false;
+        do {
+            ++$num;
+            $f = $this->config->cacheDir . 'tmp-curlycapt-server-' . $num . '.lock';
+            $this->lockHdl = fopen($f, 'w');
+            if (flock($this->lockHdl, LOCK_EX | LOCK_NB)) {
+                $this->lockFile = $f;
+                $bFound = true;
+                break;
+            } else {
+                fclose($this->lockHdl);
+            }
+        } while ($num < 200);
+
+        if (!$bFound) {
+            throw new \Exception('Too many requests running');
+        }
+
+        $this->lockFile = $f;
+        return $num;
+    }
+
+    public function cleanup()
+    {
+        if ($this->lockFile !== null && $this->lockHdl) {
+            flock($this->lockHdl, LOCK_UN);
+            unlink($this->lockFile);
+        }
+
+        $lockFiles = glob(
+            $this->config->cacheDir . 'tmp-curlycapt-server-*.lock'
+        );
+
+        $now = time();
+        foreach ($lockFiles as $file) {
+            if ($now - filemtime($file) > 120) {
+                //delete stale lock file; probably something crashed.
+                unlink($file);
+            }
+        }
+    }
+
     protected function resize($tmpPath, $img, $options)
     {
         if ($options->values['sformat'] == 'pdf') {
@@ -57,5 +111,10 @@ class Adapter_Cutycapt
         //var_dump($convertcmd);die();
         unlink($tmpPath);
     }
+
+    public function setConfig(Config $config)
+    {
+        $this->config = $config;
+    }
 }
 ?>
index 157a062..7bdbe3a 100644 (file)
@@ -56,7 +56,14 @@ class Repository
     protected function render(Image $img, Options $options)
     {
         $adapter = new Adapter_Cutycapt();
-        $adapter->render($img, $options);
+        $adapter->setConfig($this->config);
+        try {
+            $adapter->render($img, $options);
+            $adapter->cleanup();
+        } catch (\Exception $e) {
+            $adapter->cleanup();
+            throw $e;
+        }
     }
 }
 ?>
diff --git a/tools/multiple.sh b/tools/multiple.sh
new file mode 100755 (executable)
index 0000000..8515860
--- /dev/null
@@ -0,0 +1,8 @@
+#!/bin/sh
+curl -i 'http://phancap.bogo/get.php?smaxage=0&url=http%3A%2F%2Fwww.bogo%2F'&
+curl -i 'http://phancap.bogo/get.php?smaxage=0&url=http%3A%2F%2Fwww.bogo%2Ftagebuch%2F'&
+curl -i 'http://phancap.bogo/get.php?smaxage=0&url=http://www.bogo/aboutme.htm'&
+curl -i 'http://phancap.bogo/get.php?smaxage=0&url=http://www.bogo/projects.htm'&
+curl -i 'http://phancap.bogo/get.php?smaxage=0&url=http://www.bogo/tagebuch/Running%20Apache%20with%20a%20dozen%20PHP%20versions.htm'&
+curl -i 'http://phancap.bogo/get.php?smaxage=0&url=http://www.bogo/tagebuch/ouya-emulator-setup.htm'&
+