Prepare code to match cron rule: 0 if ok, !=0 if error

This commit is contained in:
Laurent Destailleur 2016-01-31 13:42:49 +01:00
parent 26cd0ac0c6
commit 426e9297ea
5 changed files with 49 additions and 30 deletions

View File

@ -50,10 +50,9 @@ if ($action=='purge' && ! preg_match('/^confirm/i',$choice) && ($choice != 'allf
{
require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
$utils = new Utils($db);
$count = $utils->purgeFiles($choice);
if ($count) $mesg=$langs->trans("PurgeNDirectoriesDeleted", $count);
else $mesg=$langs->trans("PurgeNothingToDelete");
$result = $utils->purgeFiles($choice);
$mesg = $utils->output;
setEventMessages($mesg, null, 'mesgs');
}

View File

@ -44,11 +44,11 @@ class Utils
* Purge files into directory of data files.
*
* @param string $choice Choice of purge mode ('tempfiles', 'tempfilesold' to purge temp older than 24h, 'allfiles', 'logfiles')
* @return int Nb of files deleted
* @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)
*/
function purgeFiles($choice='tempfilesold')
{
global $conf, $dolibarr_main_data_root;
global $conf, $langs, $dolibarr_main_data_root;
dol_syslog("Utils::purgeFiles choice=".$choice, LOG_DEBUG);
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
@ -123,7 +123,11 @@ class Utils
$result = $ecmdirstatic->refreshcachenboffile(1);
}
}
return $count;
if ($count > 0) $this->output=$langs->trans("PurgeNDirectoriesDeleted", $count);
else $this->output=$langs->trans("PurgeNothingToDelete");
//return $count;
return 0; // This function can be called by cron so must return 0 if OK
}
}

View File

@ -1176,6 +1176,8 @@ function dol_delete_file($file,$disableglob=0,$nophperrors=0,$nohook=0,$object=n
else $ok=unlink($filename);
if ($ok) dol_syslog("Removed file ".$filename, LOG_DEBUG);
else dol_syslog("Failed to remove file ".$filename, LOG_WARNING);
// TODO Failure to remove can be because file was already removed or because of permission
// If error because of not exists, we must can return true but we should return false if this is a permission problem
}
}
else dol_syslog("No files to delete found", LOG_WARNING);
@ -1186,7 +1188,7 @@ function dol_delete_file($file,$disableglob=0,$nophperrors=0,$nohook=0,$object=n
if ($nophperrors) $ok=@unlink($file_osencoded);
else $ok=unlink($file_osencoded);
if ($ok) dol_syslog("Removed file ".$file_osencoded, LOG_DEBUG);
else dol_syslog("Failed to remove file ".$file_osencoded, LOG_WARNING);
else dol_syslog("Failed to remove file ".$file_osencoded, LOG_WARNING);
}
return $ok;

View File

