From d30de0b6a0efe95b99a885db25a4442322d6393f Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:19:15 +0800 Subject: [PATCH] perf: update chrome applets hang (#14353) * perf: update chrome applets hang * perf: remove debug print --------- Co-authored-by: Eric --- apps/terminal/applets/chrome/ChangeLog | 4 ++ apps/terminal/applets/chrome/app.py | 53 ++++++++++++++++------- apps/terminal/applets/chrome/manifest.yml | 2 +- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/apps/terminal/applets/chrome/ChangeLog b/apps/terminal/applets/chrome/ChangeLog index 9439008bc..6b0c079b9 100644 --- a/apps/terminal/applets/chrome/ChangeLog +++ b/apps/terminal/applets/chrome/ChangeLog @@ -1,3 +1,7 @@ +#2024-10-24 Version 1.1 +## 功能优化 + - 优化快速点击造成页面卡住的问题 + #2023-09-18 Version 1.0 ## Bug 修复 - 移除窗口最小化,避免造成部分页面元素定位失败 diff --git a/apps/terminal/applets/chrome/app.py b/apps/terminal/applets/chrome/app.py index 487cd2a07..c78f31953 100644 --- a/apps/terminal/applets/chrome/app.py +++ b/apps/terminal/applets/chrome/app.py @@ -228,6 +228,8 @@ def default_chrome_driver_options(): "profile.password_manager_enabled": False } options.add_experimental_option("prefs", prefs) + # chromedriver 退出后也不关闭浏览器 + options.add_experimental_option("detach", True) options.add_experimental_option("excludeSwitches", ['enable-automation']) return options @@ -237,6 +239,7 @@ class AppletApplication(BaseApplication): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.driver = None + self.service = None self.app = WebAPP(app_name=self.app_name, user=self.user, account=self.account, asset=self.asset, platform=self.platform) self._tmp_user_dir = tempfile.TemporaryDirectory() @@ -251,10 +254,10 @@ class AppletApplication(BaseApplication): @wrapper_progress_bar def run(self): - service = Service() + self.service = Service() # driver 的 console 终端框不显示 - service.creationflags = CREATE_NO_WINDOW - self.driver = webdriver.Chrome(options=self._chrome_options, service=service) + self.service.creationflags = CREATE_NO_WINDOW + self.driver = webdriver.Chrome(options=self._chrome_options, service=self.service) self.driver.implicitly_wait(10) if self.app.asset.address != "": ok = self.app.execute(self.driver) @@ -263,21 +266,19 @@ class AppletApplication(BaseApplication): self.driver.maximize_window() def wait(self): - disconnected_msg = "Unable to evaluate script: disconnected: not connected to DevTools\n" - closed_msg = "Unable to evaluate script: no such window: target window already closed" - + from common import check_pid_alive + parent_id = self.service.process.pid + pids = get_children_pids(parent_id) + pids_status = {pid: True for pid in pids} + # 退出 chromedriver 进程,等待所有子进程退出 + self.service.stop() while True: time.sleep(5) - logs = self.driver.get_log('driver') - if len(logs) == 0: - continue - ret = logs[-1] - if isinstance(ret, dict): - message = ret.get('message', '') - if disconnected_msg in message or closed_msg in message: - break - print("ret: ", ret) - self.close() + for pid in pids_status: + pids_status[pid] = check_pid_alive(pid) + status = any(pids_status.values()) + if not status: + break def close(self): if self.driver: @@ -290,3 +291,23 @@ class AppletApplication(BaseApplication): self._tmp_user_dir.cleanup() except Exception as e: print(e) + + +def get_children_pids(parent_pid): + import subprocess + from common import decode_content + pids = [] + # wmic process where (ParentProcessId={5088}) get ProcessId + try: + cmd = ['wmic', 'process', 'where', f'(ParentProcessId={parent_pid})', 'get', 'ProcessId'] + ret = subprocess.check_output(cmd, creationflags=CREATE_NO_WINDOW) + content = decode_content(ret) + content_list = content.strip().split("\r\n") + for pid in content_list[1:]: + pid = pid.strip() + if pid: + pids.append(int(pid)) + + except Exception as e: + pass + return pids diff --git a/apps/terminal/applets/chrome/manifest.yml b/apps/terminal/applets/chrome/manifest.yml index 26864111d..15bfdbf12 100644 --- a/apps/terminal/applets/chrome/manifest.yml +++ b/apps/terminal/applets/chrome/manifest.yml @@ -1,6 +1,6 @@ name: chrome display_name: "{{ 'Chrome Browser' | trans }}" -version: 1.0 +version: 1.1 comment: "{{ 'Chrome Browser Open URL Page Address' | trans }}" author: JumpServer Team exec_type: python