cron_run_jobs.php

Go to the documentation of this file.
00001 <?php
00002 /* Copyright (C) 2012      Nicolas Villa aka Boyquotes http://informetic.fr
00003  * Copyright (C) 2013      Florian Henry <forian.henry@open-cocnept.pro>
00004  * Copyright (C) 2013      Laurent Destailleur <eldy@users.sourceforge.net>
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00025 if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal
00026 if (! defined('NOREQUIREMENU'))  define('NOREQUIREMENU','1');
00027 if (! defined('NOREQUIREHTML'))  define('NOREQUIREHTML','1');
00028 if (! defined('NOREQUIREAJAX'))  define('NOREQUIREAJAX','1');
00029 if (! defined('NOREQUIRESOC'))   define('NOREQUIRESOC','1');
00030 if (! defined('NOLOGIN'))   define('NOLOGIN','1');
00031 //if (! defined('NOREQUIRETRAN'))  define('NOREQUIRETRAN','1');
00032 
00033 // librarie core
00034 // Dolibarr environment
00035 $res = @include("../../main.inc.php"); // From htdocs directory
00036 if (! $res) {
00037   $res = @include("../../../main.inc.php"); // From "custom" directory
00038 }
00039 if (! $res) die("Include of master.inc.php fails");
00040 
00041 // librarie jobs
00042 dol_include_once("/cron/class/cronjob.class.php");
00043 
00044 global $langs, $conf;
00045 
00046 // Language Management
00047 $langs->load("admin");
00048 $langs->load("cron");
00049 
00050 
00051 
00052 
00053 
00054 /*
00055  * View
00056  */
00057 
00058 // Check the key, avoid that a stranger starts cron
00059 $key = GETPOST('securitykey','alpha');
00060 if (empty($key))
00061 {
00062   echo 'securitykey is require';
00063   exit;
00064 }
00065 if($key != $conf->global->CRON_KEY)
00066 {
00067   echo 'securitykey is wrong';
00068   exit;
00069 }
00070 // Check the key, avoid that a stranger starts cron
00071 $userlogin = GETPOST('userlogin','alpha');
00072 if (empty($userlogin))
00073 {
00074   echo 'userlogin is require';
00075   exit;
00076 }
00077 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
00078 $user=new User($db);
00079 $result=$user->fetch('',$userlogin);
00080 if ($result < 0)
00081 {
00082   echo "User Error:".$user->error;
00083   dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_WARNING);
00084   exit;
00085 }
00086 else
00087 {
00088   if (empty($user->id))
00089   {
00090     echo " User user login:".$userlogin." do not exists";
00091     dol_syslog(" User user login:".$userlogin." do not exists", LOG_WARNING);
00092     exit;
00093   }
00094 }
00095 $id = GETPOST('id','alpha');  // We accept non numeric id. We will filter later.
00096 
00097 
00098 // create a jobs object
00099 $object = new Cronjob($db);
00100 
00101 $filter=array();
00102 if (! empty($id))
00103 {
00104   if (! is_numeric($id))
00105   {
00106     echo "Error: Bad value for parameter job id";
00107     dol_syslog("cron_run_jobs.php Bad value for parameter job id", LOG_WARNING);
00108     exit;
00109   }
00110   $filter=array();
00111   $filter['t.rowid']=$id;
00112 }
00113 
00114 $result = $object->fetch_all('DESC','t.rowid', 0, 0, 1, $filter);
00115 if ($result<0)
00116 {
00117   echo "Error: ".$cronjob->error;
00118   dol_syslog("cron_run_jobs.php fetch Error".$cronjob->error, LOG_WARNING);
00119   exit;
00120 }
00121 
00122 // current date
00123 $now=dol_now();
00124 $nbofjobs=count($object->lines);
00125 $nbofjobslaunchedok=0;
00126 $nbofjobslaunchedko=0;
00127 
00128 if (is_array($object->lines) && (count($object->lines)>0))
00129 {
00130   // Loop over job
00131   foreach($object->lines as $line)
00132   {
00133     dol_syslog("cron_run_jobs.php fetch cronjobid: ".$line->id, LOG_WARNING);
00134 
00135     //If date_next_jobs is less of current dat, execute the program, and store the execution time of the next execution in database
00136     if ((($line->datenextrun <= $now) && $line->dateend < $now)
00137         || ((empty($line->datenextrun)) && (empty($line->dateend))))
00138     {
00139 
00140       dol_syslog("cron_run_jobs.php:: torun line->datenextrun:".dol_print_date($line->datenextrun,'dayhourtext')." line->dateend:".dol_print_date($line->dateend,'dayhourtext')." now:".dol_print_date($now,'dayhourtext'));
00141 
00142       $cronjob=new Cronjob($db);
00143       $result=$cronjob->fetch($line->id);
00144       if ($result<0)
00145       {
00146         echo "Error:".$cronjob->error;
00147         dol_syslog("cron_run_jobs.php:: fetch Error".$cronjob->error, LOG_ERR);
00148         exit;
00149       }
00150       // Execut job
00151       $result=$cronjob->run_jobs($userlogin);
00152       if ($result<0)
00153       {
00154         echo "Error:".$cronjob->error;
00155         dol_syslog("cron_run_jobs.php:: run_jobs Error".$cronjob->error, LOG_ERR);
00156         $nbofjobslaunchedko++;
00157       }
00158       else
00159       {
00160         $nbofjobslaunchedok++;
00161       }
00162 
00163       // We re-program the next execution and stores the last execution time for this job
00164       $result=$cronjob->reprogram_jobs($userlogin);
00165       if ($result<0)
00166       {
00167         echo "Error:".$cronjob->error;
00168         dol_syslog("cron_run_jobs.php:: reprogram_jobs Error".$cronjob->error, LOG_ERR);
00169         exit;
00170       }
00171 
00172     }
00173   }
00174   echo "Result: ".($nbofjobs)." jobs - ".($nbofjobslaunchedok+$nbofjobslaunchedko)." launched = ".$nbofjobslaunchedok." OK + ".$nbofjobslaunchedko." KO";
00175 }
00176 else
00177 {
00178   echo "No active jobs found";
00179 }
00180 
00181 $db->close();
00182 ?>