Commit 2a026455 authored by 王舵's avatar 王舵

feat: 监听browser连接断开,重新启动 browser

parent a8007006
......@@ -31,8 +31,7 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
private static final long CONTEXT_TIMEOUT = 30 * 60 * 1000;
// 清理任务调度器
private final ScheduledExecutorService cleanupScheduler =
Executors.newSingleThreadScheduledExecutor(r -> {
private final ScheduledExecutorService cleanupScheduler = Executors.newSingleThreadScheduledExecutor(r -> {
Thread t = new Thread(r, "PlaywrightCleanupScheduler");
t.setDaemon(true); // 设置为守护线程
return t;
......@@ -66,6 +65,18 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
"--disable-gpu",
"--remote-allow-origins=*")));
this.browser.onDisconnected((browser) -> {
log.info("浏览器实例已断开连接");
this.browser.close();
this.browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(true)
.setArgs(java.util.Arrays.asList(
"--no-sandbox",
"--disable-dev-shm-usage",
"--disable-gpu",
"--remote-allow-origins=*")));
});
// 每5分钟检查一次超时的用户上下文
cleanupScheduler.scheduleAtFixedRate(this::cleanupExpiredContexts,
5, 3600, TimeUnit.MINUTES);
......@@ -83,10 +94,10 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
// 移除@PostConstruct注解,避免在Spring初始化时自动调用
/*
@PostConstruct
public void initialize() {
lazyInitialize();
}
* @PostConstruct
* public void initialize() {
* lazyInitialize();
* }
*/
@Override
......@@ -112,7 +123,10 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
lazyInitialize();
Browser.NewContextOptions options = new Browser.NewContextOptions()
.setViewportSize(1920, 1080) // 设置视口大小为全高清分辨率,适用于Windows 11桌面环境
.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"); // 设置用户代理为Windows 11 Chrome浏览器
.setUserAgent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"); // 设置用户代理为Windows
// 11
// Chrome浏览器
return getUserContext(userId, options);
}
......
......@@ -2,10 +2,10 @@
spring:
# 开发环境数据源配置
datasource:
url: jdbc:h2:file:./data/hiagent_dev_db;DB_CLOSE_ON_EXIT=FALSE
driver-class-name: org.h2.Driver
username: sa
password: sa
url: jdbc:mysql://${DB_HOST:127.0.0.1}:3306/hiagent?allowMultiQueries=true&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai
driver-class-name: ${DB_DRIVER:com.mysql.cj.jdbc.Driver}
username: ${DB_NAME:root}
password: ${DB_PASSWORD:password}
# 开发环境JPA配置
jpa:
......@@ -21,7 +21,7 @@ spring:
init:
schema-locations: classpath:schema.sql
data-locations: classpath:data.sql
mode: always # 总是执行创建表和数据脚本,实现重新初始化
mode: never # 总是执行创建表和数据脚本,实现重新初始化
# 开启H2控制台
h2:
......
/*
* @Date: 2025-12-29 14:42:27
* @LastEditors: wangduo3 wangduo3@hisense.com
* @LastEditTime: 2025-12-29 16:56:52
* @FilePath: /pangea-agent/frontend/vite.config.ts
*/
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
......@@ -30,14 +36,14 @@ export default defineConfig({
},
proxy: {
"/api": {
// target: "http://localhost:8080",
target: "http://agent-backend.clouddev.hisense.com",
target: "http://localhost:8080",
// target: "http://agent-backend.clouddev.hisense.com",
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, "/api"),
},
"/ws": {
// target: "http://localhost:8080",
target: "http://agent-backend.clouddev.hisense.com",
target: "http://localhost:8080",
// target: "http://agent-backend.clouddev.hisense.com",
ws: true, // 启用WebSocket代理
changeOrigin: true,
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment