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

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

parent a8007006
...@@ -31,8 +31,7 @@ public class PlaywrightManagerImpl implements PlaywrightManager { ...@@ -31,8 +31,7 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
private static final long CONTEXT_TIMEOUT = 30 * 60 * 1000; private static final long CONTEXT_TIMEOUT = 30 * 60 * 1000;
// 清理任务调度器 // 清理任务调度器
private final ScheduledExecutorService cleanupScheduler = private final ScheduledExecutorService cleanupScheduler = Executors.newSingleThreadScheduledExecutor(r -> {
Executors.newSingleThreadScheduledExecutor(r -> {
Thread t = new Thread(r, "PlaywrightCleanupScheduler"); Thread t = new Thread(r, "PlaywrightCleanupScheduler");
t.setDaemon(true); // 设置为守护线程 t.setDaemon(true); // 设置为守护线程
return t; return t;
...@@ -66,6 +65,18 @@ public class PlaywrightManagerImpl implements PlaywrightManager { ...@@ -66,6 +65,18 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
"--disable-gpu", "--disable-gpu",
"--remote-allow-origins=*"))); "--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分钟检查一次超时的用户上下文 // 每5分钟检查一次超时的用户上下文
cleanupScheduler.scheduleAtFixedRate(this::cleanupExpiredContexts, cleanupScheduler.scheduleAtFixedRate(this::cleanupExpiredContexts,
5, 3600, TimeUnit.MINUTES); 5, 3600, TimeUnit.MINUTES);
...@@ -83,10 +94,10 @@ public class PlaywrightManagerImpl implements PlaywrightManager { ...@@ -83,10 +94,10 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
// 移除@PostConstruct注解,避免在Spring初始化时自动调用 // 移除@PostConstruct注解,避免在Spring初始化时自动调用
/* /*
@PostConstruct * @PostConstruct
public void initialize() { * public void initialize() {
lazyInitialize(); * lazyInitialize();
} * }
*/ */
@Override @Override
...@@ -112,7 +123,10 @@ public class PlaywrightManagerImpl implements PlaywrightManager { ...@@ -112,7 +123,10 @@ public class PlaywrightManagerImpl implements PlaywrightManager {
lazyInitialize(); lazyInitialize();
Browser.NewContextOptions options = new Browser.NewContextOptions() Browser.NewContextOptions options = new Browser.NewContextOptions()
.setViewportSize(1920, 1080) // 设置视口大小为全高清分辨率,适用于Windows 11桌面环境 .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); return getUserContext(userId, options);
} }
......
...@@ -2,10 +2,10 @@ ...@@ -2,10 +2,10 @@
spring: spring:
# 开发环境数据源配置 # 开发环境数据源配置
datasource: datasource:
url: jdbc:h2:file:./data/hiagent_dev_db;DB_CLOSE_ON_EXIT=FALSE url: jdbc:mysql://${DB_HOST:127.0.0.1}:3306/hiagent?allowMultiQueries=true&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai
driver-class-name: org.h2.Driver driver-class-name: ${DB_DRIVER:com.mysql.cj.jdbc.Driver}
username: sa username: ${DB_NAME:root}
password: sa password: ${DB_PASSWORD:password}
# 开发环境JPA配置 # 开发环境JPA配置
jpa: jpa:
...@@ -21,7 +21,7 @@ spring: ...@@ -21,7 +21,7 @@ spring:
init: init:
schema-locations: classpath:schema.sql schema-locations: classpath:schema.sql
data-locations: classpath:data.sql data-locations: classpath:data.sql
mode: always # 总是执行创建表和数据脚本,实现重新初始化 mode: never # 总是执行创建表和数据脚本,实现重新初始化
# 开启H2控制台 # 开启H2控制台
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 { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue"; import vue from "@vitejs/plugin-vue";
import path from "path"; import path from "path";
...@@ -30,14 +36,14 @@ export default defineConfig({ ...@@ -30,14 +36,14 @@ export default defineConfig({
}, },
proxy: { proxy: {
"/api": { "/api": {
// target: "http://localhost:8080", target: "http://localhost:8080",
target: "http://agent-backend.clouddev.hisense.com", // target: "http://agent-backend.clouddev.hisense.com",
changeOrigin: true, changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, "/api"), rewrite: (path) => path.replace(/^\/api/, "/api"),
}, },
"/ws": { "/ws": {
// target: "http://localhost:8080", target: "http://localhost:8080",
target: "http://agent-backend.clouddev.hisense.com", // target: "http://agent-backend.clouddev.hisense.com",
ws: true, // 启用WebSocket代理 ws: true, // 启用WebSocket代理
changeOrigin: true, 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