arcticaurora commited on
Commit
c5981f2
·
verified ·
1 Parent(s): 276512c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -34
app.py CHANGED
@@ -186,56 +186,79 @@ def run_dump(source_conn: str, file_path: str, options: dict):
186
  stdout=subprocess.PIPE,
187
  stderr=subprocess.PIPE,
188
  env=env,
189
- text=True
 
190
  )
191
 
192
  with migration_lock:
193
  migration_state["process"] = process
194
 
195
- # Process output
196
- for line in process.stderr:
197
- if "Dumping" in line and "table" in line:
198
- table_name = line.split("Dumping")[1].strip().split(" ")[1]
199
- with migration_lock:
200
- migration_state["progress"]["current_table"] = table_name
201
- migration_state["progress"]["tables_completed"] += 1
202
-
203
- log_message(f"Dumping table: {table_name}", "info")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
 
205
  # Wait for process to complete
206
- stdout, stderr = process.communicate()
207
- exit_code = process.returncode
 
 
208
 
209
  if exit_code == 0:
210
  # Verify file exists and has content
211
  if os.path.exists(absolute_file_path):
212
  final_size = os.path.getsize(absolute_file_path)
213
 
214
- with migration_lock:
215
- migration_state["dump_file_size"] = final_size / (1024 * 1024) # Convert to MB
216
- migration_state["progress"]["current_size_mb"] = round(final_size / (1024 * 1024), 2)
217
- migration_state["dump_completed"] = True
218
- migration_state["end_time"] = time.time()
219
- migration_state["running"] = False
220
- migration_state["process"] = None
221
-
222
- total_time = migration_state["end_time"] - migration_state["start_time"]
223
- log_message(
224
- f"Database dump completed successfully. Size: {round(final_size / (1024 * 1024), 2)} MB. Time: {round(total_time, 2)} seconds",
225
- "success"
226
- )
227
- return True
 
 
 
 
228
  else:
229
  log_message(f"Dump completed but file not found: {absolute_file_path}", "error")
230
-
231
- with migration_lock:
232
- migration_state["running"] = False
233
- migration_state["process"] = None
234
-
235
- return False
 
 
 
236
  else:
237
- error_message = stderr or "Unknown error during dump"
238
- log_message(f"Database dump failed: {error_message}", "error")
239
 
240
  with migration_lock:
241
  migration_state["running"] = False
 
186
  stdout=subprocess.PIPE,
187
  stderr=subprocess.PIPE,
188
  env=env,
189
+ text=True,
190
+ bufsize=1 # Line buffered to get real-time output
191
  )
192
 
193
  with migration_lock:
194
  migration_state["process"] = process
195
 
196
+ # Function to read stderr in real-time
197
+ def read_stderr():
198
+ for line in iter(process.stderr.readline, ''):
199
+ line = line.strip()
200
+ if line:
201
+ log_message(line, "info")
202
+ if "Dumping" in line and "table" in line:
203
+ try:
204
+ table_parts = line.split("Dumping")
205
+ if len(table_parts) > 1:
206
+ table_info = table_parts[1].strip()
207
+ table_parts = table_info.split(" ")
208
+ if len(table_parts) > 1:
209
+ table_name = table_parts[1].strip('"')
210
+ with migration_lock:
211
+ migration_state["progress"]["current_table"] = table_name
212
+ migration_state["progress"]["tables_completed"] += 1
213
+ except Exception as e:
214
+ log_message(f"Error parsing table name: {str(e)}", "warning")
215
+
216
+ # Start stderr reading thread
217
+ stderr_thread = threading.Thread(target=read_stderr, daemon=True)
218
+ stderr_thread.start()
219
 
220
  # Wait for process to complete
221
+ exit_code = process.wait()
222
+
223
+ # Wait a moment for the stderr thread to catch up
224
+ stderr_thread.join(timeout=2.0)
225
 
226
  if exit_code == 0:
227
  # Verify file exists and has content
228
  if os.path.exists(absolute_file_path):
229
  final_size = os.path.getsize(absolute_file_path)
230
 
231
+ if final_size > 0:
232
+ # Success - file exists and has content
233
+ with migration_lock:
234
+ migration_state["dump_file_size"] = final_size / (1024 * 1024) # Convert to MB
235
+ migration_state["progress"]["current_size_mb"] = round(final_size / (1024 * 1024), 2)
236
+ migration_state["dump_completed"] = True
237
+ migration_state["end_time"] = time.time()
238
+ migration_state["running"] = False
239
+ migration_state["process"] = None
240
+
241
+ total_time = migration_state["end_time"] - migration_state["start_time"]
242
+ log_message(
243
+ f"Database dump completed successfully. Size: {round(final_size / (1024 * 1024), 2)} MB. Time: {round(total_time, 2)} seconds",
244
+ "success"
245
+ )
246
+ return True
247
+ else:
248
+ log_message(f"Dump file exists but is empty (0 bytes): {absolute_file_path}", "error")
249
  else:
250
  log_message(f"Dump completed but file not found: {absolute_file_path}", "error")
251
+
252
+ # If we get here, something went wrong with the file
253
+ with migration_lock:
254
+ migration_state["dump_completed"] = False
255
+ migration_state["end_time"] = time.time()
256
+ migration_state["running"] = False
257
+ migration_state["process"] = None
258
+
259
+ return False
260
  else:
261
+ log_message(f"Database dump failed with exit code {exit_code}", "error")
 
262
 
263
  with migration_lock:
264
  migration_state["running"] = False