fix: dev.py 启动前释放端口并重试 prisma generate,修复 EPERM
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -96,6 +96,50 @@ def popen(cmd: str, cwd: Path | None = None) -> subprocess.Popen:
|
||||
return subprocess.Popen(cmd, cwd=str(cwd or ROOT), shell=True)
|
||||
|
||||
|
||||
def free_port(port: int) -> None:
|
||||
"""结束本机占用指定端口的进程(用于释放上次的 api/admin/h5 及被锁的 Prisma 引擎)。
|
||||
仅用于本地 node 进程,不要对 Docker 管理的端口使用。"""
|
||||
try:
|
||||
if IS_WINDOWS:
|
||||
out = subprocess.run(
|
||||
"netstat -ano -p tcp", capture_output=True, text=True, shell=True
|
||||
).stdout
|
||||
pids: set[str] = set()
|
||||
for line in out.splitlines():
|
||||
if f":{port} " in line and "LISTENING" in line.upper():
|
||||
pid = line.split()[-1]
|
||||
if pid and pid != "0":
|
||||
pids.add(pid)
|
||||
for pid in pids:
|
||||
subprocess.run(f"taskkill /F /PID {pid}", shell=True, capture_output=True)
|
||||
print(f"[artveris] 释放端口 {port}(结束 PID {pid})")
|
||||
else:
|
||||
out = subprocess.run(
|
||||
f"lsof -ti tcp:{port}", capture_output=True, text=True, shell=True
|
||||
).stdout
|
||||
for pid in out.split():
|
||||
subprocess.run(f"kill -9 {pid}", shell=True, capture_output=True)
|
||||
print(f"[artveris] 释放端口 {port}(结束 PID {pid})")
|
||||
except Exception as e: # noqa: BLE001
|
||||
print(f"[artveris] 释放端口 {port} 失败(忽略):{e}")
|
||||
|
||||
|
||||
def free_ports(ports: list[int]) -> None:
|
||||
for p in ports:
|
||||
free_port(int(p))
|
||||
|
||||
|
||||
def run_retry(cmd: str, cwd: Path | None = None, attempts: int = 4, delay: float = 2.0) -> bool:
|
||||
"""运行命令,失败自动重试(用于规避偶发文件锁,如 Prisma 引擎 DLL 占用)。"""
|
||||
for i in range(attempts):
|
||||
if run(cmd, cwd=cwd, check=False) == 0:
|
||||
return True
|
||||
if i < attempts - 1:
|
||||
print(f"[artveris] 命令失败,{delay} 秒后重试({i + 1}/{attempts - 1})…")
|
||||
time.sleep(delay)
|
||||
return False
|
||||
|
||||
|
||||
def wait_port(host: str, port: int, timeout: int = 60) -> bool:
|
||||
print(f"[artveris] 等待 {host}:{port} 就绪 …")
|
||||
start = time.time()
|
||||
|
||||
Reference in New Issue
Block a user