Debug the cron module

This commit is contained in:
Laurent Destailleur 2015-12-19 03:27:55 +01:00
parent a973c91c24
commit a4edd90320
6 changed files with 93 additions and 63 deletions

View File

@ -317,7 +317,7 @@ if (($action=="create") || ($action=="edit"))
print "</tr>\n"; print "</tr>\n";
print "<tr><td>"; print "<tr><td>";
print $langs->trans('CronHourStart')."</td><td>"; print $langs->trans('CronDtStart')."</td><td>";
if(!empty($object->datestart)) if(!empty($object->datestart))
{ {
$form->select_date($object->datestart,'datestart',1,1,'',"cronform"); $form->select_date($object->datestart,'datestart',1,1,'',"cronform");
@ -518,13 +518,20 @@ else
print "</td></tr>"; print "</td></tr>";
print "<tr><td>"; print "<tr><td>";
print $langs->trans('CronHourStart')."</td><td>"; print $langs->trans('CronDtStart')."</td><td>";
if(!empty($object->datestart)) {print dol_print_date($object->datestart,'dayhourtext');} else {print $langs->trans('CronNone');} if(!empty($object->datestart)) {print dol_print_date($object->datestart,'dayhourtext');}
print "</td></tr>"; print "</td></tr>";
print "<tr><td>"; print "<tr><td>";
print $langs->trans('CronDtEnd')."</td><td>"; print $langs->trans('CronDtEnd')."</td><td>";
if(!empty($object->dateend)) {print dol_print_date($object->dateend,'dayhourtext');} else {print $langs->trans('CronNone');} if(!empty($object->dateend)) {print dol_print_date($object->dateend,'dayhourtext');}
print "</td></tr>";
print '<tr><td>';
print $langs->trans('CronDtNextLaunch');
print ' ('.$langs->trans('CronFrom').')';
print "</td><td>";
if(!empty($object->datenextrun)) {print dol_print_date($object->datenextrun,'dayhoursec');} else {print $langs->trans('CronNone');}
print "</td></tr>"; print "</td></tr>";
print "<tr><td>"; print "<tr><td>";
@ -591,23 +598,17 @@ else
print '<br>'; print '<br>';
print '<table class="border" width="100%">'; print '<table class="border" width="100%">';
print '<tr><td width="30%">'; print '<tr><td width="30%">';
print $langs->trans('CronDtLastLaunch')."</td><td>"; print $langs->trans('CronDtLastLaunch')."</td><td>";
if(!empty($object->datelastrun)) {print dol_print_date($object->datelastrun,'dayhourtext');} else {print $langs->trans('CronNone');} if(!empty($object->datelastrun)) {print dol_print_date($object->datelastrun,'dayhoursec');} else {print $langs->trans('CronNone');}
print "</td></tr>";
print '<tr><td>';
print $langs->trans('CronDtNextLaunch');
print ' ('.$langs->trans('CronFrom').')';
print "</td><td>";
if(!empty($object->datenextrun)) {print dol_print_date($object->datenextrun,'dayhourtext');} else {print $langs->trans('CronNone');}
print "</td></tr>"; print "</td></tr>";
print '<tr><td>'; print '<tr><td>';
print $langs->trans('CronDtLastResult')."</td><td>"; print $langs->trans('CronDtLastResult')."</td><td>";
if(!empty($object->datelastresult)) {print dol_print_date($object->datelastresult,'dayhourtext');} else {print $langs->trans('CronNone');} if(!empty($object->datelastresult)) {print dol_print_date($object->datelastresult,'dayhoursec');} else {print $langs->trans('CronNone');}
print "</td></tr>"; print "</td></tr>";
print '<tr><td>'; print '<tr><td>';

View File

@ -521,9 +521,6 @@ class Cronjob extends CommonObject
// Check parameters // Check parameters
// Put here code to add a control on parameters values // Put here code to add a control on parameters values
if (empty($this->status)) {
$this->dateend=dol_now();
}
if (dol_strlen($this->datestart)==0) { if (dol_strlen($this->datestart)==0) {
$this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronDtStart')); $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronDtStart'));
$error++; $error++;
@ -830,7 +827,9 @@ class Cronjob extends CommonObject
/** /**
* Run a job * Run a job.
* Once job is finished, status and nb of 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 * @return int <0 if KO, >0 if OK
@ -843,7 +842,8 @@ class Cronjob extends CommonObject
$langs->load('cron'); $langs->load('cron');
if (empty($userlogin)) { if (empty($userlogin))
{
$this->error="User login is mandatory"; $this->error="User login is mandatory";
dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
return -1; return -1;
@ -891,7 +891,7 @@ class Cronjob extends CommonObject
$this->datelastrun=$now; $this->datelastrun=$now;
$this->lastoutput=''; $this->lastoutput='';
$this->lastresult=''; $this->lastresult='';
$this->nbrun=$this->nbrun+1; $this->nbrun=$this->nbrun + 1;
$result = $this->update($user); $result = $this->update($user);
if ($result<0) { if ($result<0) {
dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
@ -1055,23 +1055,28 @@ class Cronjob extends CommonObject
/** /**
* Reprogram a job * Reprogram a job
* *
* @param string $userlogin User login * @param string $userlogin User login
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*
*/ */
function reprogram_jobs($userlogin) function reprogram_jobs($userlogin)
{ {
dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG); dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG);
$now = dol_now();
require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
$user=new User($this->db); $user=new User($this->db);
$result=$user->fetch('',$userlogin); $result=$user->fetch('',$userlogin);
if ($result<0) { if ($result<0)
{
$this->error="User Error:".$user->error; $this->error="User Error:".$user->error;
dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR); dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
return -1; return -1;
}else { }
if (empty($user->id)) { else
{
if (empty($user->id))
{
$this->error=" User user login:".$userlogin." do not exists"; $this->error=" User user login:".$userlogin." do not exists";
dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR); dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
return -1; return -1;
@ -1080,17 +1085,41 @@ class Cronjob extends CommonObject
dol_syslog(get_class($this)."::reprogram_jobs ", LOG_DEBUG); dol_syslog(get_class($this)."::reprogram_jobs ", LOG_DEBUG);
if (empty($this->datenextrun)) {
$this->datenextrun=dol_now()+$this->frequency; if (empty($this->datenextrun))
} else { {
if ($this->datenextrun<dol_now()) { $this->datenextrun = $now + $this->frequency;
$this->datenextrun=dol_now()+$this->frequency; }
} else { else
$this->datenextrun=$this->datenextrun+$this->frequency; {
if ($this->datenextrun < $now && $this->frequency > 0)
{
// Loop until date is after future
while ($this->datenextrun < $now)
{
$this->datenextrun += $this->frequency;
}
}
else
{
//$this->datenextrun=$this->datenextrun+$this->frequency;
} }
} }
// Archive job
if ($this->autodelete == 2)
{
if (($this->maxrun > 0 && ($this->nbrun >= $this->maxrun))
|| ($this->dateend && ($this->datenextrun > $this->dateend)))
{
$this->status = 2;
dol_syslog(get_class($this)."::reprogram_jobs Job must be set to archived", LOG_ERR);
}
}
$result = $this->update($user); $result = $this->update($user);
if ($result<0) { if ($result<0)
{
dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR); dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR);
return -1; return -1;
} }

View File

@ -173,14 +173,14 @@ print '<tr class="liste_titre">';
print_liste_field_titre($langs->trans("ID"),$_SERVER["PHP_SELF"],"t.rowid","",$param,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("ID"),$_SERVER["PHP_SELF"],"t.rowid","",$param,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CronLabel"),$_SERVER["PHP_SELF"],"t.label","",$param,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("CronLabel"),$_SERVER["PHP_SELF"],"t.label","",$param,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CronTask"),'','',"",$param,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("CronTask"),'','',"",$param,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CronDtStart"),$_SERVER["PHP_SELF"],"t.datestart","",$param,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("CronDtStart"),$_SERVER["PHP_SELF"],"t.datestart","",$param,'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CronDtEnd"),$_SERVER["PHP_SELF"],"t.dateend","",$param,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("CronDtEnd"),$_SERVER["PHP_SELF"],"t.dateend","",$param,'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CronDtLastLaunch"),$_SERVER["PHP_SELF"],"t.datelastrun","",$param,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("CronDtNextLaunch"),$_SERVER["PHP_SELF"],"t.datenextrun","",$param,'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CronDtNextLaunch"),$_SERVER["PHP_SELF"],"t.datenextrun","",$param,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CronFrequency"),'',"","",$param,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("CronFrequency"),'',"","",$param,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CronNbRun"),$_SERVER["PHP_SELF"],"t.nbrun","",$param,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("CronMaxRun"),$_SERVER["PHP_SELF"],"t.maxrun","",$param,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CronMaxRun"),$_SERVER["PHP_SELF"],"t.maxrun","",$param,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("CronNbRun"),$_SERVER["PHP_SELF"],"t.nbrun","",$param,'align="right"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CronLastResult"),$_SERVER["PHP_SELF"],"t.lastresult","",$param,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("CronDtLastLaunch"),$_SERVER["PHP_SELF"],"t.datelastrun","",$param,'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CronLastResult"),$_SERVER["PHP_SELF"],"t.lastresult","",$param,'align="center"',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("CronLastOutput"),$_SERVER["PHP_SELF"],"t.lastoutput","",$param,'',$sortfield,$sortorder); print_liste_field_titre($langs->trans("CronLastOutput"),$_SERVER["PHP_SELF"],"t.lastoutput","",$param,'',$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"t.status","",$param,'align="center"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"t.status","",$param,'align="center"',$sortfield,$sortorder);
print_liste_field_titre(''); print_liste_field_titre('');
@ -259,19 +259,15 @@ if ($num > 0)
} }
print '</td>'; print '</td>';
print '<td>'; print '<td class="center">';
if(!empty($line->datestart)) {print dol_print_date($line->datestart,'dayhour');} if(!empty($line->datestart)) {print dol_print_date($line->datestart,'dayhour');}
print '</td>'; print '</td>';
print '<td>'; print '<td class="center">';
if(!empty($line->dateend)) {print dol_print_date($line->dateend,'dayhour');} if(!empty($line->dateend)) {print dol_print_date($line->dateend,'dayhour');}
print '</td>'; print '</td>';
print '<td>'; print '<td class="center">';
if(!empty($line->datelastrun)) {print dol_print_date($line->datelastrun,'dayhour');}
print '</td>';
print '<td>';
if(!empty($line->datenextrun)) {print dol_print_date($line->datenextrun,'dayhour');} if(!empty($line->datenextrun)) {print dol_print_date($line->datenextrun,'dayhour');}
print '</td>'; print '</td>';
@ -282,15 +278,19 @@ if ($num > 0)
if($line->unitfrequency == "604800") print $langs->trans('CronEach')." ".($line->frequency/$line->unitfrequency)." ".$langs->trans('Weeks'); if($line->unitfrequency == "604800") print $langs->trans('CronEach')." ".($line->frequency/$line->unitfrequency)." ".$langs->trans('Weeks');
print '</td>'; print '</td>';
print '<td>'; print '<td align="right">';
if (!empty($line->nbrun)) {print $line->nbrun;} else {print '0';}
print '</td>';
print '<td>';
if (!empty($line->maxrun)) {print $line->maxrun;} if (!empty($line->maxrun)) {print $line->maxrun;}
print '</td>'; print '</td>';
print '<td>'; print '<td align="right">';
if (!empty($line->nbrun)) {print $line->nbrun;} else {print '0';}
print '</td>';
print '<td class="center">';
if(!empty($line->datelastrun)) {print dol_print_date($line->datelastrun,'dayhour');}
print '</td>';
print '<td class="center">';
if(!empty($line->lastresult)) {print dol_trunc($line->lastresult);} if(!empty($line->lastresult)) {print dol_trunc($line->lastresult);}
print '</td>'; print '</td>';

