cron_run_jobs.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/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 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 cron/script/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('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  30. if (! defined('NOLOGIN')) define('NOLOGIN','1');
  31. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1');
  32. $sapi_type = php_sapi_name();
  33. $script_file = basename(__FILE__);
  34. $path=dirname(__FILE__).'/';
  35. // Test if batch mode
  36. if (substr($sapi_type, 0, 3) == 'cgi') {
  37. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  38. exit(-1);
  39. }
  40. if (! isset($argv[1]) || ! $argv[1]) {
  41. print "Usage: ".$script_file." securitykey userlogin cronjobid(optional)\n";
  42. exit(-1);
  43. }
  44. $key=$argv[1];
  45. if (! isset($argv[2]) || ! $argv[2]) {
  46. print "Usage: ".$script_file." securitykey userlogin cronjobid(optional)\n";
  47. exit(-1);
  48. } else {
  49. $userlogin=$argv[2];
  50. }
  51. require_once ($path."../../htdocs/master.inc.php");
  52. require_once (DOL_DOCUMENT_ROOT."/cron/class/cronjob.class.php");
  53. require_once (DOL_DOCUMENT_ROOT.'/user/class/user.class.php');
  54. require_once (DOL_DOCUMENT_ROOT."/cron/class/cronjob.class.php");
  55. // Global variables
  56. $version=DOL_VERSION;
  57. $error=0;
  58. /*
  59. * Main
  60. */
  61. @set_time_limit(0);
  62. print "***** ".$script_file." (".$version.") pid=".getmypid()." *****\n";
  63. // Check security key
  64. if ($key != $conf->global->CRON_KEY)
  65. {
  66. print "Error: securitykey is wrong\n";
  67. exit(-1);
  68. }
  69. // Check user login
  70. $user=new User($db);
  71. $result=$user->fetch('',$userlogin);
  72. if ($result < 0)
  73. {
  74. echo "User Error: ".$user->error;
  75. dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR);
  76. exit(-1);
  77. }
  78. else
  79. {
  80. if (empty($user->id))
  81. {
  82. echo " User user login: ".$userlogin." do not exists";
  83. dol_syslog(" User user login:".$userlogin." do not exists", LOG_ERR);
  84. exit(-1);
  85. }
  86. }
  87. if (isset($argv[3]) || $argv[3])
  88. {
  89. $id = $argv[3];
  90. }
  91. // create a jobs object
  92. $object = new Cronjob($db);
  93. $filter=array();
  94. if (empty($id)) {
  95. $filter=array();
  96. $filter['t.rowid']=$id;
  97. }
  98. $result = $object->fetch_all('DESC','t.rowid', 0, 0, 1, $filter);
  99. if ($result<0)
  100. {
  101. echo "Error: ".$object->error;
  102. dol_syslog("cron_run_jobs.php:: fetch Error ".$object->error, LOG_ERR);
  103. exit(-1);
  104. }
  105. // current date
  106. $now=dol_now();
  107. if(is_array($object->lines) && (count($object->lines)>0))
  108. {
  109. // Loop over job
  110. foreach($object->lines as $line)
  111. {
  112. //If date_next_jobs is less of current dat, execute the program, and store the execution time of the next execution in database
  113. if (($line->datenextrun < $now) && $line->dateend < $now){
  114. $cronjob=new Cronjob($db);
  115. $result=$cronjob->fetch($line->id);
  116. if ($result<0) {
  117. echo "Error:".$cronjob->error;
  118. dol_syslog("cron_run_jobs.php:: fetch Error".$cronjob->error, LOG_ERR);
  119. exit(-1);
  120. }
  121. // execute methode
  122. $result=$cronjob->run_jobs($userlogin);
  123. if ($result<0) {
  124. echo "Error:".$cronjob->error;
  125. dol_syslog("cron_run_jobs.php:: run_jobs Error".$cronjob->error, LOG_ERR);
  126. exit(-1);
  127. }
  128. // we re-program the next execution and stores the last execution time for this job
  129. $result=$cronjob->reprogram_jobs($userlogin);
  130. if ($result<0) {
  131. echo "Error:".$cronjob->error;
  132. dol_syslog("cron_run_jobs.php:: reprogram_jobs Error".$cronjob->error, LOG_ERR);
  133. exit(-1);
  134. }
  135. }
  136. }
  137. }
  138. $db->close();
  139. exit(0);
  140. ?>