1u commited on
Commit
433e445
·
verified ·
1 Parent(s): 1436d29

Upload sync_data.py

Browse files
Files changed (1) hide show
  1. sync_data.py +29 -16
sync_data.py CHANGED
@@ -1,7 +1,8 @@
1
  import os
 
2
  import shutil
3
  import time
4
- import threading
5
  from pathlib import Path
6
  from datetime import datetime
7
  from huggingface_hub import HfApi, hf_hub_download, list_repo_files
@@ -12,6 +13,15 @@ SYNC_INTERVAL = int(os.environ.get("SYNC_INTERVAL", 300))
12
  ARCHIVE_NAME = "openclaw_backup.tar.gz"
13
 
14
  api = HfApi()
 
 
 
 
 
 
 
 
 
15
 
16
 
17
  def download_from_dataset():
@@ -59,25 +69,28 @@ def upload_to_dataset():
59
  print(f"[{datetime.now()}] Upload failed: {e}")
60
 
61
 
62
- def sync_loop():
63
- while True:
 
 
64
  time.sleep(SYNC_INTERVAL)
65
  upload_to_dataset()
66
 
67
 
68
- def main():
 
 
 
 
 
69
  print(f"Dataset: {DATASET_REPO_ID}")
70
  print(f"Data dir: {DATA_DIR}")
71
- print(f"Sync interval: {SYNC_INTERVAL}s")
72
-
73
- download_from_dataset()
74
 
75
- sync_thread = threading.Thread(target=sync_loop, daemon=True)
76
- sync_thread.start()
77
-
78
- while True:
79
- time.sleep(3600)
80
-
81
-
82
- if __name__ == "__main__":
83
- main()
 
1
  import os
2
+ import sys
3
  import shutil
4
  import time
5
+ import signal
6
  from pathlib import Path
7
  from datetime import datetime
8
  from huggingface_hub import HfApi, hf_hub_download, list_repo_files
 
13
  ARCHIVE_NAME = "openclaw_backup.tar.gz"
14
 
15
  api = HfApi()
16
+ running = True
17
+
18
+
19
+ def signal_handler(signum, frame):
20
+ global running
21
+ print(f"[{datetime.now()}] Received signal {signum}, uploading before exit...")
22
+ upload_to_dataset()
23
+ running = False
24
+ sys.exit(0)
25
 
26
 
27
  def download_from_dataset():
 
69
  print(f"[{datetime.now()}] Upload failed: {e}")
70
 
71
 
72
+ def upload_loop():
73
+ signal.signal(signal.SIGTERM, signal_handler)
74
+ signal.signal(signal.SIGINT, signal_handler)
75
+ while running:
76
  time.sleep(SYNC_INTERVAL)
77
  upload_to_dataset()
78
 
79
 
80
+ if __name__ == "__main__":
81
+ if len(sys.argv) < 2:
82
+ print("Usage: python sync_data.py [download|upload_loop]")
83
+ sys.exit(1)
84
+
85
+ cmd = sys.argv[1]
86
  print(f"Dataset: {DATASET_REPO_ID}")
87
  print(f"Data dir: {DATA_DIR}")
 
 
 
88
 
89
+ if cmd == "download":
90
+ download_from_dataset()
91
+ elif cmd == "upload_loop":
92
+ print(f"Sync interval: {SYNC_INTERVAL}s")
93
+ upload_loop()
94
+ else:
95
+ print(f"Unknown command: {cmd}")
96
+ sys.exit(1)