FIX Support or multicompany for sheduled jobs

FIX Use priority to define order of sheduled jobs

Conflicts:
	htdocs/cron/list.php
This commit is contained in:
Laurent Destailleur 2018-04-05 12:28:52 +02:00
parent 88b3e08b22
commit 0223d54107
6 changed files with 55 additions and 23 deletions

View File

@ -1313,6 +1313,7 @@ class DolibarrModules // Can not be abstract, because we need to insta
{
foreach ($this->cronjobs as $key => $value)
{
$entity = isset($this->cronjobs[$key]['entity'])?$this->cronjobs[$key]['entity']:$conf->entity;
$label = isset($this->cronjobs[$key]['label'])?$this->cronjobs[$key]['label']:'';
$jobtype = isset($this->cronjobs[$key]['jobtype'])?$this->cronjobs[$key]['jobtype']:'';
$class = isset($this->cronjobs[$key]['class'])?$this->cronjobs[$key]['class']:'';
@ -1334,7 +1335,7 @@ class DolibarrModules // Can not be abstract, because we need to insta
if ($objectname) $sql.= " AND objectname = '".$this->db->escape($objectname)."'";
if ($method) $sql.= " AND methodename = '".$this->db->escape($method)."'";
if ($command) $sql.= " AND command = '".$this->db->escape($command)."'";
$sql.= " AND entity = ".$conf->entity;
$sql.= " AND entity = ".$entity; // Must be exact entity
$now=dol_now();
@ -1371,7 +1372,7 @@ class DolibarrModules // Can not be abstract, because we need to insta
if(is_int($unitfrequency)){ $sql.= "'".$this->db->escape($unitfrequency)."', "; }
if(is_int($priority)) {$sql.= "'".$this->db->escape($priority)."', ";}
if(is_int($status)){ $sql.= "'".$this->db->escape($status)."', "; }
$sql.= $conf->entity.",";
$sql.= $entity.",";
$sql.= "'".$this->db->escape($test)."'";
$sql.= ")";

View File

@ -97,9 +97,9 @@ class modCron extends DolibarrModules
// Cronjobs
$this->cronjobs = array(
0=>array('label'=>'PurgeDeleteTemporaryFilesShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'purgeFiles', 'parameters'=>'', 'comment'=>'PurgeDeleteTemporaryFiles', 'frequency'=>2, 'unitfrequency'=>3600 * 24 * 7, 'priority'=>50, 'status'=>1, 'test'=>true),
1=>array('label'=>'MakeLocalDatabaseDumpShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'dumpDatabase', 'parameters'=>'none,auto,1,auto,10', 'comment'=>'MakeLocalDatabaseDump', 'frequency'=>1, 'unitfrequency'=>3600 * 24 * 7, 'priority'=>90, 'status'=>0, 'test'=>in_array($db->type, array('mysql', 'mysqli'))),
// 1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>1, 'unitfrequency'=>3600*24)
0=>array('entity'=>0, 'label'=>'PurgeDeleteTemporaryFilesShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'purgeFiles', 'parameters'=>'', 'comment'=>'PurgeDeleteTemporaryFiles', 'frequency'=>2, 'unitfrequency'=>3600 * 24 * 7, 'priority'=>50, 'status'=>1, 'test'=>true),
1=>array('entity'=>0, 'label'=>'MakeLocalDatabaseDumpShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'dumpDatabase', 'parameters'=>'none,auto,1,auto,10', 'comment'=>'MakeLocalDatabaseDump', 'frequency'=>1, 'unitfrequency'=>3600 * 24 * 7, 'priority'=>90, 'status'=>0, 'test'=>in_array($db->type, array('mysql', 'mysqli'))),
// 1=>array('entity'=>0, 'label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>1, 'unitfrequency'=>3600*24)
);
// Permissions

View File

@ -614,6 +614,22 @@ else
print $langs->trans($object->note);
print "</td></tr>";
if (! empty($conf->multicompany->enabled))
{
print '<tr><td>';
print $langs->trans('Entity')."</td><td>";
if (! $object->entity)
{
print $langs->trans("AllEntities");
}
else
{
$mc->getInfo($obj->entity);
print $mc->label;
}
print "</td></tr>";
}
print '</table>';
print '</div>';

View File

@ -34,6 +34,7 @@ class Cronjob extends CommonObject
public $table_element='cronjob'; //!< Name of table without prefix where object is stored
public $picto = 'cron';
public $entity;
public $jobtype;
public $tms='';
public $datec='';
@ -153,7 +154,7 @@ class Cronjob extends CommonObject
// Insert request
$sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob(";
$sql.= "entity,";
$sql.= "datec,";
$sql.= "jobtype,";
$sql.= "label,";
@ -183,6 +184,7 @@ class Cronjob extends CommonObject
$sql.= "libname,";
$sql.= "test";
$sql.= ") VALUES (";
$sql.= " ".(! isset($this->entity)?$conf->entity:$this->db->escape($this->entity)).",";
$sql.= " '".$this->db->idate($now)."',";
$sql.= " ".(! isset($this->jobtype)?'NULL':"'".$this->db->escape($this->jobtype)."'").",";
$sql.= " ".(! isset($this->label)?'NULL':"'".$this->db->escape($this->label)."'").",";
@ -265,8 +267,8 @@ class Cronjob extends CommonObject
function fetch($id)
{
$sql = "SELECT";
$sql.= " t.rowid,";
$sql.= " t.rowid,";
$sql.= " t.entity,";
$sql.= " t.tms,";
$sql.= " t.datec,";
$sql.= " t.jobtype,";
@ -310,7 +312,7 @@ class Cronjob extends CommonObject
$this->id = $obj->rowid;
$this->ref = $obj->rowid;
$this->entity = $obj->entity;
$this->tms = $this->db->jdate($obj->tms);
$this->datec = $this->db->jdate($obj->datec);
$this->label = $obj->label;
@ -417,7 +419,7 @@ class Cronjob extends CommonObject
}
}
$sql.= " ORDER BY $sortfield $sortorder ";
$sql.= $this->db->order($sortfield,$sortorder);
if (!empty($limit) && !empty($offset)) {
$sql.= $this->db->plimit($limit + 1,$offset);
}
@ -445,7 +447,6 @@ class Cronjob extends CommonObject
$line->id = $obj->rowid;
$line->ref = $obj->rowid;
$line->entity = $obj->entity;
$line->tms = $this->db->jdate($obj->tms);
$line->datec = $this->db->jdate($obj->datec);
@ -575,7 +576,7 @@ class Cronjob extends CommonObject
// Update request
$sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET";
$sql.= " entity=".(isset($this->entity)?$this->db->escape($this->entity):$conf->entity).",";
$sql.= " label=".(isset($this->label)?"'".$this->db->escape($this->label)."'":"null").",";
$sql.= " jobtype=".(isset($this->jobtype)?"'".$this->db->escape($this->jobtype)."'":"null").",";
$sql.= " command=".(isset($this->command)?"'".$this->db->escape($this->command)."'":"null").",";
@ -773,7 +774,7 @@ class Cronjob extends CommonObject
{
$this->id=0;
$this->ref=0;
$this->entity=0;
$this->tms='';
$this->datec='';
$this->label='';
@ -930,6 +931,16 @@ class Cronjob extends CommonObject
return -1;
}
// Force the environment of running to the environment declared for job, so jobs launched from command line will run into correct environment
// When job is ran from GUI, the environment should already be same, except if job has entity 0 (visible into all environments)
if ($conf->entity != $this->entity && $this->entity > 0)
{
dol_syslog("We try to run a job in entity ".$this->entity." when we are in entity ".$conf->entity, LOG_WARNING);
}
$savcurrententity = $conf->entity;
$conf->entity = $this->entity;
dol_syslog(get_class($this)."::run_jobs entity for running job is ".$conf->entity);
require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
$user=new User($this->db);
$result=$user->fetch('',$userlogin);
@ -937,6 +948,7 @@ class Cronjob extends CommonObject
{
$this->error="User Error:".$user->error;
dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
$conf->entity = $savcurrententity;
return -1;
}
else
@ -945,6 +957,7 @@ class Cronjob extends CommonObject
{
$this->error=" User user login:".$userlogin." do not exists";
dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
$conf->entity = $savcurrententity;
return -1;
}
}
@ -976,6 +989,7 @@ class Cronjob extends CommonObject
$result = $this->update($user); // This include begin/commit
if ($result<0) {
dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
$conf->entity = $savcurrententity;
return -1;
}
@ -1073,6 +1087,7 @@ class Cronjob extends CommonObject
{
$this->error = $langs->trans('CronCannotLoadLib') . ': ' . $libpath;
dol_syslog(get_class($this) . "::run_jobs " . $this->error, LOG_ERR);
$conf->entity = $savcurrententity;
return -1;
}
// Load langs
@ -1080,6 +1095,7 @@ class Cronjob extends CommonObject
if ($result<0)
{
dol_syslog(get_class($this) . "::run_jobs Cannot load module langs" . $langs->error, LOG_ERR);
$conf->entity = $savcurrententity;
return -1;
}
dol_syslog(get_class($this) . "::run_jobs " . $this->libname . "::" . $this->methodename."(" . $this->params . ");", LOG_DEBUG);
@ -1142,13 +1158,12 @@ class Cronjob extends CommonObject
if ($result < 0)
{
dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
$conf->entity = $savcurrententity;
return -1;
}
else
{
return $error?-1:1;
}
$conf->entity = $savcurrententity;
return $error?-1:1;
}

View File

@ -187,7 +187,7 @@ $sql.= " t.nbrun,";
$sql.= " t.libname,";
$sql.= " t.test";
$sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t";
$sql.= " WHERE 1 = 1";
$sql.= " WHERE entity IN (0,".$conf->entity.")";
if ($status >= 0 && $status < 2) $sql.= " AND t.status = ".(empty($status)?'0':'1');
if ($status == 2) $sql.= " AND t.status = 2";
//Manage filter

View File

@ -85,7 +85,7 @@ if ($key != $conf->global->CRON_KEY)
// If param userlogin is reserved word 'firstadmin'
if ($userlogin == 'firstadmin')
{
$sql='SELECT login from '.MAIN_DB_PREFIX.'user WHERE admin = 1 and statut = 1 ORDER BY entity LIMIT 1';
$sql='SELECT login, entity from '.MAIN_DB_PREFIX.'user WHERE admin = 1 and statut = 1 ORDER BY entity LIMIT 1';
$resql=$db->query($sql);
if ($resql)
{
@ -93,7 +93,7 @@ if ($userlogin == 'firstadmin')
if ($obj)
{
$userlogin = $obj->login;
echo "First admin user found is login '".$userlogin."'\n";
echo "First admin user found is login '".$userlogin."', entity ".$obj->entity."\n";
}
}
else dol_print_error($db);
@ -132,7 +132,7 @@ if (! empty($id)) {
$filter['t.rowid']=$id;
}
$result = $object->fetch_all('DESC','t.rowid', 0, 0, 1, $filter, 0);
$result = $object->fetch_all('ASC,ASC,ASC','t.priority,t.entity,t.rowid', 0, 0, 1, $filter, 0);
if ($result<0)
{
echo "Error: ".$object->error;
@ -159,8 +159,8 @@ if(is_array($qualifiedjobs) && (count($qualifiedjobs)>0))
// Loop over job
foreach($qualifiedjobs as $line)
{
dol_syslog("cron_run_jobs.php cronjobid: ".$line->id, LOG_DEBUG);
echo "cron_run_jobs.php cronjobid: ".$line->id."\n";
dol_syslog("cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label, LOG_DEBUG);
echo "cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label."\n";
//If date_next_jobs is less of current date, execute the program, and store the execution time of the next execution in database
if (($line->datenextrun < $now) && (empty($line->datestart) || $line->datestart <= $now) && (empty($line->dateend) || $line->dateend >= $now))