View File

@ -33,18 +33,18 @@ CREATE TABLE llx_cronjob
md5params varchar(32), md5params varchar(32),
module_name varchar(255), module_name varchar(255),
priority integer DEFAULT 0, priority integer DEFAULT 0,
datelastrun datetime, -- date last run and when should be next datelastrun datetime, -- date last run start (see datelastresult for end with a result)
datenextrun datetime, -- job will be run if current date higher that this date datenextrun datetime, -- job will be run if current date higher that this date
datestart datetime, -- before this date no jobs will be run datestart datetime, -- before this date no jobs will be run
dateend datetime, -- after this date, no more jobs will be run dateend datetime, -- after this date, no more jobs will be run
datelastresult datetime, datelastresult datetime, -- date last run end
lastresult text, lastresult text,
lastoutput text, lastoutput text,
unitfrequency integer NOT NULL DEFAULT 0, unitfrequency integer NOT NULL DEFAULT 0,
frequency integer NOT NULL DEFAULT 0, frequency integer NOT NULL DEFAULT 0,
maxrun integer NOT NULL DEFAULT 0, -- set this to 1 for a job queued for on run only maxrun integer NOT NULL DEFAULT 0, -- set this to 1 for a job queued for on run only
nbrun integer, -- nb of run complete (failed or not) nbrun integer, -- nb of run complete (failed or not)
autodelete integer DEFAULT 0, -- 1=Job must be deleted once finished, 2=Job must be archived once finished (archive = status 2) autodelete integer DEFAULT 0, -- 0=Job is kept unchanged once nbrun > maxrun or date > dateend, 2=Job must be archived (archive = status 2) once nbrun > maxrun or date > dateend
status integer NOT NULL DEFAULT 1, -- 0=disabled, 1=enabled, 2=archived status integer NOT NULL DEFAULT 1, -- 0=disabled, 1=enabled, 2=archived
fk_user_author integer DEFAULT NULL, fk_user_author integer DEFAULT NULL,
fk_user_mod integer DEFAULT NULL, fk_user_mod integer DEFAULT NULL,

