This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented reades-writer-lock for updating the cached rrd images. Th…
…is should make updating the graphs much more robust on well frequented sites.
- Loading branch information
1 parent
0d02cce
commit f2e819f
Showing
3 changed files
with
35 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
/** | ||
* RRDGraph Plugin: Graph generator | ||
* | ||
* @author Daniel Goß <[email protected]> | ||
* @author Daniel Go� <[email protected]> | ||
* @license MIT | ||
*/ | ||
|
||
|
@@ -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. | ||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
} | ||
|
||
|
@@ -165,3 +174,5 @@ | |
catch (Exception $ex) { | ||
ErrorImage::outputErrorImage("Graph generation failed", $ex->getMessage()); | ||
} | ||
|
||
if (isset($cacheInfo)) unset($cacheInfo); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
/** | ||
* RRDGraph Plugin: Helper classes | ||
* | ||
* @author Daniel Goß <[email protected]> | ||
* @author Daniel Go� <[email protected]> | ||
* @license MIT | ||
*/ | ||
|
||
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -205,6 +219,10 @@ public function getLastModified() { | |
public function isValid() { | ||
return $this->validUntil > time(); | ||
} | ||
|
||
public function upgradeLock() { | ||
flock($this->fileHandle, LOCK_EX); | ||
} | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |