NEW Clean and enhance code for cron engine

This commit is contained in:
Laurent Destailleur 2016-01-31 14:58:04 +01:00
parent 426e9297ea
commit 928336c835
10 changed files with 81 additions and 53 deletions

View File

@ -107,6 +107,10 @@ $dump_buffer_len = 0;
$time_start = time(); $time_start = time();
$outputdir = $conf->admin->dir_output.'/backup';
$result=dol_mkdir($outputdir);
// MYSQL // MYSQL
if ($what == 'mysql') if ($what == 'mysql')
{ {
@ -116,7 +120,6 @@ if ($what == 'mysql')
dolibarr_set_const($db, 'SYSTEMTOOLS_MYSQLDUMP', $cmddump,'chaine',0,'',$conf->entity); dolibarr_set_const($db, 'SYSTEMTOOLS_MYSQLDUMP', $cmddump,'chaine',0,'',$conf->entity);
} }
$outputdir = $conf->admin->dir_output.'/backup';
$outputfile = $outputdir.'/'.$file; $outputfile = $outputdir.'/'.$file;
// for compression format, we add extension // for compression format, we add extension
$compression=GETPOST('compression') ? GETPOST('compression','alpha') : 'none'; $compression=GETPOST('compression') ? GETPOST('compression','alpha') : 'none';
@ -185,8 +188,6 @@ if ($what == 'mysql')
$errormsg=''; $errormsg='';
$result=dol_mkdir($outputdir);
// Debut appel methode execution // Debut appel methode execution
$fullcommandcrypted=$command." ".$paramcrypted." 2>&1"; $fullcommandcrypted=$command." ".$paramcrypted." 2>&1";
$fullcommandclear=$command." ".$paramclear." 2>&1"; $fullcommandclear=$command." ".$paramclear." 2>&1";
@ -254,7 +255,6 @@ if ($what == 'mysql')
if ($what == 'mysqlnobin') if ($what == 'mysqlnobin')
{ {
$outputdir = $conf->admin->dir_output.'/backup';
$outputfile = $outputdir.'/'.$file; $outputfile = $outputdir.'/'.$file;
$outputfiletemp = $outputfile.'-TMP.sql'; $outputfiletemp = $outputfile.'-TMP.sql';
// for compression format, we add extension // for compression format, we add extension
@ -288,7 +288,6 @@ if ($what == 'postgresql')
dolibarr_set_const($db, 'SYSTEMTOOLS_POSTGRESQLDUMP', $cmddump,'chaine',0,'',$conf->entity); dolibarr_set_const($db, 'SYSTEMTOOLS_POSTGRESQLDUMP', $cmddump,'chaine',0,'',$conf->entity);
} }
$outputdir = $conf->admin->dir_output.'/backup';
$outputfile = $outputdir.'/'.$file; $outputfile = $outputdir.'/'.$file;
// for compression format, we add extension // for compression format, we add extension
$compression=GETPOST('compression') ? GETPOST('compression','alpha') : 'none'; $compression=GETPOST('compression') ? GETPOST('compression','alpha') : 'none';
@ -299,7 +298,7 @@ if ($what == 'postgresql')
// Parameteres execution // Parameteres execution
$command=$cmddump; $command=$cmddump;
if (preg_match("/\s/",$command)) $command=$command=escapeshellarg($command); // Use quotes on command if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command
//$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass); //$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass);
//$param="-F c"; //$param="-F c";
@ -352,10 +351,6 @@ if ($what == 'postgresql')
// Si on a demande une generation
//if ($what)
//{
if ($errormsg) if ($errormsg)
{ {
setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors'); setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
@ -383,7 +378,7 @@ if ($what == 'postgresql')
setEventMessages($langs->trans("YouMustRunCommandFromCommandLineAfterLoginToUser",$dolibarr_main_db_user,$dolibarr_main_db_user), null, 'mesgs'); setEventMessages($langs->trans("YouMustRunCommandFromCommandLineAfterLoginToUser",$dolibarr_main_db_user,$dolibarr_main_db_user), null, 'mesgs');
} }
} }
//}
/* /*
$filearray=dol_dir_list($conf->admin->dir_output.'/backup','files',0,'','',$sortfield,(strtolower($sortorder)=='asc'?SORT_ASC:SORT_DESC),1); $filearray=dol_dir_list($conf->admin->dir_output.'/backup','files',0,'','',$sortfield,(strtolower($sortorder)=='asc'?SORT_ASC:SORT_DESC),1);

View File

@ -306,7 +306,7 @@ function dol_getprefix()
* To link to a module file from a core file, then this function can be used (call by hook / trigger / speciales pages) * To link to a module file from a core file, then this function can be used (call by hook / trigger / speciales pages)
* *
* @param string $relpath Relative path to file (Ie: mydir/myfile, ../myfile, ...) * @param string $relpath Relative path to file (Ie: mydir/myfile, ../myfile, ...)
* @param string $classname Class name * @param string $classname Class name (deprecated)
* @return bool True if load is a success, False if it fails * @return bool True if load is a success, False if it fails
*/ */
function dol_include_once($relpath, $classname='') function dol_include_once($relpath, $classname='')

View File

@ -913,6 +913,9 @@ class DolibarrModules // Can not be abstract, because we need to insta
$comment = isset($this->cronjobs[$key]['comment'])?$this->cronjobs[$key]['comment']:''; $comment = isset($this->cronjobs[$key]['comment'])?$this->cronjobs[$key]['comment']:'';
$frequency = isset($this->cronjobs[$key]['frequency'])?$this->cronjobs[$key]['frequency']:''; $frequency = isset($this->cronjobs[$key]['frequency'])?$this->cronjobs[$key]['frequency']:'';
$unitfrequency = isset($this->cronjobs[$key]['unitfrequency'])?$this->cronjobs[$key]['unitfrequency']:''; $unitfrequency = isset($this->cronjobs[$key]['unitfrequency'])?$this->cronjobs[$key]['unitfrequency']:'';
$status = isset($this->cronjobs[$key]['status'])?$this->cronjobs[$key]['status']:'';
$priority = isset($this->cronjobs[$key]['priority'])?$this->cronjobs[$key]['priority']:'';
$test = isset($this->cronjobs[$key]['test'])?$this->cronjobs[$key]['test']:'';
// Search if boxes def already present // Search if boxes def already present
$sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."cronjob"; $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."cronjob";
@ -936,7 +939,7 @@ class DolibarrModules // Can not be abstract, because we need to insta
if (! $err) if (! $err)
{ {
$sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob (module_name, datec, datestart, label, jobtype, classesname, objectname, methodename, command, params, note, frequency, unitfrequency, entity)"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob (module_name, datec, datestart, label, jobtype, classesname, objectname, methodename, command, params, note, frequency, unitfrequency, priority, status, entity, test)";
$sql.= " VALUES ("; $sql.= " VALUES (";
$sql.= "'".$this->db->escape($this->rights_class)."', "; $sql.= "'".$this->db->escape($this->rights_class)."', ";
$sql.= "'".$this->db->idate($now)."', "; $sql.= "'".$this->db->idate($now)."', ";
@ -951,7 +954,10 @@ class DolibarrModules // Can not be abstract, because we need to insta
$sql.= ($comment?"'".$this->db->escape($comment)."'":"null").","; $sql.= ($comment?"'".$this->db->escape($comment)."'":"null").",";
$sql.= "'".$this->db->escape($frequency)."', "; $sql.= "'".$this->db->escape($frequency)."', ";
$sql.= "'".$this->db->escape($unitfrequency)."', "; $sql.= "'".$this->db->escape($unitfrequency)."', ";
$sql.= $conf->entity; $sql.= "'".$this->db->escape($priority)."', ";
$sql.= "'".$this->db->escape($status)."', ";
$sql.= $conf->entity.",";
$sql.= "'".$this->db->escape($test)."'";
$sql.= ")"; $sql.= ")";
dol_syslog(get_class($this)."::insert_cronjobs", LOG_DEBUG); dol_syslog(get_class($this)."::insert_cronjobs", LOG_DEBUG);

View File

@ -101,7 +101,8 @@ class modCron extends DolibarrModules
// Cronjobs // Cronjobs
$this->cronjobs = array( $this->cronjobs = array(
0=>array('label'=>'PurgeDeleteTemporaryFilesShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'purgeFiles', 'parameters'=>'', 'comment'=>'PurgeDeleteTemporaryFiles', 'frequency'=>1, 'unitfrequency'=>3600 * 24 * 7), 0=>array('label'=>'PurgeDeleteTemporaryFilesShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'purgeFiles', 'parameters'=>'', 'comment'=>'PurgeDeleteTemporaryFiles', 'frequency'=>1, 'unitfrequency'=>3600 * 24 * 7, 'priority'=>10, 'status'=>1, 'test'=>'1'),
1=>array('label'=>'MakeLocalDatabaseDumpShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'dumpDatabase', 'parameters'=>'', 'comment'=>'MakeLocalDatabaseDump', 'frequency'=>1, 'unitfrequency'=>3600 * 24 * 7, 'priority'=>20, 'status'=>0, 'test'=>'0'),
// 1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>1, 'unitfrequency'=>3600*24) // 1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>1, 'unitfrequency'=>3600*24)
); );

View File

@ -107,7 +107,7 @@ class modFacture extends DolibarrModules
// Cronjobs // Cronjobs
$this->cronjobs = array( $this->cronjobs = array(
0=>array('label'=>'RecurringInvoices', 'jobtype'=>'method', 'class'=>'compta/facture/class/facture-rec.class.php', 'objectname'=>'Facture', 'method'=>'generateRecurringInvoices', 'parameters'=>'', 'comment'=>'Generate recurring invoices', 'frequency'=>1, 'unitfrequency'=>3600*24), 0=>array('label'=>'RecurringInvoices', 'jobtype'=>'method', 'class'=>'compta/facture/class/facture-rec.class.php', 'objectname'=>'FactureRec', 'method'=>'generateRecurringInvoices', 'parameters'=>'', 'comment'=>'Generate recurring invoices', 'frequency'=>1, 'unitfrequency'=>3600*24),
// 1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>3600, 'unitfrequency'=>3600) // 1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>3600, 'unitfrequency'=>3600)
); );
// List of cron jobs entries to add // List of cron jobs entries to add

View File

@ -391,8 +391,9 @@ class Cronjob extends CommonObject
$sql.= " t.fk_user_author,"; $sql.= " t.fk_user_author,";
$sql.= " t.fk_user_mod,"; $sql.= " t.fk_user_mod,";
$sql.= " t.note,"; $sql.= " t.note,";
$sql.= " t.nbrun"; $sql.= " t.nbrun,";
$sql .= ", t.libname"; $sql.= " t.libname,";
$sql.= " t.test";
$sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t"; $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t";
$sql.= " WHERE 1 = 1"; $sql.= " WHERE 1 = 1";
@ -466,6 +467,7 @@ class Cronjob extends CommonObject
$line->note = $obj->note; $line->note = $obj->note;
$line->nbrun = $obj->nbrun; $line->nbrun = $obj->nbrun;
$line->libname = $obj->libname; $line->libname = $obj->libname;
$line->test = $obj->test;
$this->lines[]=$line; $this->lines[]=$line;
$i++; $i++;
@ -905,8 +907,10 @@ class Cronjob extends CommonObject
if ($this->jobtype=='method') if ($this->jobtype=='method')
{ {
// load classes // load classes
$ret=dol_include_once($this->classesname,$this->objectname); if (! $error)
if (! $error && $ret===false) {
$ret=dol_include_once($this->classesname);
if ($ret===false || (! class_exists($this->objectname)))
{ {
$this->error=$langs->trans('CronCannotLoadClass',$this->classesname,$this->objectname); $this->error=$langs->trans('CronCannotLoadClass',$this->classesname,$this->objectname);
dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
@ -915,6 +919,21 @@ class Cronjob extends CommonObject
$retval = $this->lastresult; $retval = $this->lastresult;
$error++; $error++;
} }
}
// test if method exists
if (! $error)
{
if (! method_exists($this->objectname, $this->methodename))
{
$this->error=$langs->trans('CronMethodDoesNotExists',$this->objectname,$this->methodename);
dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR);
$this->lastoutput = $this->error;
$this->lastresult = -1;
$retval = $this->lastresult;
$error++;
}
}
// Load langs // Load langs
if (! $error) if (! $error)
@ -923,7 +942,8 @@ class Cronjob extends CommonObject
if ($result < 0) if ($result < 0)
{ {
dol_syslog(get_class($this)."::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR); dol_syslog(get_class($this)."::run_jobs Cannot load module lang file - ".$langs->error, LOG_ERR);
$this->lastoutput = $langs->error; $this->error = $langs->error;
$this->lastoutput = $this->error;
$this->lastresult = -1; $this->lastresult = -1;
$retval = $this->lastresult; $retval = $this->lastresult;
$error++; $error++;

View File

@ -220,6 +220,8 @@ if ($num > 0)
$style='pair'; $style='pair';
foreach($object->lines as $line) foreach($object->lines as $line)
{ {
if (! verifCond($line->test)) continue; // Discard line with test = false
// title profil // title profil
if ($style=='pair') {$style='impair';} if ($style=='pair') {$style='impair';}
else {$style='pair';} else {$style='pair';}

View File

@ -35,6 +35,7 @@ UPDATE llx_projet as p set opp_percent = (SELECT percent from llx_c_lead_status
ALTER TABLE llx_overwrite_trans ADD UNIQUE INDEX uk_overwrite_trans(lang, transkey); ALTER TABLE llx_overwrite_trans ADD UNIQUE INDEX uk_overwrite_trans(lang, transkey);
ALTER TABLE llx_cronjob MODIFY COLUMN unitfrequency varchar(255) NOT NULL DEFAULT '3600'; ALTER TABLE llx_cronjob MODIFY COLUMN unitfrequency varchar(255) NOT NULL DEFAULT '3600';
ALTER TABLE llx_cronjob ADD COLUMN test varchar(255) DEFAULT '1';
ALTER TABLE llx_facture ADD INDEX idx_facture_fk_statut (fk_statut); ALTER TABLE llx_facture ADD INDEX idx_facture_fk_statut (fk_statut);

View File

@ -46,6 +46,7 @@ CREATE TABLE llx_cronjob
nbrun integer, -- nb of run complete (failed or not) nbrun integer, -- nb of run complete (failed or not)
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 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
test varchar(255) DEFAULT '1',
fk_user_author integer DEFAULT NULL, fk_user_author integer DEFAULT NULL,
fk_user_mod integer DEFAULT NULL, fk_user_mod integer DEFAULT NULL,
fk_mailing integer DEFAULT NULL, -- id of emailing if job was queued to send mass emailing fk_mailing integer DEFAULT NULL, -- id of emailing if job was queued to send mass emailing

View File

@ -89,3 +89,5 @@ CronMenu=Cron
CronCannotLoadClass=Cannot load class %s or object %s CronCannotLoadClass=Cannot load class %s or object %s
UseMenuModuleToolsToAddCronJobs=Go into menu "Home - Modules tools - Job list" to see and edit scheduled jobs. UseMenuModuleToolsToAddCronJobs=Go into menu "Home - Modules tools - Job list" to see and edit scheduled jobs.
TaskDisabled=Job disabled TaskDisabled=Job disabled
MakeLocalDatabaseDumpShort=Local database backup
MakeLocalDatabaseDump=Create a local database dump