From 671f5b4269bb36f94c1903423cfd6b8531998af1 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 9 Apr 2014 07:34:27 +0200 Subject: [PATCH] support concurrent requests with cutycapt --- src/phancap/Adapter/Cutycapt.php | 61 +++++++++++++++++++++++++++++++- src/phancap/Repository.php | 9 ++++- tools/multiple.sh | 8 +++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100755 tools/multiple.sh diff --git a/src/phancap/Adapter/Cutycapt.php b/src/phancap/Adapter/Cutycapt.php index 909c90f..863c732 100644 --- a/src/phancap/Adapter/Cutycapt.php +++ b/src/phancap/Adapter/Cutycapt.php @@ -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; + } } ?> diff --git a/src/phancap/Repository.php b/src/phancap/Repository.php index 157a062..7bdbe3a 100644 --- a/src/phancap/Repository.php +++ b/src/phancap/Repository.php @@ -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 index 0000000..8515860 --- /dev/null +++ b/tools/multiple.sh @@ -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'& + -- 2.30.2