vcron.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * This file is a cron microclock script.
  4. * It will be used as replacement of setting individual
  5. * cron lines for all virtual instances.
  6. *
  7. * Setup this vcron to run at the smallest period possible, as
  8. * it will schedule all availables vchamilos to be run as required.
  9. * Note that one activaton of this cron may not always run real crons
  10. * or may be run more than one cron.
  11. *
  12. * If used on a big system with clustering, ensure hostnames are adressed
  13. * at the load balancer entry and not on physical hosts
  14. *
  15. * @package plugin/vchamilo
  16. * @category plugins
  17. * @author Valery fremaux (valery.fremaux@gmail.com)
  18. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
  19. */
  20. define('CLI_SCRIPT', true); // for chamilo imported code
  21. require_once dirname(dirname(dirname(__FILE__))).'/main/inc/global.inc.php';
  22. require_once dirname(dirname(dirname(__FILE__))).'/local/classes/database.class.php';
  23. global $DB;
  24. $DB = new DatabaseManager();
  25. define('ROUND_ROBIN', 0);
  26. define('LOWEST_POSSIBLE_GAP', 1);
  27. global $VCRON;
  28. $VCRON = new StdClass;
  29. $VCRON->ACTIVATION = 'cli'; // choose how individual cron are launched, 'cli' or 'web'
  30. $VCRON->STRATEGY = ROUND_ROBIN ; // choose vcron rotation mode
  31. $VCRON->PERIOD = 15 * MINSECS ; // used if LOWEST_POSSIBLE_GAP to setup the max gap
  32. $VCRON->TIMEOUT = 300; // time out for CURL call to effective cron
  33. // $VCRON->TRACE = $_configuration['root_sys'].'plugin/vchamilo/log/vcrontrace.log'; // Trace file where to collect cron outputs
  34. $VCRON->TRACE = '/data/log/chamilo/vcrontrace.log'; // Trace file where to collect cron outputs
  35. $VCRON->TRACE_ENABLE = true; // enables tracing
  36. if (!is_dir($_configuration['root_sys'].'plugin/vchamilo/log')) {
  37. mkdir($_configuration['root_sys'].'plugin/vchamilo/log', 0777, true);
  38. }
  39. /**
  40. * fire a cron URL using CURL.
  41. *
  42. *
  43. */
  44. function fire_vhost_cron($vhost) {
  45. global $VCRON;
  46. if ($VCRON->TRACE_ENABLE) {
  47. $CRONTRACE = fopen($VCRON->TRACE, 'a');
  48. }
  49. $ch = curl_init($vhost->root_web.'/main/cron/run.php');
  50. $http_proxy_host = api_get_setting('vchamilo_httpproxyhost', 'vchamilo');
  51. $http_proxy_port = api_get_setting('vchamilo_httpproxyport', 'vchamilo');
  52. $http_proxy_bypass = api_get_setting('vchamilo_httpproxybypass', 'vchamilo');
  53. $http_proxy_user = api_get_setting('vchamilo_httpproxyuser', 'vchamilo');
  54. $http_proxy_password = api_get_setting('vchamilo_httpproxypassword', 'vchamilo');
  55. curl_setopt($ch, CURLOPT_TIMEOUT, $VCRON->TIMEOUT);
  56. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  57. curl_setopt($ch, CURLOPT_POST, true);
  58. curl_setopt($ch, CURLOPT_USERAGENT, 'Chamilo');
  59. curl_setopt($ch, CURLOPT_POSTFIELDS, '');
  60. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml charset=UTF-8"));
  61. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  62. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  63. // Check for proxy.
  64. if (!empty($http_proxy_host) and !is_proxybypass($uri)) {
  65. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
  66. if (empty($http_proxy_port)) {
  67. echo "Using proxy $http_proxy_host\n";
  68. curl_setopt($ch, CURLOPT_PROXY, $http_proxy_host);
  69. } else {
  70. echo "Using proxy $http_proxy_host:$http_proxy_port\n";
  71. curl_setopt($ch, CURLOPT_PROXY, $http_proxy_host.':'.$http_proxy_port);
  72. }
  73. if (!empty($http_proxy_user) and !empty($http_proxy_password)) {
  74. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $http_proxy_user.':'.$http_proxy_password);
  75. if (defined('CURLOPT_PROXYAUTH')) {
  76. // any proxy authentication if PHP 5.1
  77. curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
  78. }
  79. }
  80. }
  81. $timestamp_send = time();
  82. $rawresponse = curl_exec($ch);
  83. $timestamp_receive = time();
  84. if ($rawresponse === false) {
  85. $error = curl_errno($ch) .':'. curl_error($ch);
  86. if ($VCRON->TRACE_ENABLE){
  87. if ($CRONTRACE){
  88. fputs($CRONTRACE, "VCron start on $vhost->root_web : ".api_time_to_hms($timestamp_send)."\n" );
  89. fputs($CRONTRACE, "VCron Error : $error \n");
  90. fputs($CRONTRACE, "VCron stop on $vhost->root_web : $timestamp_receive\n#################\n\n" );
  91. fclose($CRONTRACE);
  92. }
  93. }
  94. echo("VCron started on $vhost->root_web : ".api_time_to_hms($timestamp_send)."\n" );
  95. echo("VCron Error : $error \n");
  96. echo("VCron stop on $vhost->root_web : ".api_time_to_hms($timestamp_receive)."\n#################\n\n" );
  97. return false;
  98. }
  99. if ($VCRON->TRACE_ENABLE) {
  100. if ($CRONTRACE) {
  101. fputs($CRONTRACE, "VCron start on $vhost->vhostname : ".api_time_to_hms($timestamp_send)."\n" );
  102. fputs($CRONTRACE, $rawresponse."\n");
  103. fputs($CRONTRACE, "VCron stop on $vhost->vhostname : ".api_time_to_hms($timestamp_receive)."\n#################\n\n" );
  104. fclose($CRONTRACE);
  105. }
  106. }
  107. echo("VCron start on $vhost->root_web : ".api_time_to_hms($timestamp_send)."\n" );
  108. echo($rawresponse."\n");
  109. echo("VCron stop on $vhost->root_web : ".api_time_to_hms($timestamp_receive)."\n#################\n\n" );
  110. $vhost->lastcrongap = time() - $vhost->lastcron;
  111. $vhost->lastcron = $timestamp_send;
  112. $vhost->croncount++;
  113. $vhostid = $vhost->id;
  114. unset($vhost->id);
  115. Database::update('vchamilo', (array)$vhost, array('id = ?' => $vhostid));
  116. }
  117. /**
  118. * fire a cron URL using cli exec
  119. *
  120. *
  121. */
  122. function exec_vhost_cron($vhost) {
  123. global $VCRON, $DB, $_configuration;
  124. if ($VCRON->TRACE_ENABLE) {
  125. $CRONTRACE = fopen($VCRON->TRACE, 'a');
  126. }
  127. $cmd = 'php "'.$_configuration['root_sys'].'/plugin/vchamilo/cli/cron.php" --host='.$vhost->root_web;
  128. $timestamp_send = time();
  129. exec($cmd, $rawresponse);
  130. $timestamp_receive = time();
  131. if ($VCRON->TRACE_ENABLE) {
  132. if ($CRONTRACE) {
  133. fputs($CRONTRACE, "VCron start on $vhost->root_web : $timestamp_send\n" );
  134. fputs($CRONTRACE, $rawresponse."\n");
  135. fputs($CRONTRACE, "VCron stop on $vhost->root_web : $timestamp_receive\n#################\n\n" );
  136. fclose($CRONTRACE);
  137. }
  138. }
  139. echo "VCron start on $vhost->root_web : $timestamp_send\n";
  140. echo implode("\n", $rawresponse)."\n";
  141. echo "VCron stop on $vhost->root_web : $timestamp_receive\n#################\n\n";
  142. $vhost->lastcrongap = time() - $vhost->lastcron;
  143. $vhost->lastcron = $timestamp_send;
  144. $vhost->croncount++;
  145. $DB->update_record('vchamilo', $vhost, 'id');
  146. }
  147. /**
  148. * check if $url matches anything in proxybypass list
  149. *
  150. * any errors just result in the proxy being used (least bad)
  151. *
  152. * @global object
  153. * @param string $url url to check
  154. * @return boolean true if we should bypass the proxy
  155. */
  156. function is_proxybypass($url)
  157. {
  158. $http_proxy_host = api_get_setting('vchamilo_httpproxyhost', 'vchamilo');
  159. $http_proxy_port = api_get_setting('vchamilo_httpproxyport', 'vchamilo');
  160. $http_proxy_bypass = api_get_setting('vchamilo_httpproxybypass', 'vchamilo');
  161. // sanity check
  162. if (empty($http_proxy_host) or empty($http_proxy_bypass)) {
  163. return false;
  164. }
  165. // get the host part out of the url
  166. if (!$host = parse_url($url, PHP_URL_HOST)) {
  167. return false;
  168. }
  169. // get the possible bypass hosts into an array
  170. $matches = explode(',', $http_proxy_bypass);
  171. // check for a match
  172. // (IPs need to match the left hand side and hosts the right of the url,
  173. // but we can recklessly check both as there can't be a false +ve)
  174. $bypass = false;
  175. foreach ($matches as $match) {
  176. $match = trim($match);
  177. // try for IP match (Left side)
  178. $lhs = substr($host,0,strlen($match));
  179. if (strcasecmp($match,$lhs)==0) {
  180. return true;
  181. }
  182. // try for host match (Right side)
  183. $rhs = substr($host,-strlen($match));
  184. if (strcasecmp($match,$rhs)==0) {
  185. return true;
  186. }
  187. }
  188. // nothing matched.
  189. return false;
  190. }
  191. // Main execution sequence
  192. if (!$vchamilos = Database::select('*', 'vchamilo', array(), 'all')){
  193. die("Nothing to do. No Vhosts");
  194. }
  195. $allvhosts = array_values($vchamilos);
  196. echo("<pre>");
  197. echo("Chamilo VCron... start\n");
  198. echo("Last croned : ".api_get_setting('vchamilo_cron_lasthost', 'vchamilo')."\n");
  199. if ($VCRON->STRATEGY == ROUND_ROBIN) {
  200. $rr = 0;
  201. foreach ($allvhosts as $vhostassoc) {
  202. $vhost = (object)$vhostassoc;
  203. if ($rr == 1) {
  204. api_set_setting('vchamilo_cron_lasthost', $vhost->id);
  205. echo("Round Robin : ".$vhost->root_web."\n");
  206. if ($VCRON->ACTIVATION == 'cli') {
  207. exec_vhost_cron($vhost);
  208. } else {
  209. fire_vhost_cron($vhost);
  210. }
  211. die('Done.');
  212. }
  213. if ($vhost->id == api_get_setting('vchamilo_cron_lasthost', 'vchamilo')){
  214. $rr = 1; // take next one
  215. }
  216. }
  217. // We were at last. Loop back and take first.
  218. $firsthost = (object)$allvhosts[0];
  219. api_set_setting('vchamilo_cron_lasthost', $firsthost->id, 'vchamilo');
  220. echo("Round Robin : ".$firsthost->root_web."\n");
  221. if ($VCRON->ACTIVATION == 'cli') {
  222. exec_vhost_cron($firsthost);
  223. } else {
  224. fire_vhost_cron($firsthost);
  225. }
  226. } else if ($VCRON->STRATEGY == LOWEST_POSSIBLE_GAP) {
  227. // First make measurement of cron period.
  228. if (api_get_setting('vcrontickperiod', 'vchamilo')) {
  229. api_set_setting('vcrontime', time(), 'vchamilo');
  230. return;
  231. }
  232. api_set_setting('vcrontickperiod', time() - api_get_setting('vcrontime', 'vchamilo'), 'vchamilo');
  233. $hostsperturn = max(1, $VCRON->PERIOD / api_get_setting('vcrontickperiod', 'vchamilo') * count($allvhosts));
  234. $i = 0;
  235. foreach ($allvhosts as $vhostassoc) {
  236. $vhost = (object)$vhostassoc;
  237. if ((time() - $vhost->lastcron) > $VCRON->PERIOD) {
  238. if ($VCRON->ACTIVATION == 'cli') {
  239. exec_vhost_cron($vhost);
  240. } else {
  241. fire_vhost_cron($vhost);
  242. }
  243. $i++;
  244. if ($i >= $hostsperturn) {
  245. return;
  246. }
  247. }
  248. }
  249. }