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()
|
||||
|
||||
+9
-3
@@ -14,15 +14,20 @@ from __future__ import annotations
|
||||
import sys
|
||||
import time
|
||||
|
||||
from _lib import ROOT, build_env, load_config, popen, run, wait_port, write_env
|
||||
from _lib import build_env, free_ports, load_config, popen, run, run_retry, wait_port, write_env
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = sys.argv[1:]
|
||||
cfg = load_config()
|
||||
app = cfg["app"]
|
||||
env = build_env(cfg)
|
||||
write_env(env)
|
||||
|
||||
# 0. 释放上次残留的本地服务(同时解除对 Prisma 引擎 DLL 的占用,避免 generate EPERM)
|
||||
free_ports([app["api_port"], app["admin_port"], app["h5_port"]])
|
||||
time.sleep(1.5)
|
||||
|
||||
if "--install" in args:
|
||||
run("pnpm install")
|
||||
|
||||
@@ -35,8 +40,9 @@ def main() -> None:
|
||||
# 2. 构建共享包
|
||||
run("pnpm build:shared")
|
||||
|
||||
# 3. 数据库迁移 + 种子
|
||||
run("pnpm --filter @artveris/api prisma:generate")
|
||||
# 3. 数据库迁移 + 种子(generate 偶发被占用,自动重试)
|
||||
if not run_retry("pnpm --filter @artveris/api prisma:generate", attempts=4, delay=2):
|
||||
print("[artveris] 警告:prisma generate 仍失败(客户端可能已存在),继续启动 …")
|
||||
run("pnpm --filter @artveris/api prisma:deploy")
|
||||
if "--no-seed" not in args:
|
||||
run("pnpm --filter @artveris/api seed", check=False)
|
||||
|
||||
Reference in New Issue
Block a user