Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
Implemented reades-writer-lock for updating the cached rrd images. Th…
Browse files Browse the repository at this point in the history
…is should make updating the graphs much more robust on well frequented sites.
  • Loading branch information
FlashSystems committed Apr 4, 2015
1 parent 0d02cce commit f2e819f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
17 changes: 14 additions & 3 deletions graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* RRDGraph Plugin: Graph generator
*
* @author Daniel Goß <[email protected]>
* @author Daniel Go� <[email protected]>
* @license MIT
*/

Expand Down Expand Up @@ -30,7 +30,7 @@
//-- ACL-Check
if (auth_quickaclcheck($pageId) < AUTH_READ) throw new Exception("Access denied by ACL.");

//-- Currently only fs and e are supported moeds.
//-- Currently only fs and e are supported modes.
if ($mode != 'fs') $mode = 'e';

//-- Load the rrdgraph helper. This helper contains the cache manager and other stuff used here.
Expand All @@ -41,6 +41,10 @@
$cacheInfo = $rrdGraphHelper->getImageCacheInfo($pageId, $graphId, $rangeNr, $mode);
if (! $cacheInfo->isValid()) {

//-- We found we should update the file. Upgrade our lock to an exclusive one.
// This way we OWN the lockfile and nobody else can get confused while we do our thing.
$cacheInfo->upgradeLock();

$recipe = $rrdGraphHelper->fetchRecipe($pageId, $graphId);
if ($recipe === null) throw new Exception("The graph " . $graphId . " is not defined on page " . $pageId);

Expand Down Expand Up @@ -137,12 +141,17 @@
}

//-- Correct the filename of the graph in case the rangeNr was modified by the range check.
unset($cacheInfo);
$cacheInfo = $rrdGraphHelper->getImageCacheInfo($pageId, $graphId, $rangeNr, $mode);

//-- We've to reupgrade the lock, because we got a new cacheInfo instance.
$cacheInfo->UpgradeLock();

//-- Render the RRD-Graph
if (rrd_graph($cacheInfo->getFilename(), array_merge($commandLine, $graphCommands)) === false) {throw new Exception(rrd_error());}
if (rrd_graph($cacheInfo->getFilename(), array_merge($commandLine, $graphCommands)) === false) throw new Exception(rrd_error());

//-- Get the new cache info of the image to send the correct headers.
unset($cacheInfo);
$cacheInfo = $rrdGraphHelper->getImageCacheInfo($pageId, $graphId, $rangeNr, $mode);
}

Expand All @@ -165,3 +174,5 @@
catch (Exception $ex) {
ErrorImage::outputErrorImage("Graph generation failed", $ex->getMessage());
}

if (isset($cacheInfo)) unset($cacheInfo);
20 changes: 19 additions & 1 deletion helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* RRDGraph Plugin: Helper classes
*
* @author Daniel Goß <[email protected]>
* @author Daniel Go� <[email protected]>
* @license MIT
*/

Expand Down Expand Up @@ -158,6 +158,9 @@ public function getLastModified() {
class rrdgraph_image_info {
/** @var String Name of the rrd image file within the cache. */
private $fileName;

/** @var Resource File handle used to lock the file. */
private $fileHandle;

/** @var Integer Timestamp until the file named by $fileName ist considered valid. */
private $validUntil;
Expand All @@ -175,6 +178,17 @@ public function __construct($fileName, $validUntil, $lastModified) {
$this->fileName = $fileName;
$this->validUntil = $validUntil;
$this->lastModified = $lastModified;

//-- Get a shared lock on the lock-file.
$this->fileHandle = fopen($fileName . ".lock", "w+");
flock($this->fileHandle, LOCK_SH);
}

/**
* D'tor
*/
public function __destruct() {
fclose($this->fileHandle);
}

/**
Expand Down Expand Up @@ -205,6 +219,10 @@ public function getLastModified() {
public function isValid() {
return $this->validUntil > time();
}

public function upgradeLock() {
flock($this->fileHandle, LOCK_EX);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions plugin.info.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
base rrdgraph
author Daniel Goß
email [email protected]
date 2014-10-20
date 2015-04-04
name RRD graph generator
desc This plugin creates graphs from RRD tool data. Uses php5-rrd to generate the images.
url http://www.tobedone.info/
url https://github.com/FlashSystems/rrdgraph

0 comments on commit f2e819f

Please sign in to comment.