vcron.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. mkdir($_configuration['root_sys'].'plugin/vchamilo/log', 0777, true);
  40. }
  41. /**
  42. * fire a cron URL using CURL.
  43. */
  44. function fire_vhost_cron($vhost)
  45. {
  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, ["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, ['id = ?' => $vhostid]);
  117. }
  118. /**
  119. * fire a cron URL using cli exec.
  120. */
  121. function exec_vhost_cron($vhost)
  122. {
  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. *
  154. * @param string $url url to check
  155. *
  156. * @return bool true if we should bypass the proxy
  157. */
  158. function is_proxybypass($url)
  159. {
  160. $http_proxy_host = api_get_setting('vchamilo_httpproxyhost', 'vchamilo');
  161. $http_proxy_port = api_get_setting('vchamilo_httpproxyport', 'vchamilo');
  162. $http_proxy_bypass = api_get_setting('vchamilo_httpproxybypass', 'vchamilo');
  163. // sanity check
  164. if (empty($http_proxy_host) or empty($http_proxy_bypass)) {
  165. return false;
  166. }
  167. // get the host part out of the url
  168. if (!$host = parse_url($url, PHP_URL_HOST)) {
  169. return false;
  170. }
  171. // get the possible bypass hosts into an array
  172. $matches = explode(',', $http_proxy_bypass);
  173. // check for a match
  174. // (IPs need to match the left hand side and hosts the right of the url,
  175. // but we can recklessly check both as there can't be a false +ve)
  176. $bypass = false;
  177. foreach ($matches as $match) {
  178. $match = trim($match);
  179. // try for IP match (Left side)
  180. $lhs = substr($host, 0, strlen($match));
  181. if (strcasecmp($match, $lhs) == 0) {
  182. return true;
  183. }
  184. // try for host match (Right side)
  185. $rhs = substr($host, -strlen($match));
  186. if (strcasecmp($match, $rhs) == 0) {
  187. return true;
  188. }
  189. }
  190. // nothing matched.
  191. return false;
  192. }
  193. // Main execution sequence
  194. if (!$vchamilos = Database::select('*', 'vchamilo', [], 'all')) {
  195. die("Nothing to do. No Vhosts");
  196. }
  197. $allvhosts = array_values($vchamilos);
  198. echo "<pre>";
  199. echo "Chamilo VCron... start\n";
  200. echo "Last croned : ".api_get_setting('vchamilo_cron_lasthost', 'vchamilo')."\n";
  201. if ($VCRON->STRATEGY == ROUND_ROBIN) {
  202. $rr = 0;
  203. foreach ($allvhosts as $vhostassoc) {
  204. $vhost = (object) $vhostassoc;
  205. if ($rr == 1) {
  206. api_set_setting('vchamilo_cron_lasthost', $vhost->id);
  207. echo "Round Robin : ".$vhost->root_web."\n";
  208. if ($VCRON->ACTIVATION == 'cli') {
  209. exec_vhost_cron($vhost);
  210. } else {
  211. fire_vhost_cron($vhost);
  212. }
  213. die('Done.');
  214. }
  215. if ($vhost->id == api_get_setting('vchamilo_cron_lasthost', 'vchamilo')) {
  216. $rr = 1; // take next one
  217. }
  218. }
  219. // We were at last. Loop back and take first.
  220. $firsthost = (object) $allvhosts[0];
  221. api_set_setting('vchamilo_cron_lasthost', $firsthost->id, 'vchamilo');
  222. echo "Round Robin : ".$firsthost->root_web."\n";
  223. if ($VCRON->ACTIVATION == 'cli') {
  224. exec_vhost_cron($firsthost);
  225. } else {
  226. fire_vhost_cron($firsthost);
  227. }
  228. } elseif ($VCRON->STRATEGY == LOWEST_POSSIBLE_GAP) {
  229. // First make measurement of cron period.
  230. if (api_get_setting('vcrontickperiod', 'vchamilo')) {
  231. api_set_setting('vcrontime', time(), 'vchamilo');
  232. return;
  233. }
  234. api_set_setting('vcrontickperiod', time() - api_get_setting('vcrontime', 'vchamilo'), 'vchamilo');
  235. $hostsperturn = max(1, $VCRON->PERIOD / api_get_setting('vcrontickperiod', 'vchamilo') * count($allvhosts));
  236. $i = 0;
  237. foreach ($allvhosts as $vhostassoc) {
  238. $vhost = (object) $vhostassoc;
  239. if ((time() - $vhost->lastcron) > $VCRON->PERIOD) {
  240. if ($VCRON->ACTIVATION == 'cli') {
  241. exec_vhost_cron($vhost);
  242. } else {
  243. fire_vhost_cron($vhost);
  244. }
  245. $i++;
  246. if ($i >= $hostsperturn) {
  247. return;
  248. }
  249. }
  250. }
  251. }