PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/v1.9/watchdog.php

https://bitbucket.org/rev22/timekoin
PHP | 242 lines | 163 code | 42 blank | 37 comment | 33 complexity | 32aa4c00c8a1e60546408619e72674e7 MD5 | raw file
  1. <?PHP
  2. include 'configuration.php';
  3. include 'function.php';
  4. if($_GET["action"]=="begin_watchdog")
  5. {
  6. if(mysql_connect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD) == FALSE)
  7. {
  8. // Database connect error
  9. $datbase_error = TRUE;
  10. }
  11. if(mysql_select_db(MYSQL_DATABASE) == FALSE)
  12. {
  13. // Database select error
  14. $datbase_error = TRUE;
  15. }
  16. // Check last heartbeat and make sure it was more than X seconds ago
  17. $watchdog_heartbeat_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_heartbeat_active' LIMIT 1"),0,"field_data");
  18. if($watchdog_heartbeat_active == FALSE && $datbase_error == FALSE)
  19. {
  20. mysql_query("UPDATE `main_loop_status` SET `field_data` = '" . time() . "' WHERE `main_loop_status`.`field_name` = 'watchdog_last_heartbeat' LIMIT 1");
  21. // Set loop at active now
  22. mysql_query("UPDATE `main_loop_status` SET `field_data` = '1' WHERE `main_loop_status`.`field_name` = 'watchdog_heartbeat_active' LIMIT 1");
  23. source_bg('watchdog.php', TRUE);
  24. header("Location: index.php?menu=system&code=2");
  25. exit;
  26. }
  27. else
  28. {
  29. header("Location: index.php?menu=system&code=89");
  30. exit;
  31. }
  32. }
  33. $mysql_link = mysql_connect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD);
  34. mysql_select_db(MYSQL_DATABASE);
  35. while(1)
  36. {
  37. // Are we to remain active?
  38. $loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_heartbeat_active' LIMIT 1"),0,"field_data");
  39. if($loop_active === FALSE) // Databaes Error
  40. {
  41. // Database Error, try to re-establish a connection after 5 seconds
  42. mysql_close($mysql_link);
  43. sleep(5);
  44. $mysql_link = mysql_connect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD);
  45. mysql_select_db(MYSQL_DATABASE);
  46. // Keep track of errors in case this can't be recovered from
  47. $datbase_error = TRUE;
  48. $database_error_counter++;
  49. }
  50. else
  51. {
  52. $datbase_error = 0;
  53. $database_error_counter = 0;
  54. }
  55. if($loop_active == 1)
  56. {
  57. // Main loop work goes below
  58. // Set the working status of 2
  59. mysql_query("UPDATE `main_loop_status` SET `field_data` = '2' WHERE `main_loop_status`.`field_name` = 'watchdog_heartbeat_active' LIMIT 1");
  60. //*****************************************************************************************************
  61. //*****************************************************************************************************
  62. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_heartbeat_active' LIMIT 1"),0,"field_data");
  63. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_last_heartbeat' LIMIT 1"),0,"field_data");
  64. if($script_loop_active > 0)
  65. {
  66. // Main should still be active
  67. if((time() - $script_last_heartbeat) > 30) // Greater than triple the loop time, something is wrong
  68. {
  69. // Main stop was unexpected
  70. write_log("Main Timekoin Processor is Inactive or Failed, will need a manual restart...", "WA");
  71. }
  72. }
  73. sleep(3);
  74. //*****************************************************************************************************
  75. //*****************************************************************************************************
  76. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'generation_heartbeat_active' LIMIT 1"),0,"field_data");
  77. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'generation_last_heartbeat' LIMIT 1"),0,"field_data");
  78. if($script_loop_active > 0)
  79. {
  80. // Generation should still be active
  81. if((time() - $script_last_heartbeat) > 75)
  82. {
  83. write_log("Generation Clerk has become Stuck, going to Reset...", "WA");
  84. // Possible script failure, try reset the database to let it continue in the next loop
  85. mysql_query("UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'generation_heartbeat_active' LIMIT 1");
  86. }
  87. }
  88. sleep(3);
  89. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'treasurer_heartbeat_active' LIMIT 1"),0,"field_data");
  90. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'treasurer_last_heartbeat' LIMIT 1"),0,"field_data");
  91. if($script_loop_active > 0)
  92. {
  93. // Treasurer should still be active
  94. if((time() - $script_last_heartbeat) > 75)
  95. {
  96. write_log("Treasurer has become Stuck, going to Reset...", "WA");
  97. // Possible script failure, try reset the database to let it continue in the next loop
  98. mysql_query("UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'treasurer_heartbeat_active' LIMIT 1");
  99. }
  100. }
  101. sleep(4);
  102. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'peerlist_heartbeat_active' LIMIT 1"),0,"field_data");
  103. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'peerlist_last_heartbeat' LIMIT 1"),0,"field_data");
  104. if($script_loop_active > 0)
  105. {
  106. // Peerlist should still be active
  107. if((time() - $script_last_heartbeat) > 75)
  108. {
  109. write_log("Peer List Clerk has become Stuck, going to Reset...", "WA");
  110. // Possible script failure, try reset the database to let it continue in the next loop
  111. mysql_query("UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'peerlist_heartbeat_active' LIMIT 1");
  112. }
  113. }
  114. sleep(4);
  115. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'queueclerk_heartbeat_active' LIMIT 1"),0,"field_data");
  116. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'queueclerk_last_heartbeat' LIMIT 1"),0,"field_data");
  117. if($script_loop_active > 0)
  118. {
  119. // Queueclerk should still be active
  120. if((time() - $script_last_heartbeat) > 105)
  121. {
  122. write_log("Transaction Queue Clerk has become Stuck, going to Reset...", "WA");
  123. // Possible script failure, try reset the database to let it continue in the next loop
  124. mysql_query("UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'queueclerk_heartbeat_active' LIMIT 1");
  125. }
  126. }
  127. sleep(4);
  128. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'genpeer_heartbeat_active' LIMIT 1"),0,"field_data");
  129. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'genpeer_last_heartbeat' LIMIT 1"),0,"field_data");
  130. if($script_loop_active > 0)
  131. {
  132. // Genpeer should still be active
  133. if((time() - $script_last_heartbeat) > 105)
  134. {
  135. write_log("Generation Peer Clerk has become Stuck, going to Reset...", "WA");
  136. // Possible script failure, try reset the database to let it continue in the next loop
  137. mysql_query("UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'genpeer_heartbeat_active' LIMIT 1");
  138. }
  139. }
  140. sleep(4);
  141. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'transclerk_heartbeat_active' LIMIT 1"),0,"field_data");
  142. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'transclerk_last_heartbeat' LIMIT 1"),0,"field_data");
  143. if($script_loop_active > 0)
  144. {
  145. // Transclerk should still be active
  146. if((time() - $script_last_heartbeat) > 135)
  147. {
  148. write_log("Transaction Clerk has become Stuck, going to Reset...", "WA");
  149. // Possible script failure, try reset the database to let it continue in the next loop
  150. mysql_query("UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'transclerk_heartbeat_active' LIMIT 1");
  151. }
  152. }
  153. sleep(4);
  154. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'foundation_heartbeat_active' LIMIT 1"),0,"field_data");
  155. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'foundation_last_heartbeat' LIMIT 1"),0,"field_data");
  156. if($script_loop_active > 0)
  157. {
  158. // Foundation should still be active
  159. if((time() - $script_last_heartbeat) > 115)
  160. {
  161. write_log("Foundation Clerk has become Stuck, going to Reset...", "WA");
  162. // Possible script failure, try reset the database to let it continue in the next loop
  163. mysql_query("UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'foundation_heartbeat_active' LIMIT 1");
  164. }
  165. }
  166. sleep(4);
  167. //*****************************************************************************************************
  168. //*****************************************************************************************************
  169. // (Very Last Thing to do in Script)
  170. // Time to wake up and start again
  171. $sql = "UPDATE `main_loop_status` SET `field_data` = '" . time() . "' WHERE `main_loop_status`.`field_name` = 'watchdog_last_heartbeat' LIMIT 1";
  172. mysql_query($sql);
  173. // Mark this loop finished...
  174. $loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_heartbeat_active' LIMIT 1"),0,"field_data");
  175. if($loop_active == 3) // Do a final check to make sure we shouldn't stop running instead
  176. {
  177. // Stop the loop and reset status back to 0
  178. mysql_query("UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'watchdog_heartbeat_active' LIMIT 1");
  179. exit;
  180. }
  181. else
  182. {
  183. mysql_query("UPDATE `main_loop_status` SET `field_data` = '1' WHERE `main_loop_status`.`field_name` = 'watchdog_heartbeat_active' LIMIT 1");
  184. }
  185. } // Check if Active
  186. else
  187. {
  188. // Something is not working right, delay to avoid fast infinite loop
  189. if($datbase_error == TRUE && $database_error_counter < 6)
  190. {
  191. sleep(2);
  192. }
  193. else
  194. {
  195. // Script was called improperly from somewhere or while it was already running, exit to avoid loop stacking
  196. exit;
  197. }
  198. }
  199. }// End infinite loop
  200. ?>