@ -829,10 +829,10 @@ class Cronjob extends CommonObject
/**
* Run a job.
* Once job is finished, status and nb of of run is updated.
* Once job is finished, status and nb of run is updated.
* This function does not plan the next run. This is done by function ->reprogram_jobs
*
* @param string $userlogin User login
* @param string $userlogin User login
* @return int <0 if KO, >0 if OK
*/
function run_jobs($userlogin)
@ -947,17 +947,19 @@ class Cronjob extends CommonObject
$result = call_user_func_array(array($object, $this->methodename), $params_arr);
}
if ($result===false)
if ($result===false || $result != 0)
{
dol_syslog(get_class($this)."::run_jobs ".$object->error, LOG_ERR);
$this->lastoutput = $object->error;
$this->lastresult = -1;
$langs->load("errors");
dol_syslog(get_class($this)."::run_jobs result=".$result." error=".$object->error, LOG_ERR);
$this->error = $object->error?$object->error:$langs->trans('ErrorUnknown');
$this->lastoutput = $this->error;
$this->lastresult = is_numeric($result)?$result:-1;
$retval = $this->lastresult;
$error++;
}
else
{
$this->lastoutput='NA';
$this->lastoutput=$object->output;
$this->lastresult=var_export($result,true);
$retval = $this->lastresult;
}
@ -993,13 +995,15 @@ class Cronjob extends CommonObject
$result = call_user_func_array($this->methodename, $params_arr);
}
if ($result === false)
if ($result === false || $result != 0)
{
dol_syslog(get_class($this) . "::run_jobs " . $object->error, LOG_ERR);
$this->lastoutput = $object->error;
$this->lastresult = -1;
$retval = $this->lastresult;
$langs->load("errors");
dol_syslog(get_class($this)."::run_jobs result=".$result, LOG_ERR);
$this->error = $langs->trans('ErrorUnknown');
$this->lastoutput = $this->error;
$this->lastresult = is_numeric($result)?$result:-1;
$retval = $this->lastresult;
$error++;
}
else
{
@ -1024,6 +1028,16 @@ class Cronjob extends CommonObject
if ($execmethod == 1)
{
exec($command, $output_arr, $retval);
if ($retval != 0)
{
$langs->load("errors");
dol_syslog(get_class($this)."::run_jobs retval=".$retval, LOG_ERR);
$this->error = 'Error '.$retval;
$this->lastoutput = ''; // Will be filled later
$this->lastresult = $retval;
$retval = $this->lastresult;
$error++;
}
}
if ($execmethod == 2)
{
@ -1046,7 +1060,7 @@ class Cronjob extends CommonObject
}
}
dol_syslog(get_class($this)."::run_jobs output_arr:".var_export($output_arr,true), LOG_DEBUG);
dol_syslog(get_class($this)."::run_jobs output_arr:".var_export($output_arr,true)." lastoutput=".$this->lastoutput." lastresult=".$this->lastresult, LOG_DEBUG);
// Update with result
if (is_array($output_arr) && count($output_arr)>0)

View File

@ -99,7 +99,7 @@ if ($action == 'confirm_execute' && $confirm == "yes" && $user->rights->cron->ex
$now = dol_now(); // Date we start
$resrunjob = $object->run_jobs($user->login);
$resrunjob = $object->run_jobs($user->login); // Return -1 if KO, 1 if OK
if ($resrunjob < 0) {
setEventMessages($object->error, $object->errors, 'errors');
}
@ -110,8 +110,8 @@ if ($action == 'confirm_execute' && $confirm == "yes" && $user->rights->cron->ex
{
if ($resrunjob >= 0) // We add result of reprogram ony if no error message already reported
{
if ($object->lastresult > 0) setEventMessages($langs->trans("JobFinished"), null, 'warnings');
else setEventMessages($langs->trans("JobFinished"), null, 'mesgs');
if ($object->lastresult >= 0) setEventMessages($langs->trans("JobFinished"), null, 'mesgs');
else setEventMessages($langs->trans("JobFinished"), null, 'errors');
}
$action='';
}
@ -252,15 +252,15 @@ if ($num > 0)
$texttoshow.=$langs->trans('CronClass').': '. $line->classesname.'<br>';
$texttoshow.=$langs->trans('CronObject').': '. $line->objectname.'<br>';
$texttoshow.=$langs->trans('CronMethod').': '. $line->methodename;
$texttoshow.='<br>'.$langs->trans('CronArgs').':'. $line->params;
$texttoshow.='<br>'.$langs->trans('Comment').':'. $line->note;
$texttoshow.='<br>'.$langs->trans('CronArgs').': '. $line->params;
$texttoshow.='<br>'.$langs->trans('Comment').': '. $langs->trans($line->note);
}
elseif ($line->jobtype=='command')
{
$text=$langs->trans('CronCommand');
$texttoshow=$langs->trans('CronCommand').': '.dol_trunc($line->command);
$texttoshow.='<br>'.$langs->trans('CronArgs').':'. $line->params;
$texttoshow.='<br>'.$langs->trans('Comment').':'. $line->note;
$texttoshow.='<br>'.$langs->trans('CronArgs').': '. $line->params;
$texttoshow.='<br>'.$langs->trans('Comment').': '. $langs->trans($line->note);
}
print $form->textwithpicto($text, $texttoshow, 1);
print '</td>';