monitor_download_and_ingest.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import os
  2. import time
  3. import paramiko
  4. from dotenv import load_dotenv
  5. repo_root = "c:/Users/lanfr144/Documents/DOPRO1/Antigravity/Food"
  6. load_dotenv(dotenv_path=os.path.join(repo_root, ".env"))
  7. host = os.environ.get('SERVER_HOST', '192.168.130.170')
  8. user = os.environ.get('SERVER_USER', 'francois')
  9. password = os.environ.get('SERVER_PASS', 'BTSai123')
  10. def is_downloading(ssh):
  11. stdin, stdout, stderr = ssh.exec_command("pgrep -f download_csv.sh")
  12. out1 = stdout.read().decode('utf-8').strip()
  13. stdin, stdout, stderr = ssh.exec_command("pgrep -f curl")
  14. out2 = stdout.read().decode('utf-8').strip()
  15. return bool(out1 or out2)
  16. def main():
  17. ssh = paramiko.SSHClient()
  18. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  19. try:
  20. ssh.connect(host, username=user, password=password, timeout=15)
  21. print("Connected! Monitoring download...")
  22. # Monitor download loop
  23. start_time = time.time()
  24. while True:
  25. downloading = is_downloading(ssh)
  26. # Check file size
  27. stdin, stdout, stderr = ssh.exec_command("ls -lh ~/food_project/data/en.openfoodfacts.org.products.csv")
  28. size_out = stdout.read().decode('utf-8').strip()
  29. print(f"[Elapsed: {int(time.time() - start_time)}s] Downloading: {downloading}. File info: {size_out}")
  30. if not downloading:
  31. # Double check to make sure it's really finished
  32. time.sleep(5)
  33. if not is_downloading(ssh):
  34. print("Download has finished!")
  35. break
  36. time.sleep(30)
  37. # Trigger ingestion
  38. print("\nTriggering database ingestion on remote server...")
  39. ssh.exec_command("cd ~/food_project && bash start_batch_ingest.sh")
  40. time.sleep(5)
  41. # Monitor row count growth
  42. print("\nMonitoring database row count:")
  43. for _ in range(6):
  44. stdin, stdout, stderr = ssh.exec_command("docker exec -i food_project-mysql-1 mysql -u food_reader -pBTSai123 food_db -e 'SELECT COUNT(*) FROM products_core;'")
  45. count_out = stdout.read().decode('utf-8').strip()
  46. print(f"Row count: {count_out.replace('mysql: [Warning] Using a password on the command line interface can be insecure.', '').strip()}")
  47. time.sleep(15)
  48. except Exception as e:
  49. print(f"Error: {e}")
  50. finally:
  51. ssh.close()
  52. print("Connection closed.")
  53. if __name__ == "__main__":
  54. main()