- added new housekeeping cronjob für cached files, closed #200

- added new cache size information to /setup page
This commit is contained in:
Exodus4D
2016-06-25 20:51:55 +02:00
parent 029e8cee67
commit e16b507ef0
9 changed files with 202 additions and 35 deletions

View File

@@ -7,7 +7,7 @@ web = TRUE
; run every minute
instant = * * * * *
; run in downtime 11:00 GMT/UTC
; run on EVE downtime 11:00 GMT/UTC
downtime = 0 11 * * *
[CRON.jobs]
@@ -24,4 +24,7 @@ deleteLogData = Cron\CharacterUpdate->deleteLogData, @hourly
deleteMapData = Cron\MapUpdate->deleteMapData, @downtime
; delete expired character cookie authentication data
deleteAuthenticationData = Cron\CharacterUpdate->deleteAuthenticationData, @downtime
deleteAuthenticationData = Cron\CharacterUpdate->deleteAuthenticationData, @downtime
; delete expired cache files
deleteExpiredCacheData = Cron\Cache->deleteExpiredData, @weekly

View File

@@ -8,6 +8,7 @@
namespace Controller;
use data\filesystem\Search;
use DB;
use DB\SQL;
use DB\SQL\MySQL as MySQL;
@@ -174,7 +175,7 @@ class Setup extends Controller {
$f3->set('checkDatabase', $this->checkDatabase($f3, $fixColumns));
// set cache size
$f3->set('cacheSize', $this->getCacheSize($f3));
$f3->set('cacheSize', $this->getCacheData($f3));
}
/**
@@ -229,7 +230,7 @@ class Setup extends Controller {
$serverInfo = [
'time' => [
'label' => 'Time',
'value' => date('Y/m/d H:i:s') . ' - (' . date_default_timezone_get() . ')'
'value' => date('Y/m/d H:i:s') . ' - (' . $f3->get('TZ') . ')'
],
'os' => [
'label' => 'OS',
@@ -746,12 +747,29 @@ class Setup extends Controller {
/**
* get cache folder size as string
* @param \Base $f3
* @return string
* @return array
*/
protected function getCacheSize(\Base $f3){
$dir = $f3->get('TEMP') . '/cache';
$bytes = $this->folderSize($dir);
return $this->convertBytes($bytes);
protected function getCacheData(\Base $f3){
// get all cache -----------------------------------------------------------------------------------------
$cacheFilesAll = Search::getFilesByMTime( $f3->get('TEMP') );
$bytesAll = 0;
foreach($cacheFilesAll as $filename => $file) {
$bytesAll += $file->getSize();
}
// get data cache -----------------------------------------------------------------------------------------
$cacheFilesData = Search::getFilesByMTime( $f3->get('TEMP') . 'cache/' );
$bytesData = 0;
foreach($cacheFilesData as $filename => $file) {
$bytesData += $file->getSize();
}
return [
'all' => $this->convertBytes($bytesAll),
'data' => $this->convertBytes($bytesData),
'template' => $this->convertBytes($bytesAll - $bytesData)
];
}
/**
@@ -762,19 +780,6 @@ class Setup extends Controller {
$f3->clear('CACHE');
}
/**
* get folder size in bytes (recursive)
* @param string $dir
* @return int
*/
protected function folderSize($dir){
$size = 0;
foreach (glob(rtrim($dir, '/').'/*', GLOB_NOSORT) as $each) {
$size += is_file($each) ? filesize($each) : $this->folderSize($each);
}
return $size;
}
/**
* convert Bytes to string + suffix
* @param int $bytes

60
app/main/cron/cache.php Normal file
View File

@@ -0,0 +1,60 @@
<?php
/**
* Created by PhpStorm.
* User: Exodus
* Date: 25.06.2016
* Time: 14:59
*/
namespace cron;
use data\filesystem\Search;
use Controller;
class Cache {
const LOG_TEXT = '%s [%\'_10s] files, size [%\'_10s] byte, not writable [%\'_10s] files, errors [%\'_10s], exec (%.3Fs)';
/**
* clear expired cached files
* >> >php index.php "/cron/deleteExpiredCacheData"
* @param \Base $f3
*/
function deleteExpiredData(\Base $f3){
$time_start = microtime(true);
// cache dir (dir is recursively searched...)
$cacheDir = $f3->get('TEMP');
$filterTime = (int)strtotime('-' . $f3->get('PATHFINDER.CACHE.EXPIRE_MAX') . ' seconds');
$expiredFiles = Search::getFilesByMTime($cacheDir, $filterTime);
$deletedFiles = 0;
$deletedSize = 0;
$notWritableFiles = 0;
$deleteErrors = 0;
foreach($expiredFiles as $filename => $file) {
/**
* @var $file \SplFileInfo
*/
if( $file->isWritable() ){
$tmpSize = $file->getSize();
if( unlink($file->getRealPath()) ){
$deletedSize += $tmpSize;
$deletedFiles++;
}else{
$deleteErrors++;
}
}else{
$notWritableFiles++;
}
}
$execTime = microtime(true) - $time_start;
// Log ------------------------
$log = Controller\LogController::getLogger('cron_' . __FUNCTION__);
$log->write( sprintf(self::LOG_TEXT, __FUNCTION__, $deletedFiles, $deletedSize, $notWritableFiles, $deleteErrors, $execTime) );
}
}

View File

@@ -22,7 +22,7 @@ class MapUpdate {
* >> php index.php "/cron/deactivateMapData"
* @param \Base $f3
*/
function deactivateMapData($f3){
function deactivateMapData(\Base $f3){
$pfDB = DB\Database::instance()->getDB('PF');
@@ -48,7 +48,7 @@ class MapUpdate {
* >> php index.php "/cron/deleteMapData"
* @param \Base $f3
*/
function deleteMapData($f3){
function deleteMapData(\Base $f3){
$pfDB = DB\Database::instance()->getDB('PF');

View File

@@ -0,0 +1,58 @@
<?php
/**
* Created by PhpStorm.
* User: Exodus
* Date: 25.06.2016
* Time: 16:58
*/
namespace data\filesystem;
class Search {
/**
* timestamp (seconds) filter files by mTime()
* -> default = "no filter"
* @var int
*/
static $filterTime = 0;
/**
* recursive file filter by mTime
* @param string $dir
* @param int $mTime
* @return \RecursiveIteratorIterator
*/
static function getFilesByMTime($dir, $mTime = null){
if(is_null($mTime)){
self::$filterTime = time();
}else{
self::$filterTime = (int)$mTime;
}
$directory = new \RecursiveDirectoryIterator( $dir, \FilesystemIterator::SKIP_DOTS );
$files = new \RecursiveCallbackFilterIterator($directory, function ($current, $key, $iterator) {
// Check for last modification date
/**
* @var $current \RecursiveDirectoryIterator
*/
if (
!$current->isFile() || // allow recursion
(
strpos($current->getFilename(), '.') !== 0 && // skip e.g. ".gitignore"
$current->getMTime() < self::$filterTime // check last modification date
)
){
return true;
}
return false;
});
return new \RecursiveIteratorIterator($files);
}
}

View File

@@ -92,10 +92,12 @@ EXECUTION_LIMIT = 50
; CACHE ===========================================================================================
[PATHFINDER.CACHE]
; cache character log informations in seconds. This is ignored if ship/system switch was detected
; expire time for character log data (seconds). (default: 300s)
CHARACTER_LOG = 300
; cache time for all system data within a constellation (this will never change) 30d
; expire time for static system data (seconds) (default: 30d)
CONSTELLATION_SYSTEMS = 2592000
; max expire time. Expired cache files will be deleted by cronjob (seconds) (default: 60d)
EXPIRE_MAX = 5184000
; LOGGING =========================================================================================
[PATHFINDER.LOGFILES]

File diff suppressed because one or more lines are too long

View File

@@ -758,25 +758,56 @@
<div class="panel-heading text-left">
<h3 class="panel-title">Cache
<span class="pull-right">
<check if="{{ @cacheSize }}">
<check if="{{ @cacheSize.all }}">
<true>
{{ @cacheSize }}&nbsp;
<i class="fa fa-fw fa-battery-half txt-color txt-color-success"></i>
{{ @cacheSize.all }}&nbsp;
</true>
<false>
0 KB&nbsp;
<i class="fa fa-fw fa-battery-empty txt-color txt-color-warning" title="" data-original-title="empty cache"></i>
</false>
</check>
</span>
</h3>
</div>
<div class="panel-footer btn-group btn-group-justified">
<a href="?clearCache=1" class="btn btn-warning {{ (@cacheSize) ?'':'disabled' }}">
<i class="fa fa-fw fa-times"></i> Clear cache
</a>
<div class="panel-body no-padding">
<div class="btn-group btn-group-justified">
<span class="btn btn-default disabled btn-fake">
Data
<span class="pull-right">
<check if="{{ @cacheSize.data }}">
<true>
{{ @cacheSize.data }}
</true>
<false>
0 KB
</false>
</check>
</span>
</span>
<a href="?clearCache=1" class="btn btn-warning {{ (@cacheSize.data) ?'':'disabled' }}" role="button">
<i class="fa fa-fw fa-times"></i> Clear
</a>
</div>
<div class="btn-group btn-group-justified">
<span class="btn btn-default disabled btn-fake">
Template
<span class="pull-right">
<check if="{{ @cacheSize.template }}">
<true>
{{ @cacheSize.template }}
</true>
<false>
0 KB
</false>
</check>
</span>
</span>
<span class="btn btn-default disabled btn-fake"></span>
</div>
</div>
</div>
</div>

View File

@@ -28,6 +28,14 @@ input, select{
}
}
// "fake" button (no user interaction)
.btn-fake{
border: none;
text-align: left;
color: $gray-lighter !important;
background-color: $gray !important;
}
// drag&drop zone
.pf-form-dropzone{
border: 2px dashed $gray-darker;