sync.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. var sync_db_list = [
  2. { script: "sync/table_article.php", count: -1, finished: 0, enable: false },
  3. { script: "sync/table_term.php", count: -1, finished: 0, enable: false },
  4. { script: "sync/table_article_collect.php", count: -1, finished: 0, enable: false },
  5. { script: "sync/table_channel.php", count: -1, finished: 0, enable: false },
  6. { script: "sync/table_sentence.php", count: -1, finished: 0, enable: true },
  7. ];
  8. var isStop = false;
  9. var sync_curr_do_db = 0;
  10. function sync_index_init() {
  11. render_progress();
  12. }
  13. function sync_pull() {
  14. sync_curr_do_db = 0;
  15. isStop = false;
  16. $("#sync_log").html("working"); //
  17. sync_do_db($("#sync_server_address").val(), $("#sync_local_address").val(), 1);
  18. }
  19. function sync_push() {
  20. isStop = false;
  21. sync_curr_do_db = 0;
  22. $("#sync_log").html("working"); //
  23. sync_do_db($("#sync_local_address").val(), $("#sync_server_address").val(), 1);
  24. }
  25. function sync_stop() {
  26. isStop = true;
  27. }
  28. var retryCount = 0;
  29. function sync_do_db(src, dest, time = 1) {
  30. let size = 500;
  31. while (sync_db_list[sync_curr_do_db].enable == false) {
  32. sync_curr_do_db++;
  33. if (sync_curr_do_db >= sync_db_list.length) {
  34. $("#sync_log").html($("#sync_log").html() + "<br>All Done"); //
  35. return;
  36. }
  37. }
  38. if (sync_db_list[sync_curr_do_db].count < 0) {
  39. $.get(
  40. "sync.php",
  41. {
  42. server: src,
  43. localhost: dest,
  44. path: sync_db_list[sync_curr_do_db].script,
  45. time: time,
  46. size: -1,
  47. },
  48. function (data) {
  49. let result;
  50. try {
  51. result = JSON.parse(data);
  52. sync_db_list[sync_curr_do_db].count = parseInt(result.data);
  53. sync_do_db(src, dest, time);
  54. } catch (error) {
  55. console.error(error + " data:" + data);
  56. return;
  57. }
  58. }
  59. );
  60. } else {
  61. $.get(
  62. "sync.php",
  63. {
  64. server: src,
  65. localhost: dest,
  66. path: sync_db_list[sync_curr_do_db].script,
  67. time: time,
  68. size: size,
  69. },
  70. function (data) {
  71. let result;
  72. try {
  73. result = JSON.parse(data);
  74. } catch (error) {
  75. console.error(error + " data:" + data);
  76. return;
  77. }
  78. $("#sync_log").html(
  79. $("#sync_log").html() +
  80. "<div><h2>" +
  81. sync_db_list[sync_curr_do_db].script +
  82. "</h2>" +
  83. result.message +
  84. "</div>"
  85. ); //
  86. render_progress();
  87. if (isStop) {
  88. return;
  89. }
  90. if (result.error > 0 && retryCount < 2) {
  91. retryCount++;
  92. sync_do_db(src, dest, time);
  93. return;
  94. }
  95. retryCount = 0;
  96. sync_db_list[sync_curr_do_db].finished += parseInt(result.src_row);
  97. if (result.src_row >= size) {
  98. //没弄完,接着弄
  99. sync_do_db(src, dest, result.time);
  100. } else {
  101. sync_curr_do_db++;
  102. if (sync_curr_do_db < sync_db_list.length) {
  103. while (sync_db_list[sync_curr_do_db].enable == false) {
  104. sync_curr_do_db++;
  105. if (sync_curr_do_db >= sync_db_list.length) {
  106. $("#sync_log").html($("#sync_log").html() + "<br>All Done"); //
  107. return;
  108. }
  109. }
  110. sync_do_db(src, dest, 1);
  111. } else {
  112. $("#sync_log").html($("#sync_log").html() + "<br>All Done"); //
  113. }
  114. }
  115. }
  116. );
  117. }
  118. }
  119. function render_progress() {
  120. let html = "";
  121. for (const iterator of sync_db_list) {
  122. let spanWidth = parseInt((500 * iterator.finished) / iterator.count);
  123. html +=
  124. "<div style='width:500px;background-color:white;color:black;'><span style='background-color:green;display:inline-block;width:" +
  125. spanWidth +
  126. "px;'>" +
  127. iterator.script +
  128. "|" +
  129. iterator.finished +
  130. "/" +
  131. iterator.count +
  132. "<span></div>";
  133. }
  134. $("#sync_result").html(html);
  135. }
  136. function login() {
  137. $("#server_msg").html("正在登录<br>");
  138. $.post(
  139. "login.php",
  140. {
  141. userid: $("#userid").val(),
  142. password: $("#password").val(),
  143. server: $("#sync_server_address").val(),
  144. },
  145. function (data) {
  146. let result = JSON.parse(data);
  147. $("#server_msg").html(result.message);
  148. }
  149. );
  150. }