vcron.php 10 KB

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