perf: update chrome applets hang (#14353)

* perf: update chrome applets hang

* perf: remove debug print

---------

Co-authored-by: Eric <xplzv@126.com>
pull/14373/head
fit2bot 1 month ago committed by GitHub
parent af91b6faeb
commit d30de0b6a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,3 +1,7 @@
#2024-10-24 Version 1.1
## 功能优化
- 优化快速点击造成页面卡住的问题
#2023-09-18 Version 1.0
## Bug 修复
- 移除窗口最小化,避免造成部分页面元素定位失败

@ -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:
for pid in pids_status:
pids_status[pid] = check_pid_alive(pid)
status = any(pids_status.values())
if not status:
break
print("ret: ", ret)
self.close()
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

@ -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

Loading…
Cancel
Save