cron_run_jobs.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2012 Nicolas Villa aka Boyquotes http://informetic.fr
  4. * Copyright (C) 2013 Florian Henry <forian.henry@open-concept.pro
  5. * Copyright (C) 2013-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file scripts/cron/cron_run_jobs.php
  22. * \ingroup cron
  23. * \brief Execute pendings jobs
  24. */
  25. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal
  26. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
  27. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  28. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  29. if (! defined('NOLOGIN')) define('NOLOGIN','1');
  30. $sapi_type = php_sapi_name();
  31. $script_file = basename(__FILE__);
  32. $path=dirname(__FILE__).'/';
  33. // Test if batch mode
  34. if (substr($sapi_type, 0, 3) == 'cgi') {
  35. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  36. exit(-1);
  37. }
  38. require_once $path."../../htdocs/master.inc.php";
  39. require_once DOL_DOCUMENT_ROOT."/cron/class/cronjob.class.php";
  40. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  41. // Check parameters
  42. if (! isset($argv[1]) || ! $argv[1]) {
  43. usage($path,$script_file);
  44. exit(-1);
  45. }
  46. $key=$argv[1];
  47. if (! isset($argv[2]) || ! $argv[2]) {
  48. usage($path,$script_file);
  49. exit(-1);
  50. }
  51. $userlogin=$argv[2];
  52. // Global variables
  53. $version=DOL_VERSION;
  54. $error=0;
  55. // Language Management
  56. $langs->loadLangs(array('main', 'admin', 'cron', 'dict'));
  57. /*
  58. * Main
  59. */
  60. // current date
  61. $now=dol_now();
  62. @set_time_limit(0);
  63. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." ***** userlogin=" . $userlogin . " ***** " . dol_print_date($now, 'dayhourrfc') . " *****\n";
  64. // Check module cron is activated
  65. if (empty($conf->cron->enabled))
  66. {
  67. print "Error: module Scheduled jobs (cron) not activated\n";
  68. exit(-1);
  69. }
  70. // Check module cron is activated
  71. if (empty($conf->cron->enabled))
  72. {
  73. print "Error: module Scheduled jobs (cron) not activated\n";
  74. exit(-1);
  75. }
  76. // Check security key
  77. if ($key != $conf->global->CRON_KEY)
  78. {
  79. print "Error: securitykey is wrong\n";
  80. exit(-1);
  81. }
  82. // If param userlogin is reserved word 'firstadmin'
  83. if ($userlogin == 'firstadmin')
  84. {
  85. $sql='SELECT login, entity from '.MAIN_DB_PREFIX.'user WHERE admin = 1 and statut = 1 ORDER BY entity LIMIT 1';
  86. $resql=$db->query($sql);
  87. if ($resql)
  88. {
  89. $obj=$db->fetch_object($resql);
  90. if ($obj)
  91. {
  92. $userlogin = $obj->login;
  93. echo "First admin user found is login '".$userlogin."', entity ".$obj->entity."\n";
  94. }
  95. }
  96. else dol_print_error($db);
  97. }
  98. // Check user login
  99. $user=new User($db);
  100. $result=$user->fetch('',$userlogin);
  101. if ($result < 0)
  102. {
  103. echo "User Error: ".$user->error;
  104. dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
  105. exit(-1);
  106. }
  107. else
  108. {
  109. if (empty($user->id))
  110. {
  111. echo "User login: ".$userlogin." does not exists";
  112. dol_syslog("User login:".$userlogin." does not exists", LOG_ERR);
  113. exit(-1);
  114. }
  115. }
  116. $user->getrights();
  117. if (isset($argv[3]) || $argv[3])
  118. {
  119. $id = $argv[3];
  120. }
  121. // create a jobs object
  122. $object = new Cronjob($db);
  123. $filter=array();
  124. if (! empty($id)) {
  125. if (! is_numeric($id))
  126. {
  127. echo "Error: Bad value for parameter job id";
  128. dol_syslog("cron_run_jobs.php Bad value for parameter job id", LOG_WARNING);
  129. exit;
  130. }
  131. $filter['t.rowid']=$id;
  132. }
  133. $result = $object->fetch_all('ASC,ASC,ASC','t.priority,t.entity,t.rowid', 0, 0, 1, $filter, 0);
  134. if ($result<0)
  135. {
  136. echo "Error: ".$object->error;
  137. dol_syslog("cron_run_jobs.php fetch Error ".$object->error, LOG_ERR);
  138. exit(-1);
  139. }
  140. $qualifiedjobs = array();
  141. foreach($object->lines as $val)
  142. {
  143. if (! verifCond($val->test)) continue;
  144. $qualifiedjobs[] = $val;
  145. }
  146. // TODO Duplicate code. This sequence of code must be shared with code into public/cron/cron_run_jobs.php php page.
  147. // current date
  148. $nbofjobs=count($qualifiedjobs);
  149. $nbofjobslaunchedok=0;
  150. $nbofjobslaunchedko=0;
  151. if (is_array($qualifiedjobs) && (count($qualifiedjobs)>0))
  152. {
  153. $savconf = dol_clone($conf);
  154. // Loop over job
  155. foreach($qualifiedjobs as $line)
  156. {
  157. dol_syslog("cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label, LOG_DEBUG);
  158. echo "cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label;
  159. // Force reload of setup for the current entity
  160. if ($line->entity != $conf->entity)
  161. {
  162. dol_syslog("cron_run_jobs.php we work on another entity so we reload mysoc, langs, user and conf", LOG_DEBUG);
  163. echo " -> we change entity so we reload mysoc, langs, user and conf";
  164. $conf->entity = (empty($line->entity)?1:$line->entity);
  165. $conf->setValues($db); // This make also the $mc->setValues($conf); that reload $mc->sharings
  166. $mysoc->setMysoc($conf);
  167. // Force recheck that user is ok for the entity to process and reload permission for entity
  168. if ($conf->entity != $user->entity && $user->entity != 0)
  169. {
  170. $result=$user->fetch('', $userlogin, '', 0, $conf->entity);
  171. if ($result < 0)
  172. {
  173. echo "\nUser Error: ".$user->error."\n";
  174. dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
  175. exit(-1);
  176. }
  177. else
  178. {
  179. if ($result == 0)
  180. {
  181. echo "\nUser login: ".$userlogin." does not exists for entity ".$conf->entity."\n";
  182. dol_syslog("User login:".$userlogin." does not exists", LOG_ERR);
  183. exit(-1);
  184. }
  185. }
  186. $user->getrights();
  187. }
  188. // Reload langs
  189. $langcode = (empty($conf->global->MAIN_LANG_DEFAULT)?'auto':$conf->global->MAIN_LANG_DEFAULT);
  190. if (! empty($user->conf->MAIN_LANG_DEFAULT)) $langcode = $user->conf->MAIN_LANG_DEFAULT;
  191. if($langs->getDefaultLang() != $langcode) $langs->setDefaultLang($langcode);
  192. }
  193. //If date_next_jobs is less of current date, execute the program, and store the execution time of the next execution in database
  194. if (($line->datenextrun < $now) && (empty($line->datestart) || $line->datestart <= $now) && (empty($line->dateend) || $line->dateend >= $now))
  195. {
  196. echo " - qualified";
  197. dol_syslog("cron_run_jobs.php line->datenextrun:".dol_print_date($line->datenextrun,'dayhourrfc')." line->datestart:".dol_print_date($line->datestart,'dayhourrfc')." line->dateend:".dol_print_date($line->dateend,'dayhourrfc')." now:".dol_print_date($now,'dayhourrfc'));
  198. $cronjob=new Cronjob($db);
  199. $result=$cronjob->fetch($line->id);
  200. if ($result < 0)
  201. {
  202. echo "Error cronjobid: ".$line->id." cronjob->fetch: ".$cronjob->error."\n";
  203. echo "Failed to fetch job ".$line->id."\n";
  204. dol_syslog("cron_run_jobs.php::fetch Error ".$cronjob->error, LOG_ERR);
  205. exit(-1);
  206. }
  207. // Execute job
  208. $result=$cronjob->run_jobs($userlogin);
  209. if ($result < 0)
  210. {
  211. echo "Error cronjobid: ".$line->id." cronjob->run_job: ".$cronjob->error."\n";
  212. echo "At least one job failed. Go on menu Home-Setup-Admin tools to see result for each job.\n";
  213. echo "You can also enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
  214. dol_syslog("cron_run_jobs.php::run_jobs Error ".$cronjob->error, LOG_ERR);
  215. $nbofjobslaunchedko++;
  216. }
  217. else
  218. {
  219. $nbofjobslaunchedok++;
  220. }
  221. echo " - result of run_jobs = ".$result;
  222. // We re-program the next execution and stores the last execution time for this job
  223. $result=$cronjob->reprogram_jobs($userlogin, $now);
  224. if ($result<0)
  225. {
  226. echo "Error cronjobid: ".$line->id." cronjob->reprogram_job: ".$cronjob->error."\n";
  227. echo "Enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
  228. dol_syslog("cron_run_jobs.php::reprogram_jobs Error ".$cronjob->error, LOG_ERR);
  229. exit(-1);
  230. }
  231. echo " - reprogrammed\n";
  232. }
  233. else
  234. {
  235. echo " - not qualified\n";
  236. dol_syslog("cron_run_jobs.php job not qualified line->datenextrun:".dol_print_date($line->datenextrun,'dayhourrfc')." line->datestart:".dol_print_date($line->datestart,'dayhourrfc')." line->dateend:".dol_print_date($line->dateend,'dayhourrfc')." now:".dol_print_date($now,'dayhourrfc'));
  237. }
  238. }
  239. $conf = $savconf;
  240. }
  241. else
  242. {
  243. echo "cron_run_jobs.php no qualified job found\n";
  244. }
  245. $db->close();
  246. if ($nbofjobslaunchedko) exit(1);
  247. exit(0);
  248. /**
  249. * script cron usage
  250. *
  251. * @param string $path path
  252. * @param string $script_file filename
  253. * @return void
  254. */
  255. function usage($path,$script_file)
  256. {
  257. global $conf;
  258. print "Usage: ".$script_file." securitykey userlogin|'firstadmin' [cronjobid]\n";
  259. print "The script return 0 when everything worked successfully.\n";
  260. print "\n";
  261. print "On Linux system, you can have cron jobs ran automatically by adding an entry into cron.\n";
  262. print "For example, to run pending tasks each day at 3:30, you can add this line:\n";
  263. print "30 3 * * * ".$path.$script_file." securitykey userlogin > ".DOL_DATA_ROOT."/".$script_file.".log\n";
  264. print "For example, to run pending tasks every 5mn, you can add this line:\n";
  265. print "*/5 * * * * ".$path.$script_file." securitykey userlogin > ".DOL_DATA_ROOT."/".$script_file.".log\n";
  266. }