sync.js 3.7 KB

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