fix: account open fire-and-forget; open-browser direct+sync
All checks were successful
技能自动化发布 / release (push) Successful in 8s
All checks were successful
技能自动化发布 / release (push) Successful in 8s
This commit is contained in:
@@ -166,10 +166,33 @@ def cmd_reveal_profile(account_id):
|
||||
)
|
||||
|
||||
|
||||
def _spawn_browser_child(cmd: list[str], *, cwd: str, env: dict) -> subprocess.Popen:
|
||||
"""Detach Playwright child so `account open` can return immediately."""
|
||||
popen_kwargs: dict = {
|
||||
"cwd": cwd,
|
||||
"env": env,
|
||||
"stdin": subprocess.DEVNULL,
|
||||
"stdout": subprocess.DEVNULL,
|
||||
"stderr": subprocess.DEVNULL,
|
||||
"close_fds": True,
|
||||
}
|
||||
if sys.platform == "win32":
|
||||
# Keep browser window; detach from parent console / Skill Action wait.
|
||||
flags = subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS
|
||||
create_no_window = getattr(subprocess, "CREATE_NO_WINDOW", 0)
|
||||
if create_no_window:
|
||||
flags |= create_no_window
|
||||
popen_kwargs["creationflags"] = flags
|
||||
else:
|
||||
popen_kwargs["start_new_session"] = True
|
||||
return subprocess.Popen(cmd, **popen_kwargs)
|
||||
|
||||
|
||||
def cmd_open(account_id):
|
||||
"""
|
||||
打开该账号的持久化浏览器,仅用于肉眼核对。
|
||||
不写数据库、不判定是否已登录。
|
||||
拉起浏览器子进程后立即返回(不等待窗口关闭),便于 Skill Action sync/direct。
|
||||
"""
|
||||
log = get_skill_logger()
|
||||
init_db()
|
||||
@@ -179,26 +202,24 @@ def cmd_open(account_id):
|
||||
if not target:
|
||||
log.warning("browser_open_error account_id=%r not_found", account_id)
|
||||
print("ERROR:ACCOUNT_NOT_FOUND")
|
||||
return
|
||||
sys.exit(1)
|
||||
|
||||
channel = resolve_chromium_channel()
|
||||
if not channel:
|
||||
_print_browser_install_hint()
|
||||
return
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
import playwright # noqa: F401
|
||||
except ImportError:
|
||||
print(
|
||||
"ERROR:需要 playwright Python 包。"
|
||||
)
|
||||
return
|
||||
print("ERROR:需要 playwright Python 包。")
|
||||
sys.exit(1)
|
||||
|
||||
profile_dir = (target.get("profile_dir") or "").strip()
|
||||
if not profile_dir:
|
||||
log.warning("browser_open_error account_id=%s profile_dir_missing", account_id)
|
||||
print("ERROR:PROFILE_DIR_MISSING 账号中 profile_dir 为空。")
|
||||
return
|
||||
sys.exit(1)
|
||||
os.makedirs(profile_dir, exist_ok=True)
|
||||
|
||||
conn = get_conn()
|
||||
@@ -216,28 +237,49 @@ def cmd_open(account_id):
|
||||
)
|
||||
|
||||
browser_name = "Google Chrome" if channel == "chrome" else "Microsoft Edge"
|
||||
print(f"正在打开 [{target['account_label']}] 的 {browser_name}(仅查看,不写入数据库)")
|
||||
print(f"地址:{url}")
|
||||
print("本命令不做登录检测。")
|
||||
|
||||
cfg = {"channel": channel, "profile_dir": profile_dir, "url": url}
|
||||
with tempfile.NamedTemporaryFile(
|
||||
mode="w", suffix=".json", delete=False, encoding="utf-8"
|
||||
) as jf:
|
||||
json.dump(cfg, jf, ensure_ascii=False)
|
||||
cfg_path = jf.name
|
||||
|
||||
try:
|
||||
subprocess.run(
|
||||
child = _spawn_browser_child(
|
||||
[sys.executable, "-m", "service.open_child_runner", cfg_path],
|
||||
cwd=_SCRIPTS_ROOT,
|
||||
env=subprocess_env_with_trace(),
|
||||
)
|
||||
log.info("browser_open_close account_id=%s channel=%s", account_id, channel)
|
||||
except Exception as e:
|
||||
log.error("browser_open_error account_id=%s error=%s", account_id, str(e))
|
||||
raise
|
||||
finally:
|
||||
try:
|
||||
os.unlink(cfg_path)
|
||||
except OSError:
|
||||
pass
|
||||
log.error("browser_open_error account_id=%s error=%s", account_id, str(e))
|
||||
print(f"ERROR:BROWSER_OPEN_FAILED {e}")
|
||||
sys.exit(1)
|
||||
|
||||
log.info(
|
||||
"browser_open_spawned account_id=%s channel=%s pid=%s",
|
||||
account_id,
|
||||
channel,
|
||||
child.pid,
|
||||
)
|
||||
print(
|
||||
json.dumps(
|
||||
{
|
||||
"success": True,
|
||||
"data": {
|
||||
"account_id": int(target["id"]) if target.get("id") is not None else account_id,
|
||||
"account_label": target.get("account_label"),
|
||||
"browser": browser_name,
|
||||
"channel": channel,
|
||||
"profile_dir": os.path.abspath(profile_dir),
|
||||
"url": url,
|
||||
"detached": True,
|
||||
"pid": child.pid,
|
||||
},
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
"""Playwright 仅打开浏览器子进程:由 browser_service.cmd_open 启动,argv[1] 为 JSON 配置路径。"""
|
||||
"""Playwright 仅打开浏览器子进程:由 browser_service.cmd_open 启动,argv[1] 为 JSON 配置路径。
|
||||
|
||||
父进程拉起后即返回;本子进程自行维护浏览器生命周期,读完配置后删除临时 JSON。
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
from playwright.sync_api import sync_playwright
|
||||
@@ -12,8 +16,14 @@ from util.playwright_stealth import (
|
||||
|
||||
|
||||
def main():
|
||||
with open(sys.argv[1], encoding="utf-8") as f:
|
||||
cfg_path = sys.argv[1]
|
||||
with open(cfg_path, encoding="utf-8") as f:
|
||||
c = json.load(f)
|
||||
try:
|
||||
os.unlink(cfg_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
with sync_playwright() as p:
|
||||
args, ignore_automation = persistent_context_launch_parts()
|
||||
lc_kw = dict(
|
||||
|
||||
Reference in New Issue
Block a user