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'; require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
$utils = new Utils($db); $utils = new Utils($db);
$count = $utils->purgeFiles($choice); $result = $utils->purgeFiles($choice);
if ($count) $mesg=$langs->trans("PurgeNDirectoriesDeleted", $count); $mesg = $utils->output;
else $mesg=$langs->trans("PurgeNothingToDelete");
setEventMessages($mesg, null, 'mesgs'); setEventMessages($mesg, null, 'mesgs');
} }

View File

@ -44,11 +44,11 @@ class Utils
* Purge files into directory of data files. * Purge files into directory of data files.
* *
* @param string $choice Choice of purge mode ('tempfiles', 'tempfilesold' to purge temp older than 24h, 'allfiles', 'logfiles') * @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') 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); dol_syslog("Utils::purgeFiles choice=".$choice, LOG_DEBUG);
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
@ -124,6 +124,10 @@ class Utils
} }
} }
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); else $ok=unlink($filename);
if ($ok) dol_syslog("Removed file ".$filename, LOG_DEBUG); if ($ok) dol_syslog("Removed file ".$filename, LOG_DEBUG);
else dol_syslog("Failed to remove file ".$filename, LOG_WARNING); 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); else dol_syslog("No files to delete found", LOG_WARNING);

View File

@ -829,7 +829,7 @@ class Cronjob extends CommonObject
/** /**
* Run a job. * 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 * 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
@ -947,17 +947,19 @@ class Cronjob extends CommonObject
$result = call_user_func_array(array($object, $this->methodename), $params_arr); $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); $langs->load("errors");
$this->lastoutput = $object->error; dol_syslog(get_class($this)."::run_jobs result=".$result." error=".$object->error, LOG_ERR);
$this->lastresult = -1; $this->error = $object->error?$object->error:$langs->trans('ErrorUnknown');
$this->lastoutput = $this->error;
$this->lastresult = is_numeric($result)?$result:-1;
$retval = $this->lastresult; $retval = $this->lastresult;
$error++; $error++;
} }
else else
{ {
$this->lastoutput='NA'; $this->lastoutput=$object->output;
$this->lastresult=var_export($result,true); $this->lastresult=var_export($result,true);
$retval = $this->lastresult; $retval = $this->lastresult;
} }
@ -993,13 +995,15 @@ class Cronjob extends CommonObject
$result = call_user_func_array($this->methodename, $params_arr); $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); $langs->load("errors");
dol_syslog(get_class($this)."::run_jobs result=".$result, LOG_ERR);
$this->lastoutput = $object->error; $this->error = $langs->trans('ErrorUnknown');
$this->lastresult = -1; $this->lastoutput = $this->error;
$this->lastresult = is_numeric($result)?$result:-1;
$retval = $this->lastresult; $retval = $this->lastresult;
$error++;
} }
else else
{ {
@ -1024,6 +1028,16 @@ class Cronjob extends CommonObject
if ($execmethod == 1) if ($execmethod == 1)
{ {
exec($command, $output_arr, $retval); 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) 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 // Update with result
if (is_array($output_arr) && count($output_arr)>0) 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 $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) { if ($resrunjob < 0) {
setEventMessages($object->error, $object->errors, 'errors'); 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 ($resrunjob >= 0) // We add result of reprogram ony if no error message already reported
{ {
if ($object->lastresult > 0) setEventMessages($langs->trans("JobFinished"), null, 'warnings'); if ($object->lastresult >= 0) setEventMessages($langs->trans("JobFinished"), null, 'mesgs');
else setEventMessages($langs->trans("JobFinished"), null, 'mesgs'); else setEventMessages($langs->trans("JobFinished"), null, 'errors');
} }
$action=''; $action='';
} }
@ -252,15 +252,15 @@ if ($num > 0)
$texttoshow.=$langs->trans('CronClass').': '. $line->classesname.'<br>'; $texttoshow.=$langs->trans('CronClass').': '. $line->classesname.'<br>';
$texttoshow.=$langs->trans('CronObject').': '. $line->objectname.'<br>'; $texttoshow.=$langs->trans('CronObject').': '. $line->objectname.'<br>';
$texttoshow.=$langs->trans('CronMethod').': '. $line->methodename; $texttoshow.=$langs->trans('CronMethod').': '. $line->methodename;
$texttoshow.='<br>'.$langs->trans('CronArgs').':'. $line->params; $texttoshow.='<br>'.$langs->trans('CronArgs').': '. $line->params;
$texttoshow.='<br>'.$langs->trans('Comment').':'. $line->note; $texttoshow.='<br>'.$langs->trans('Comment').': '. $langs->trans($line->note);
} }
elseif ($line->jobtype=='command') elseif ($line->jobtype=='command')
{ {
$text=$langs->trans('CronCommand'); $text=$langs->trans('CronCommand');
$texttoshow=$langs->trans('CronCommand').': '.dol_trunc($line->command); $texttoshow=$langs->trans('CronCommand').': '.dol_trunc($line->command);
$texttoshow.='<br>'.$langs->trans('CronArgs').':'. $line->params; $texttoshow.='<br>'.$langs->trans('CronArgs').': '. $line->params;
$texttoshow.='<br>'.$langs->trans('Comment').':'. $line->note; $texttoshow.='<br>'.$langs->trans('Comment').': '. $langs->trans($line->note);
} }
print $form->textwithpicto($text, $texttoshow, 1); print $form->textwithpicto($text, $texttoshow, 1);
print '</td>'; print '</td>';