Optimize performance of cron that rotate logs

This commit is contained in:
Laurent Destailleur 2018-09-26 01:45:24 +02:00
parent c2335c0a5e
commit f2d53e80e5
2 changed files with 49 additions and 46 deletions

View File

@ -341,7 +341,7 @@ class Utils
{
// Renommer fichier sortie en fichier erreur
//print "$outputfile -> $outputerror";
@dol_delete_file($outputerror,1);
@dol_delete_file($outputerror, 1, 0, 0, null, false, 0);
@rename($outputfile,$outputerror);
// Si safe_mode on et command hors du parametre exec, on a un fichier out vide donc errormsg vide
if (! $errormsg)
@ -448,7 +448,7 @@ class Utils
{
$i++;
if ($i <= $keeplastnfiles) continue;
dol_delete_file($val['fullname']);
dol_delete_file($val['fullname'], 0, 0, 0, null, false, 0);
}
}
@ -675,8 +675,9 @@ class Utils
}
/**
* This saves syslog files and compresses older ones
* Used from cronjob
* This saves syslog files and compresses older ones.
* Nb of archive to keep is defined into $conf->global->SYSLOG_FILE_SAVES
* CAN BE A CRON TASK
*
* @return int 0 if OK, < 0 if KO
*/
@ -714,50 +715,52 @@ class Utils
$logname = $file['name'];
$logpath = $file['path'];
// Handle already compressed files to rename them and add +1
if (dol_is_file($logpath.'/'.$logname) && dol_filesize($logpath.'/'.$logname) > 0) // If log file exists and is not empty
{
// Handle already compressed files to rename them and add +1
$filter = '^'.preg_quote($logname, '/').'\.([0-9]+)\.gz$';
$filter = '^'.preg_quote($logname, '/').'\.([0-9]+)\.gz$';
$gzfilestmp = dol_dir_list($logpath, 'files', 0, $filter);
$gzfiles = array();
$gzfilestmp = dol_dir_list($logpath, 'files', 0, $filter);
$gzfiles = array();
foreach($gzfilestmp as $gzfile) {
$tabmatches = array();
preg_match('/'.$filter.'/i', $gzfile['name'], $tabmatches);
foreach($gzfilestmp as $gzfile) {
$tabmatches = array();
preg_match('/'.$filter.'/i', $gzfile['name'], $tabmatches);
$numsave = intval($tabmatches[1]);
$numsave = intval($tabmatches[1]);
$gzfiles[$numsave] = $gzfile;
}
krsort($gzfiles, SORT_NUMERIC);
foreach($gzfiles as $numsave => $dummy) {
if (dol_is_file($logpath.'/'.$logname.'.'.($numsave+1).'.gz')) {
return -2;
$gzfiles[$numsave] = $gzfile;
}
if($numsave >= $nbSaves) {
dol_delete_file($logpath.'/'.$logname.'.'.$numsave.'.gz');
} else {
dol_move($logpath.'/'.$logname.'.'.$numsave.'.gz', $logpath.'/'.$logname.'.'.($numsave+1).'.gz', 0, 1, 0, 0);
}
}
krsort($gzfiles, SORT_NUMERIC);
// Compress last save
if (dol_is_file($logpath.'/'.$logname.'.1')) {
if($nbSaves > 1) {
$gzfilehandle = gzopen($logpath.'/'.$logname.'.2.gz', 'wb9');
foreach($gzfiles as $numsave => $dummy) {
if (dol_is_file($logpath.'/'.$logname.'.'.($numsave+1).'.gz')) {
return -2;
}
if($numsave >= $nbSaves) {
dol_delete_file($logpath.'/'.$logname.'.'.$numsave.'.gz', 0, 0, 0, null, false, 0);
} else {
dol_move($logpath.'/'.$logname.'.'.$numsave.'.gz', $logpath.'/'.$logname.'.'.($numsave+1).'.gz', 0, 1, 0, 0);
}
}
// Compress current file and recreate it
if ($nbSaves > 0) { // If $nbSaves is 1, we keep 1 archive .gz file, If 2, we keep 2 .gz files
$gzfilehandle = gzopen($logpath.'/'.$logname.'.1.gz', 'wb9');
if (empty($gzfilehandle)) {
$this->error = 'Failted to open file '.$logpath.'/'.$logname.'.2.gz';
$this->error = 'Failted to open file '.$logpath.'/'.$logname.'.1.gz';
return -3;
}
$sourcehandle = fopen($logpath.'/'.$logname.'.1', 'r');
$sourcehandle = fopen($logpath.'/'.$logname, 'r');
if (empty($sourcehandle)) {
$this->error = 'Failed to open file '.$logpath.'/'.$logname.'.1';
$this->error = 'Failed to open file '.$logpath.'/'.$logname;
return -4;
}
@ -767,19 +770,18 @@ class Utils
fclose($sourcehandle);
gzclose($gzfilehandle);
} else {
dol_delete_file($logpath.'/'.$logname.'.1');
}
}
// Compress current file et recreate it
if (dol_is_file($logpath.'/'.$logname)) {
if (dol_move($logpath.'/'.$logname, $logpath.'/'.$logname.'.1', 0, 1, 0, 0))
{
$newlog = fopen($logpath.'/'.$logname, 'a+');
fclose($newlog);
@chmod($logpath.'/'.$logname.'.1.gz', octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK));
}
dol_delete_file($logpath.'/'.$logname, 0, 0, 0, null, false, 0);
// Create empty file
$newlog = fopen($logpath.'/'.$logname, 'a+');
fclose($newlog);
//var_dump($logpath.'/'.$logname." - ".octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK));
@chmod($logpath.'/'.$logname, octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK));
}
}

View File

@ -1133,10 +1133,11 @@ function dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disable
* @param int $nohook Disable all hooks
* @param object $object Current object in use
* @param boolean $allowdotdot Allow to delete file path with .. inside. Never use this, it is reserved for migration purpose.
* @param int $indexdatabase Try to remove also index entries.
* @return boolean True if no error (file is deleted or if glob is used and there's nothing to delete), False if error
* @see dol_delete_dir
*/
function dol_delete_file($file,$disableglob=0,$nophperrors=0,$nohook=0,$object=null,$allowdotdot=false)
function dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1)
{
global $db, $conf, $user, $langs;
global $hookmanager;
@ -1200,7 +1201,7 @@ function dol_delete_file($file,$disableglob=0,$nophperrors=0,$nohook=0,$object=n
{
$rel_filetodelete = preg_replace('/^[\\/]/', '', $rel_filetodelete);
if (is_object($db)) // $db may not be defined when lib is in a context with define('NOREQUIREDB',1)
if (is_object($db) && $indexdatabase) // $db may not be defined when lib is in a context with define('NOREQUIREDB',1)
{
dol_syslog("Try to remove also entries in database for full relative path = ".$rel_filetodelete, LOG_DEBUG);
include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';