# -*- coding:utf-8 -*-
"""
@Author  : xuxingchen
@Contact : xuxingchen@sinochem.com
@Desc    : 主程序运行入口
"""

import multiprocessing
import subprocess

from config import AUTO_CALLBACK, ENV_FLAG, LAN_IP
from utils import logger


def run_script(script_path):
    # 使用 subprocess 运行脚本
    subprocess.run(['python', script_path])


if __name__ == "__main__":
    logger.Logger.init(f"当前程序环境 ENV_FLAG: {ENV_FLAG}")
    logger.Logger.init(f"当前环境IP: {LAN_IP}")
    # 创建两个进程
    p1 = multiprocessing.Process(target=run_script, args=('backend.py',))
    p2 = multiprocessing.Process(target=run_script, args=('app.py',))
    p3 = multiprocessing.Process(target=run_script, args=('image_server.py',))
    p4 = multiprocessing.Process(target=run_script, args=('auto_callback.py',))

    # 启动进程
    p1.start()
    p2.start()
    p3.start()
    if AUTO_CALLBACK:
        p4.start()

    # 等待进程完成
    p1.join()
    p2.join()
    p3.join()
    if AUTO_CALLBACK:
        p4.join()
