vcron.php 10.0 KB

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