import os
import time
from datetime import datetime

# 你的 Python 路径（如无需特定可直接 python3）
PYTHON = "/usr/bin/python3"

# 要执行的脚本
SCRIPTS = [
    "2miners_collector.py",
    "kryptex_collector.py",
    "herominers_collector.py",
    "vipor_collector.py"
]


def get_half_round_sleep():
    now = datetime.now()
    minute = now.minute

    # 计算距离下个半点的秒数
    if minute < 30:
        target = now.replace(minute=30, second=0, microsecond=0)
    else:
        target = now.replace(hour=now.hour + 1, minute=0, second=0, microsecond=0)

    return int((target - now).total_seconds())


def run_scripts():
    print("\n================= 开始执行采集任务 =================")
    for script in SCRIPTS:
        print(f"▶ 正在执行 {script} ...")
        os.system(f"{PYTHON} {script}")  # 如果脚本不在同目录请加绝对路径
        print(f"✔ 执行完成 {script}\n")

    print("================= 当前轮次已完成 =================\n")


if __name__ == "__main__":
    while True:
        run_scripts()
        sleep_time = get_half_round_sleep()
        print(f"⏳ 等待 {sleep_time} 秒进入下个半点执行...\n")
        time.sleep(sleep_time)