Spaces:
Sleeping
Sleeping
chuan
commited on
Commit
·
e09b7ad
1
Parent(s):
16ceaf4
optimize: shorten sync interval to 4h and implement auto-cleanup for 1GB limit
Browse files- app.py +11 -6
- 服务/数据采集/hf_sync.py +16 -1
app.py
CHANGED
|
@@ -49,7 +49,7 @@ def 启动后台采集():
|
|
| 49 |
print(f"[Collector] {line.strip()}")
|
| 50 |
|
| 51 |
def 周期性整理与同步():
|
| 52 |
-
"""每隔
|
| 53 |
import sys
|
| 54 |
organize_script = os.path.join("服务", "数据采集", "整理行情数据.py")
|
| 55 |
env = os.environ.copy()
|
|
@@ -60,14 +60,19 @@ def 周期性整理与同步():
|
|
| 60 |
time.sleep(60) # 启动后 1 分钟先跑一次
|
| 61 |
print("🕒 开始执行周期性数据整理与同步...")
|
| 62 |
try:
|
| 63 |
-
#
|
| 64 |
subprocess.run([sys.executable, organize_script], env=env, check=True)
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
-
print(f"❌
|
| 68 |
|
| 69 |
-
# 每
|
| 70 |
-
time.sleep(
|
| 71 |
|
| 72 |
# 启动后台线程
|
| 73 |
thread_collector = threading.Thread(target=启动后台采集, daemon=True)
|
|
|
|
| 49 |
print(f"[Collector] {line.strip()}")
|
| 50 |
|
| 51 |
def 周期性整理与同步():
|
| 52 |
+
"""每隔 4 小时执行一次数据整理与 HF 同步"""
|
| 53 |
import sys
|
| 54 |
organize_script = os.path.join("服务", "数据采集", "整理行情数据.py")
|
| 55 |
env = os.environ.copy()
|
|
|
|
| 60 |
time.sleep(60) # 启动后 1 分钟先跑一次
|
| 61 |
print("🕒 开始执行周期性数据整理与同步...")
|
| 62 |
try:
|
| 63 |
+
# 1. 执行整理 (整理后会自动删除原始碎片)
|
| 64 |
subprocess.run([sys.executable, organize_script], env=env, check=True)
|
| 65 |
+
|
| 66 |
+
# 2. 执行云端同步
|
| 67 |
+
print("📤 开始同步到 Hugging Face Dataset...")
|
| 68 |
+
from 服务.数据采集.hf_sync import sync_to_hf
|
| 69 |
+
if sync_to_hf():
|
| 70 |
+
print("✅ 周期性整理与同步完成。")
|
| 71 |
except Exception as e:
|
| 72 |
+
print(f"❌ 周期性整理与同步失败: {e}")
|
| 73 |
|
| 74 |
+
# 每 4 小时运行一次
|
| 75 |
+
time.sleep(4 * 3600)
|
| 76 |
|
| 77 |
# 启动后台线程
|
| 78 |
thread_collector = threading.Thread(target=启动后台采集, daemon=True)
|
服务/数据采集/hf_sync.py
CHANGED
|
@@ -47,7 +47,7 @@ def sync_to_hf():
|
|
| 47 |
return True
|
| 48 |
|
| 49 |
# 上传整个目录
|
| 50 |
-
logger.info(f"
|
| 51 |
api.upload_folder(
|
| 52 |
folder_path=str(LOCAL_DATA_DIR),
|
| 53 |
repo_id=repo_id,
|
|
@@ -56,6 +56,21 @@ def sync_to_hf():
|
|
| 56 |
ignore_patterns=["*.tmp", "*.log"]
|
| 57 |
)
|
| 58 |
logger.info("✅ 同步完成!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
return True
|
| 60 |
except Exception as e:
|
| 61 |
logger.error(f"❌ 同步过程中发生错误: {e}")
|
|
|
|
| 47 |
return True
|
| 48 |
|
| 49 |
# 上传整个目录
|
| 50 |
+
logger.info(f"📤 正在同步数据到 Hugging Face Dataset: {repo_id}...")
|
| 51 |
api.upload_folder(
|
| 52 |
folder_path=str(LOCAL_DATA_DIR),
|
| 53 |
repo_id=repo_id,
|
|
|
|
| 56 |
ignore_patterns=["*.tmp", "*.log"]
|
| 57 |
)
|
| 58 |
logger.info("✅ 同步完成!")
|
| 59 |
+
|
| 60 |
+
# --- 新增:同步成功后清理本地已整理的文件,节省 1GB 空间 ---
|
| 61 |
+
logger.info("🧹 正在清理本地已上传的数据以节省空间...")
|
| 62 |
+
try:
|
| 63 |
+
import shutil
|
| 64 |
+
for item in os.listdir(LOCAL_DATA_DIR):
|
| 65 |
+
item_path = os.path.join(LOCAL_DATA_DIR, item)
|
| 66 |
+
if os.path.isfile(item_path):
|
| 67 |
+
os.remove(item_path)
|
| 68 |
+
elif os.path.isdir(item_path):
|
| 69 |
+
shutil.rmtree(item_path)
|
| 70 |
+
logger.info("✅ 本地已整理数据清理完毕,空间已释放。")
|
| 71 |
+
except Exception as e:
|
| 72 |
+
logger.warning(f"⚠️ 清理本地空间时出错 (非致命): {e}")
|
| 73 |
+
|
| 74 |
return True
|
| 75 |
except Exception as e:
|
| 76 |
logger.error(f"❌ 同步过程中发生错误: {e}")
|