import json
from pathlib import Path

import psutil


def write_spawn_debug_info(config_dir: Path, spawn_pid, process_name, **extra_info):
    debug_server_dir = config_dir / "dbg-srv"
    debug_server_dir.mkdir(parents=True, exist_ok=True)
    with (debug_server_dir / f"{spawn_pid}.info").open("w") as f:
        extra_info.update(
            {
                "pid": spawn_pid,
                "create_time": psutil.Process(spawn_pid).create_time(),
                "name": process_name,
            }
        )
        json.dump(extra_info, f)
