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 // Renommer fichier sortie en fichier erreur
//print "$outputfile -> $outputerror"; //print "$outputfile -> $outputerror";
@dol_delete_file($outputerror,1); @dol_delete_file($outputerror, 1, 0, 0, null, false, 0);
@rename($outputfile,$outputerror); @rename($outputfile,$outputerror);
// Si safe_mode on et command hors du parametre exec, on a un fichier out vide donc errormsg vide // Si safe_mode on et command hors du parametre exec, on a un fichier out vide donc errormsg vide
if (! $errormsg) if (! $errormsg)
@ -448,7 +448,7 @@ class Utils
{ {
$i++; $i++;
if ($i <= $keeplastnfiles) continue; 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 * This saves syslog files and compresses older ones.
* Used from cronjob * 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 * @return int 0 if OK, < 0 if KO
*/ */
@ -714,6 +715,8 @@ class Utils
$logname = $file['name']; $logname = $file['name'];
$logpath = $file['path']; $logpath = $file['path'];
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 // Handle already compressed files to rename them and add +1
$filter = '^'.preg_quote($logname, '/').'\.([0-9]+)\.gz$'; $filter = '^'.preg_quote($logname, '/').'\.([0-9]+)\.gz$';
@ -738,26 +741,26 @@ class Utils
} }
if($numsave >= $nbSaves) { if($numsave >= $nbSaves) {
dol_delete_file($logpath.'/'.$logname.'.'.$numsave.'.gz'); dol_delete_file($logpath.'/'.$logname.'.'.$numsave.'.gz', 0, 0, 0, null, false, 0);
} else { } else {
dol_move($logpath.'/'.$logname.'.'.$numsave.'.gz', $logpath.'/'.$logname.'.'.($numsave+1).'.gz', 0, 1, 0, 0); dol_move($logpath.'/'.$logname.'.'.$numsave.'.gz', $logpath.'/'.$logname.'.'.($numsave+1).'.gz', 0, 1, 0, 0);
} }
} }
// Compress last save // Compress current file and recreate it
if (dol_is_file($logpath.'/'.$logname.'.1')) {
if($nbSaves > 1) { if ($nbSaves > 0) { // If $nbSaves is 1, we keep 1 archive .gz file, If 2, we keep 2 .gz files
$gzfilehandle = gzopen($logpath.'/'.$logname.'.2.gz', 'wb9'); $gzfilehandle = gzopen($logpath.'/'.$logname.'.1.gz', 'wb9');
if (empty($gzfilehandle)) { 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; return -3;
} }
$sourcehandle = fopen($logpath.'/'.$logname.'.1', 'r'); $sourcehandle = fopen($logpath.'/'.$logname, 'r');
if (empty($sourcehandle)) { if (empty($sourcehandle)) {
$this->error = 'Failed to open file '.$logpath.'/'.$logname.'.1'; $this->error = 'Failed to open file '.$logpath.'/'.$logname;
return -4; return -4;
} }
@ -767,19 +770,18 @@ class Utils
fclose($sourcehandle); fclose($sourcehandle);
gzclose($gzfilehandle); gzclose($gzfilehandle);
} else {
dol_delete_file($logpath.'/'.$logname.'.1'); @chmod($logpath.'/'.$logname.'.1.gz', octdec(empty($conf->global->MAIN_UMASK)?'0664':$conf->global->MAIN_UMASK));
}
} }
// Compress current file et recreate it dol_delete_file($logpath.'/'.$logname, 0, 0, 0, null, false, 0);
if (dol_is_file($logpath.'/'.$logname)) { // Create empty file
if (dol_move($logpath.'/'.$logname, $logpath.'/'.$logname.'.1', 0, 1, 0, 0))
{
$newlog = fopen($logpath.'/'.$logname, 'a+'); $newlog = fopen($logpath.'/'.$logname, 'a+');
fclose($newlog); 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 int $nohook Disable all hooks
* @param object $object Current object in use * @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 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 * @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 * @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 $db, $conf, $user, $langs;
global $hookmanager; 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); $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); 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'; include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';