cron_run_jobs.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /* Copyright (C) 2012 Nicolas Villa aka Boyquotes http://informetic.fr
  3. * Copyright (C) 2013 Florian Henry <forian.henry@open-cocnept.pro>
  4. * Copyright (C) 2013-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2017 Regis Houssin <regis.houssin@inodbox.com>
  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 <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/public/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. // For MultiCompany module.
  31. // Do not use GETPOST here, function is not defined and define must be done before including main.inc.php
  32. $entity = (!empty($_GET['entity']) ? (int) $_GET['entity'] : (!empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
  33. if (is_numeric($entity)) define("DOLENTITY", $entity);
  34. // Error if CLI mode
  35. if (php_sapi_name() == "cli")
  36. {
  37. echo "Error: This page can't be used as a CLI script. For the CLI version of script, launch cron_run_job.php available into scripts/cron/ directory.\n";
  38. exit(-1);
  39. }
  40. // librarie core
  41. // Dolibarr environment
  42. require '../../main.inc.php';
  43. // librarie jobs
  44. dol_include_once("/cron/class/cronjob.class.php");
  45. global $langs, $conf;
  46. // Language Management
  47. $langs->loadLangs(array("admin", "cron", "dict"));
  48. /*
  49. * View
  50. */
  51. // current date
  52. $now = dol_now();
  53. // Check the key, avoid that a stranger starts cron
  54. $key = GETPOST('securitykey', 'alpha');
  55. if (empty($key))
  56. {
  57. echo 'Securitykey is required. Check setup of cron jobs module.';
  58. exit;
  59. }
  60. if ($key != $conf->global->CRON_KEY)
  61. {
  62. echo 'Securitykey is wrong.';
  63. exit;
  64. }
  65. // Check the key, avoid that a stranger starts cron
  66. $userlogin = GETPOST('userlogin', 'alpha');
  67. if (empty($userlogin))
  68. {
  69. echo 'Userlogin is required.';
  70. exit;
  71. }
  72. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  73. $user = new User($db);
  74. $result = $user->fetch('', $userlogin);
  75. if ($result < 0)
  76. {
  77. echo "User Error:".$user->error;
  78. dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
  79. exit;
  80. }
  81. else
  82. {
  83. if (empty($user->id))
  84. {
  85. echo " User login:".$userlogin." do not exists";
  86. dol_syslog(" User login:".$userlogin." do not exists", LOG_ERR);
  87. exit;
  88. }
  89. }
  90. $id = GETPOST('id', 'alpha'); // We accept non numeric id. We will filter later.
  91. // create a jobs object
  92. $object = new Cronjob($db);
  93. $filter = array();
  94. if (!empty($id)) {
  95. if (!is_numeric($id))
  96. {
  97. echo "Error: Bad value for parameter job id";
  98. dol_syslog("cron_run_jobs.php Bad value for parameter job id", LOG_WARNING);
  99. exit;
  100. }
  101. $filter['t.rowid'] = $id;
  102. }
  103. $result = $object->fetch_all('ASC,ASC,ASC', 't.priority,t.entity,t.rowid', 0, 0, 1, $filter, 0);
  104. if ($result < 0)
  105. {
  106. echo "Error: ".$object->error;
  107. dol_syslog("cron_run_jobs.php fetch Error".$object->error, LOG_ERR);
  108. exit;
  109. }
  110. $qualifiedjobs = array();
  111. foreach ($object->lines as $val)
  112. {
  113. if (!verifCond($val->test)) continue;
  114. $qualifiedjobs[] = $val;
  115. }
  116. // TODO Duplicate code. This sequence of code must be shared with code into cron_run_jobs.php script.
  117. // current date
  118. $nbofjobs = count($qualifiedjobs);
  119. $nbofjobslaunchedok = 0;
  120. $nbofjobslaunchedko = 0;
  121. if (is_array($qualifiedjobs) && (count($qualifiedjobs) > 0))
  122. {
  123. $savconf = dol_clone($conf);
  124. // Loop over job
  125. foreach ($qualifiedjobs as $line)
  126. {
  127. dol_syslog("cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label, LOG_DEBUG);
  128. echo "cron_run_jobs.php cronjobid: ".$line->id." priority=".$line->priority." entity=".$line->entity." label=".$line->label;
  129. // Force reload of setup for the current entity
  130. if ($line->entity != $conf->entity)
  131. {
  132. dol_syslog("cron_run_jobs.php we work on another entity so we reload user and conf", LOG_DEBUG);
  133. echo " -> we change entity so we reload user and conf";
  134. $conf->entity = (empty($line->entity) ? 1 : $line->entity);
  135. $conf->setValues($db); // This make also the $mc->setValues($conf); that reload $mc->sharings
  136. // Force recheck that user is ok for the entity to process and reload permission for entity
  137. if ($conf->entity != $user->entity && $user->entity != 0)
  138. {
  139. $result = $user->fetch('', $userlogin, '', 0, $conf->entity);
  140. if ($result < 0)
  141. {
  142. echo "\nUser Error: ".$user->error."\n";
  143. dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
  144. exit(-1);
  145. }
  146. else
  147. {
  148. if ($result == 0)
  149. {
  150. echo "\nUser login: ".$userlogin." does not exists for entity ".$conf->entity."\n";
  151. dol_syslog("User login:".$userlogin." does not exists", LOG_ERR);
  152. exit(-1);
  153. }
  154. }
  155. $user->getrights();
  156. }
  157. }
  158. //If date_next_jobs is less of current date, execute the program, and store the execution time of the next execution in database
  159. if (($line->datenextrun < $now) && (empty($line->datestart) || $line->datestart <= $now) && (empty($line->dateend) || $line->dateend >= $now))
  160. {
  161. echo " - qualified";
  162. 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'));
  163. $cronjob = new Cronjob($db);
  164. $result = $cronjob->fetch($line->id);
  165. if ($result < 0)
  166. {
  167. echo "Error cronjobid: ".$line->id." cronjob->fetch: ".$cronjob->error."\n";
  168. echo "Failed to fetch job ".$line->id."\n";
  169. dol_syslog("cron_run_jobs.php::fetch Error".$cronjob->error, LOG_ERR);
  170. exit;
  171. }
  172. // Execute job
  173. $result = $cronjob->run_jobs($userlogin);
  174. if ($result < 0)
  175. {
  176. echo "Error cronjobid: ".$line->id." cronjob->run_job: ".$cronjob->error."\n";
  177. echo "At least one job failed. Go on menu Home-Setup-Admin tools to see result for each job.\n";
  178. echo "You can also enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
  179. dol_syslog("cron_run_jobs.php::run_jobs Error".$cronjob->error, LOG_ERR);
  180. $nbofjobslaunchedko++;
  181. }
  182. else
  183. {
  184. $nbofjobslaunchedok++;
  185. }
  186. echo " - result of run_jobs = ".$result;
  187. // We re-program the next execution and stores the last execution time for this job
  188. $result = $cronjob->reprogram_jobs($userlogin, $now);
  189. if ($result < 0)
  190. {
  191. echo "Error cronjobid: ".$line->id." cronjob->reprogram_job: ".$cronjob->error."\n";
  192. echo "Enable module Log if not yet enabled, run again and take a look into dolibarr.log file\n";
  193. dol_syslog("cron_run_jobs.php::reprogram_jobs Error".$cronjob->error, LOG_ERR);
  194. exit;
  195. }
  196. echo " - reprogrammed\n";
  197. }
  198. else
  199. {
  200. echo " - not qualified\n";
  201. 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'));
  202. }
  203. }
  204. $conf = $savconf;
  205. echo "Result: ".($nbofjobs)." jobs - ".($nbofjobslaunchedok + $nbofjobslaunchedko)." launched = ".$nbofjobslaunchedok." OK + ".$nbofjobslaunchedko." KO";
  206. }
  207. else
  208. {
  209. echo "Result: No active jobs found.";
  210. }
  211. $db->close();