cron_run_jobs.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  31. $sapi_type = php_sapi_name();
  32. $script_file = basename(__FILE__);
  33. $path=dirname(__FILE__).'/';
  34. // Test if batch mode
  35. if (substr($sapi_type, 0, 3) == 'cgi') {
  36. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  37. exit(-1);
  38. }
  39. require_once $path."../../htdocs/master.inc.php";
  40. require_once DOL_DOCUMENT_ROOT."/cron/class/cronjob.class.php";
  41. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  42. // Check parameters
  43. if (! isset($argv[1]) || ! $argv[1]) {
  44. usage($path,$script_file);
  45. exit(-1);
  46. }
  47. $key=$argv[1];
  48. if (! isset($argv[2]) || ! $argv[2]) {
  49. usage($path,$script_file);
  50. exit(-1);
  51. }
  52. $userlogin=$argv[2];
  53. // Global variables
  54. $version=DOL_VERSION;
  55. $error=0;
  56. /*
  57. * Main
  58. */
  59. // current date
  60. $now=dol_now();
  61. @set_time_limit(0);
  62. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." ***** userlogin=" . $userlogin . " ***** " . dol_print_date($now, 'dayhourrfc') . " *****\n";
  63. // Check module cron is activated
  64. if (empty($conf->cron->enabled))
  65. {
  66. print "Error: module Scheduled jobs (cron) not activated\n";
  67. exit(-1);
  68. }
  69. // Check module cron is activated
  70. if (empty($conf->cron->enabled))
  71. {
  72. print "Error: module Scheduled jobs (cron) not activated\n";
  73. exit(-1);
  74. }
  75. // Check security key
  76. if ($key != $conf->global->CRON_KEY)
  77. {
  78. print "Error: securitykey is wrong\n";
  79. exit(-1);
  80. }
  81. // If param userlogin is reserved word 'firstadmin'
  82. if ($userlogin == 'firstadmin')
  83. {
  84. $sql='SELECT login, entity from '.MAIN_DB_PREFIX.'user WHERE admin = 1 and statut = 1 ORDER BY entity LIMIT 1';
  85. $resql=$db->query($sql);
  86. if ($resql)
  87. {
  88. $obj=$db->fetch_object($resql);
  89. if ($obj)
  90. {
  91. $userlogin = $obj->login;
  92. echo "First admin user found is login '".$userlogin."', entity ".$obj->entity."\n";
  93. }
  94. }
  95. else dol_print_error($db);
  96. }
  97. // Check user login
  98. $user=new User($db);
  99. $result=$user->fetch('',$userlogin);
  100. if ($result < 0)
  101. {
  102. echo "User Error: ".$user->error;
  103. dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
  104. exit(-1);
  105. }
  106. else
  107. {
  108. if (empty($user->id))
  109. {
  110. echo "User login: ".$userlogin." does not exists";
  111. dol_syslog("User login:".$userlogin." does not exists", LOG_ERR);
  112. exit(-1);
  113. }
  114. }
  115. $user->getrights();
  116. if (isset($argv[3]) || $argv[3])
  117. {
  118. $id = $argv[3];
  119. }
  120. // create a jobs object
  121. $object = new Cronjob($db);
  122. $filter=array();
  123. if (! empty($id)) {
  124. if (! is_numeric($id))
  125. {
  126. echo "Error: Bad value for parameter job id";
  127. dol_syslog("cron_run_jobs.php Bad value for parameter job id", LOG_WARNING);
  128. exit;
  129. }
  130. $filter['t.rowid']=$id;
  131. }
  132. $result = $object->fetch_all('ASC,ASC,ASC','t.priority,t.entity,t.rowid', 0, 0, 1, $filter, 0);
  133. if ($result<0)
  134. {
  135. echo "Error: ".$object->error;
  136. dol_syslog("cron_run_jobs.php:: fetch Error ".$object->error, LOG_ERR);
  137. exit(-1);
  138. }
  139. $qualifiedjobs = array();
  140. foreach($object->lines as $val)
  141. {
  142. if (! verifCond($val->test)) continue;
  143. $qualifiedjobs[] = $val;
  144. }
  145. // TODO Duplicate. This sequence of code must be shared with code into public/cron/cron_run_jobs.php php page.
  146. $nbofjobs=count($qualifiedjobs);
  147. $nbofjobslaunchedok=0;
  148. $nbofjobslaunchedko=0;
  149. if (is_array($qualifiedjobs) && (count($qualifiedjobs)>0))
  150. {
  151. // Loop over job
  152. foreach($qualifiedjobs as $line)
  153. {
  154. dol_syslog("cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label, LOG_DEBUG);
  155. echo "cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label;
  156. //If date_next_jobs is less of current date, execute the program, and store the execution time of the next execution in database
  157. if (($line->datenextrun < $now) && (empty($line->datestart) || $line->datestart <= $now) && (empty($line->dateend) || $line->dateend >= $now))
  158. {
  159. echo " - qualified";
  160. 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'));
  161. $cronjob=new Cronjob($db);
  162. $result=$cronjob->fetch($line->id);
  163. if ($result < 0)
  164. {
  165. echo "Error cronjobid: ".$line->id." cronjob->fetch: ".$cronjob->error."\n";
  166. echo "Failed to fetch job ".$line->id."\n";
  167. dol_syslog("cron_run_jobs.php::fetch Error ".$cronjob->error, LOG_ERR);
  168. exit(-1);
  169. }
  170. // Execute job
  171. $result=$cronjob->run_jobs($userlogin);
  172. if ($result < 0)
  173. {
  174. echo "Error cronjobid: ".$line->id." cronjob->run_job: ".$cronjob->error."\n";
  175. echo "At least one job failed. Go on menu Home-Setup-Admin tools to see result for each job.\n";
  176. echo "You can also enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
  177. dol_syslog("cron_run_jobs.php::run_jobs Error ".$cronjob->error, LOG_ERR);
  178. $nbofjobslaunchedko++;
  179. }
  180. else
  181. {
  182. $nbofjobslaunchedok++;
  183. }
  184. echo " - result of run_jobs = ".$result;
  185. // we re-program the next execution and stores the last execution time for this job
  186. $result=$cronjob->reprogram_jobs($userlogin, $now);
  187. if ($result<0)
  188. {
  189. echo "Error cronjobid: ".$line->id." cronjob->reprogram_job: ".$cronjob->error."\n";
  190. echo "Enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
  191. dol_syslog("cron_run_jobs.php::reprogram_jobs Error ".$cronjob->error, LOG_ERR);
  192. exit(-1);
  193. }
  194. echo " - reprogrammed\n";
  195. }
  196. else
  197. {
  198. echo " - not qualified\n";
  199. 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'));
  200. }
  201. }
  202. }
  203. else
  204. {
  205. echo "cron_run_jobs.php no qualified job found\n";
  206. }
  207. $db->close();
  208. if ($nbofjobslaunchedko) exit(1);
  209. exit(0);
  210. /**
  211. * script cron usage
  212. *
  213. * @param string $path path
  214. * @param string $script_file filename
  215. * @return void
  216. */
  217. function usage($path,$script_file)
  218. {
  219. global $conf;
  220. print "Usage: ".$script_file." securitykey userlogin|'firstadmin' [cronjobid]\n";
  221. print "The script return 0 when everything worked successfully.\n";
  222. print "\n";
  223. print "On Linux system, you can have cron jobs ran automatically by adding an entry into cron.\n";
  224. print "For example, to run pending tasks each day at 3:30, you can add this line:\n";
  225. print "30 3 * * * ".$path.$script_file." securitykey userlogin > ".DOL_DATA_ROOT."/".$script_file.".log\n";
  226. print "For example, to run pending tasks every 5mn, you can add this line:\n";
  227. print "*/5 * * * * ".$path.$script_file." securitykey userlogin > ".DOL_DATA_ROOT."/".$script_file.".log\n";
  228. }