transaction.cron.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * This script should be called by cron.d to update items based on stored
  4. * transactions
  5. * @package chamilo.migration
  6. */
  7. /**
  8. * Init
  9. */
  10. if (PHP_SAPI != 'cli') {
  11. exit;
  12. }
  13. $num_trans = 500; //how many transactions to process during each execution
  14. // Check for pidfile. Normally, no concurrent write is possible as transactions
  15. // should be started at about 120 seconds intervals.
  16. $pidfile = __DIR__.'/chamilo.transaction.pid';
  17. $pid = getmypid();
  18. if (is_file($pidfile)) {
  19. $pid = file_get_contents($pidfile);
  20. error_log("Transaction run frozen: PID file already exists with PID $pid in $pidfile");
  21. die('PID exists - Transaction run frozen');
  22. } else {
  23. $res = file_put_contents($pidfile,$pid);
  24. if ($res === false) {
  25. error_log('Failed writing PID file - Transaction run frozen');
  26. die('Failed writing PID file - Transaction run frozen');
  27. }
  28. error_log('Written PID file with PID '.$pid.'. Now starting transaction run');
  29. }
  30. $cron_start = time();
  31. require_once dirname(__FILE__).'/../../main/inc/global.inc.php';
  32. require_once 'config.php';
  33. require_once 'migration.class.php';
  34. // If the script if called with a 'fix' argument, then deal with it differently
  35. // (do not call the webservice, but instead try to re-execute previously
  36. // failed transactions). The default mode is "process".
  37. $modes = array('fix','process');
  38. $mode = 'process';
  39. if (($argc < 2) or empty($argv[1])) {
  40. error_log('No mode provided for transaction cron.d process in '.__FILE__.', assuming "process"');
  41. } elseif (!in_array($argv[1],$modes)) {
  42. error_log('Mode '.$argv[1].' not recognized in '.__FILE__);
  43. //die();
  44. } else {
  45. $mode = $argv[1];
  46. }
  47. $branch_id = 0;
  48. // We need $branch_id defined before calling db_matches.php
  49. // The only thing we need from db_matches is the definition of the web service
  50. require_once 'db_matches.php';
  51. /**
  52. * Process
  53. */
  54. $migration = new Migration();
  55. $migration->set_web_service_connection_info($matches);
  56. require $migration->web_service_connection_info['filename'];
  57. $mig = new $migration->web_service_connection_info['class'];
  58. error_log('Building in-memory data_list for speed-up '.time());
  59. $data_list = array('boost_users'=>true, 'boost_courses'=>true, 'boost_sessions'=>true);
  60. /**
  61. * Build an array of in-memory database data to reduce time spent querying
  62. */
  63. if (count($data_list['users'])<1) {
  64. MigrationCustom::fill_data_list($data_list);
  65. }
  66. error_log('Built in-memory data_list for speed-up '.time());
  67. // Counter for transactions found and dealt with
  68. $count_transactions = 0;
  69. /**
  70. * Check each branch for transactions to execute (and execute them)
  71. */
  72. $branches = $migration->get_branches();
  73. foreach ($branches as $id => $branch) {
  74. $response = '';
  75. $branch_id = $branch['branch_id'];
  76. if ($mode == 'process') {
  77. //Load transactions saved before
  78. $params = array('branch_id' => $branch_id, 'number_of_transactions' => $num_trans);
  79. $migration->get_transactions_from_webservice($params);
  80. $count_transactions += $migration->execute_transactions($params);
  81. } else {
  82. //if mode==fix
  83. error_log('Fixing transactions');
  84. $params = array('branch_id' => $branch_id, 'number_of_transactions' => $num_trans);
  85. $migration->execute_transactions($params);
  86. }
  87. }
  88. /**
  89. * Free the PID file used as semaphore
  90. */
  91. if (is_file($pidfile)) {
  92. $opid = trim(file_get_contents($pidfile));
  93. if (intval($opid) == intval($pid)) {
  94. $res = @exec('rm '.$pidfile);
  95. if ($res === false) {
  96. error_log('Could not delete PID file');
  97. die('Could not delete PID file');
  98. }
  99. error_log('PID file deleted for PID '.$pid);
  100. error_log(str_repeat('=',40));
  101. } else {
  102. error_log('PID file is not of current process. Not deleting.');
  103. die('PID file is not of current process. Not deleting.'."\n");
  104. }
  105. }
  106. $cron_total = time() - $cron_start;
  107. error_log('Total time taken for transaction run: '.$cron_total.'s for '.$count_transactions.' transactions');