mirror of
https://github.com/black-ant/Ant-Browser.git
synced 2026-07-14 18:48:55 +08:00
channel: master
version: 1.2.0
source-ref: D:\code\open_source\ant-chrome master
source-commit: f3d7ec5
merged-public-base: 3d264eb
53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
package backend
|
|
|
|
import (
|
|
"ant-chrome/backend/internal/logger"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
func (a *App) BrowserInstanceStop(profileId string) (*BrowserProfile, error) {
|
|
log := logger.New("Browser")
|
|
a.browserMgr.Mutex.Lock()
|
|
defer a.browserMgr.Mutex.Unlock()
|
|
|
|
profile, exists := a.browserMgr.Profiles[profileId]
|
|
if !exists {
|
|
return nil, fmt.Errorf("profile not found")
|
|
}
|
|
|
|
cmd := a.browserMgr.BrowserProcesses[profileId]
|
|
debugPort := profile.DebugPort
|
|
if tryCloseBrowserViaCDP(debugPort, 5*time.Second) {
|
|
a.markProfileStoppedLocked(profileId, profile)
|
|
log.Info("实例停止", logger.F("profile_id", profileId), logger.F("method", "cdp"), logger.F("debug_port", debugPort))
|
|
return profile, nil
|
|
}
|
|
|
|
if cmd != nil && cmd.Process != nil {
|
|
if err := a.stopBrowserProcess(cmd); err != nil {
|
|
log.Error("实例停止失败", logger.F("profile_id", profileId), logger.F("error", err))
|
|
profile.LastError = err.Error()
|
|
return profile, err
|
|
}
|
|
}
|
|
|
|
if debugPort > 0 && canConnectDebugPort(debugPort, 250*time.Millisecond) {
|
|
err := fmt.Errorf("实例停止失败:浏览器仍在运行(调试端口 %d 仍可访问)", debugPort)
|
|
log.Error("实例停止失败", logger.F("profile_id", profileId), logger.F("debug_port", debugPort), logger.F("reason", err.Error()))
|
|
profile.LastError = err.Error()
|
|
return profile, err
|
|
}
|
|
|
|
a.markProfileStoppedLocked(profileId, profile)
|
|
log.Info("实例停止", logger.F("profile_id", profileId))
|
|
return profile, nil
|
|
}
|
|
|
|
func (a *App) BrowserInstanceRestart(profileId string) (*BrowserProfile, error) {
|
|
if _, err := a.BrowserInstanceStop(profileId); err != nil {
|
|
return nil, err
|
|
}
|
|
return a.BrowserInstanceStart(profileId)
|
|
}
|