/xtracks-cronjobs/index.php

https://github.com/ClickBooth/XTracks · PHP · 197 lines · 113 code · 40 blank · 44 comment · 22 complexity · f4a8d306d1857c164d70e895a498e0a5 MD5 · raw file

  1. <? include_once($_SERVER['DOCUMENT_ROOT'] . '/xtracks-app/bootstrap.php');
  2. // Execute crons.
  3. globalCronRun();
  4. function globalCronRun()
  5. {
  6. $dbs = db::getRows('SHOW DATABASES');
  7. foreach($dbs as $db)
  8. {
  9. $db_name = $db['Database'];
  10. if (preg_match('/^prosper_(.+)/', $db_name, $match)) {
  11. if ($match[1] == 'master') {
  12. continue;
  13. } else {
  14. // Run client jobs.
  15. clientCronRun($db_name);
  16. }
  17. }
  18. }
  19. }
  20. function clientCronRun($db)
  21. {
  22. //heres the psuedo cronjobs
  23. if (Run1MinCronjob($db) == true) {
  24. if (RunHourlyCronJob($db) == true) {
  25. RunDailyCronjob($db);
  26. }
  27. }
  28. }
  29. function RunDailyCronjob($db)
  30. {
  31. //check to run the daily cronjob
  32. $now = time();
  33. $today_day = date('j', time());
  34. $today_month = date('n', time());
  35. $today_year = date('Y', time());
  36. //the click_time is recorded in the middle of the day
  37. $cronjob_time = mktime(12,0,0,$today_month,$today_day,$today_year);
  38. $mysql['cronjob_time'] = mysql_real_escape_string($cronjob_time);
  39. $mysql['cronjob_type'] = mysql_real_escape_string('daily');
  40. //check to make sure this click_summary doesn't already exist
  41. $check_sql = "SELECT COUNT(*) FROM $db.202_cronjobs WHERE cronjob_type='".$mysql['cronjob_type']."' AND cronjob_time='".$mysql['cronjob_time']."'";
  42. $check_result = _mysql_query($check_sql);
  43. $check_count = mysql_result($check_result,0,0);
  44. if ($check_count == 0 ) {
  45. //if a cronjob hasn't run today, record it now.
  46. $insert_sql = "INSERT INTO $db.202_cronjobs SET cronjob_type='".$mysql['cronjob_type']."', cronjob_time='".$mysql['cronjob_time']."'";
  47. $insert_result = _mysql_query($insert_sql);
  48. /* -------- THIS CLEARS OUT THE CLICK SPY MEMORY TABLE --------- */
  49. //this function runs everyday at midnight to clear out the temp clicks_memory table
  50. $from = time() - 86400;
  51. //this makes it so we only have the most recent last 24 hour stuff, anything older, kill it.
  52. //we want to keep our SPY TABLE, low
  53. $click_sql = "DELETE FROM $db.202_clicks_spy WHERE click_time < $from";
  54. $click_result = _mysql_query($click_sql);
  55. //$click_count = mysql_affected_rows();
  56. //clear the last 24 hour ip addresses
  57. $last_ip_sql = "DELETE FROM $db.202_last_ips WHERE time < $from";
  58. $last_ip_result = _mysql_query($last_ip_sql);
  59. $last_ip_affected_rows = mysql_affected_rows();
  60. /* -------- THIS CLEARS OUT THE CHART TABLE --------- */
  61. $chart_sql = "DELETE FROM $db.202_charts";
  62. $chart_result = _mysql_query($chart_sql);
  63. //$chart_count = mysql_affected_rows(); */
  64. /* -------- NOW DELETE ALL THE OLD CRONJOB ENTRIES STUFF --------- */
  65. $mysql['cronjob_time'] = $mysql['cronjob_time'] - 86400;
  66. $delete_sql = "DELETE FROM 202_cronjobs WHERE cronjob_time < ".$mysql['cronjob_time']."";
  67. $delete_result = _mysql_query($delete_sql);
  68. return true;
  69. } else {
  70. return false;
  71. }
  72. }
  73. function RunHourlyCronJob($db) {
  74. //check to run the daily cronjob, not currently in-use
  75. $now = time();
  76. $today_day = date('j', time());
  77. $today_month = date('n', time());
  78. $today_year = date('Y', time());
  79. $today_hour = date('G', time());
  80. //the click_time is recorded in the middle of the day
  81. $cronjob_time = mktime($today_hour,0,0,$today_month,$today_day,$today_year);
  82. $mysql['cronjob_time'] = mysql_real_escape_string($cronjob_time);
  83. $mysql['cronjob_type'] = mysql_real_escape_string('hour');
  84. //check to make sure this click_summary doesn't already exist
  85. $check_sql = "SELECT COUNT(*) FROM $db.202_cronjobs WHERE cronjob_type='".$mysql['cronjob_type']."' AND cronjob_time='".$mysql['cronjob_time']."'";
  86. $check_result = _mysql_query($check_sql);
  87. $check_count = mysql_result($check_result,0,0);
  88. if ($check_count == 0 ) {
  89. /*
  90. //if a cronjob hasn't run today, record it now.
  91. $insert_sql = "INSERT INTO 202_cronjobs SET cronjob_type='".$mysql['cronjob_type']."', cronjob_time='".$mysql['cronjob_time']."'";
  92. $insert_result = _mysql_query($insert_sql);
  93. /* -------- CURL THE WEBSITES TO SEE IF UP OR NOT --------- */
  94. /*
  95. $c = new curl("http://party202.com") ;
  96. $c->setopt(CURLOPT_FOLLOWLOCATION, true) ;
  97. echo $c->exec() ;
  98. if ($theError = $c->hasError()) {
  99. mail('5034444444@mobile.att.net','Server Down',$theError);
  100. echo $theError ;
  101. }
  102. $c->close() ;
  103. */
  104. return true;
  105. } else {
  106. return false;
  107. }
  108. }
  109. function Run1MinCronjob($db)
  110. {
  111. //check to run the 1minute cronjob, change this to every minute
  112. $now = time();
  113. $today_minute = date('i', time());
  114. $today_hour = date('G', time());
  115. $today_day = date('j', time());
  116. $today_month = date('n', time());
  117. $today_year = date('Y', time());
  118. $today_minute = ceil($today_minute / 1);
  119. $today_minute = $today_minute * 1;
  120. if ($today_minute == 60) {
  121. $today_minute = 0;
  122. $today_hour++;
  123. if ($today_hour == 24) {
  124. $today_hour = 0;
  125. }
  126. }
  127. //the click_time is recorded in the middle of the day
  128. $cronjob_time = mktime($today_hour,$today_minute,0,$today_month,$today_day,$today_year);
  129. $mysql['cronjob_time'] = mysql_real_escape_string($cronjob_time);
  130. $mysql['cronjob_type'] = mysql_real_escape_string('1min');
  131. //check to make sure this click_summary doesn't already exist
  132. $check_sql = "SELECT COUNT(*) FROM $db.202_cronjobs WHERE cronjob_type='".$mysql['cronjob_type']."' AND cronjob_time='".$mysql['cronjob_time']."'";
  133. $check_result = mysql_query($check_sql) or record_mysql_error($check_sql);
  134. $check_count = mysql_result($check_result,0,0);
  135. if ($check_count == 0 ) {
  136. //if a cronjob hasn't run today, record it now.
  137. $insert_sql = "INSERT INTO $db.202_cronjobs SET cronjob_type='".$mysql['cronjob_type']."', cronjob_time='".$mysql['cronjob_time']."'";
  138. $insert_result = mysql_query($insert_sql);
  139. /* -------- THIS RUNS THE DELAYED QUERIES --------- */
  140. $delayed_sql = "SELECT delayed_sql FROM $db.202_delayed_sqls WHERE delayed_time <=".time();
  141. $delayed_result = _mysql_query($delayed_sql);
  142. while ($delayed_row = mysql_fetch_assoc($delayed_result)) {
  143. //run each sql
  144. $update_sql = $delayed_row['delayed_sql'];
  145. $update_result = _mysql_query($update_sql);
  146. }
  147. //delete all old delayed sqls
  148. $delayed_sql = "DELETE FROM $db.202_delayed_sqls WHERE delayed_time <=".time();
  149. $delayed_result = _mysql_query($delayed_sql);
  150. return true;
  151. } else {
  152. return false;
  153. }
  154. }