View File

@ -36,8 +36,8 @@ CronInfo=Scheduled job module allow to execute job that have been planned
CronWaitingJobs=Waiting jobs CronWaitingJobs=Waiting jobs
CronTask=Job CronTask=Job
CronNone=None CronNone=None
CronDtStart=Start date CronDtStart=Not before
CronDtEnd=End date CronDtEnd=Not after
CronDtNextLaunch=Next execution CronDtNextLaunch=Next execution
CronDtLastLaunch=Last execution CronDtLastLaunch=Last execution
CronFrequency=Frequency CronFrequency=Frequency

View File

@ -1,8 +1,8 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
/* Copyright (C) 2012 Nicolas Villa aka Boyquotes http://informetic.fr /* Copyright (C) 2012 Nicolas Villa aka Boyquotes http://informetic.fr
* Copyright (C) 2013 Florian Henry <forian.henry@open-concept.pro * Copyright (C) 2013 Florian Henry <forian.henry@open-concept.pro
* Copyright (C) 2013 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2013-2015 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -147,7 +147,7 @@ if(is_array($object->lines) && (count($object->lines)>0))
exit(-1); exit(-1);
} }
// we re-program the next execution and stores the last execution time for this job // we re-program the next execution and stores the last execution time for this job
$result=$cronjob->reprogram_jobs($userlogin); $result=$cronjob->reprogram_jobs($userlogin);
if ($result<0) { if ($result<0) {
echo "Error:".$cronjob->error; echo "Error:".$cronjob->error;