feat(web): implement panel-only start/stop methods for in-process restarts

This commit is contained in:
farhadh 2026-05-11 16:18:04 +02:00
parent 548ac7133b
commit 4f80eec11d
No known key found for this signature in database
2 changed files with 40 additions and 18 deletions

View file

@ -81,11 +81,7 @@ func runWebServer() {
case syscall.SIGHUP: case syscall.SIGHUP:
logger.Info("Received SIGHUP signal. Restarting servers...") logger.Info("Received SIGHUP signal. Restarting servers...")
// --- FIX FOR TELEGRAM BOT CONFLICT (409): Stop bot before restart --- err := server.StopPanelOnly()
service.StopBot()
// --
err := server.Stop()
if err != nil { if err != nil {
logger.Debug("Error stopping web server:", err) logger.Debug("Error stopping web server:", err)
} }
@ -96,7 +92,7 @@ func runWebServer() {
server = web.NewServer() server = web.NewServer()
global.SetWebServer(server) global.SetWebServer(server)
err = server.Start() err = server.StartPanelOnly()
if err != nil { if err != nil {
log.Fatalf("Error restarting web server: %v", err) log.Fatalf("Error restarting web server: %v", err)
return return

View file

@ -259,11 +259,13 @@ func (s *Server) initRouter() (*gin.Engine, error) {
// startTask schedules background jobs (Xray checks, traffic jobs, cron // startTask schedules background jobs (Xray checks, traffic jobs, cron
// jobs) which the panel relies on for periodic maintenance and monitoring. // jobs) which the panel relies on for periodic maintenance and monitoring.
func (s *Server) startTask() { func (s *Server) startTask(restartXray bool) {
s.customGeoService.EnsureOnStartup() s.customGeoService.EnsureOnStartup()
err := s.xrayService.RestartXray(true) if restartXray {
if err != nil { err := s.xrayService.RestartXray(true)
logger.Warning("start xray failed:", err) if err != nil {
logger.Warning("start xray failed:", err)
}
} }
// Check whether xray is running every second // Check whether xray is running every second
s.cron.AddJob("@every 1s", job.NewCheckXrayRunningJob()) s.cron.AddJob("@every 1s", job.NewCheckXrayRunningJob())
@ -348,6 +350,15 @@ func (s *Server) startTask() {
// Start initializes and starts the web server with configured settings, routes, and background jobs. // Start initializes and starts the web server with configured settings, routes, and background jobs.
func (s *Server) Start() (err error) { func (s *Server) Start() (err error) {
return s.start(true, true)
}
// StartPanelOnly initializes the panel during an in-process panel restart without cycling Xray.
func (s *Server) StartPanelOnly() (err error) {
return s.start(false, false)
}
func (s *Server) start(restartXray bool, startTgBot bool) (err error) {
// This is an anonymous function, no function name // This is an anonymous function, no function name
defer func() { defer func() {
if err != nil { if err != nil {
@ -427,12 +438,14 @@ func (s *Server) Start() (err error) {
s.httpServer.Serve(listener) s.httpServer.Serve(listener)
}() }()
s.startTask() s.startTask(restartXray)
isTgbotenabled, err := s.settingService.GetTgbotEnabled() if startTgBot {
if (err == nil) && (isTgbotenabled) { isTgbotenabled, err := s.settingService.GetTgbotEnabled()
tgBot := s.tgbotService.NewTgbot() if (err == nil) && (isTgbotenabled) {
tgBot.Start(i18nFS) tgBot := s.tgbotService.NewTgbot()
tgBot.Start(i18nFS)
}
} }
return nil return nil
@ -440,13 +453,26 @@ func (s *Server) Start() (err error) {
// Stop gracefully shuts down the web server, stops Xray, cron jobs, and Telegram bot. // Stop gracefully shuts down the web server, stops Xray, cron jobs, and Telegram bot.
func (s *Server) Stop() error { func (s *Server) Stop() error {
return s.stop(true, true)
}
// StopPanelOnly stops only panel-owned HTTP/background resources for an in-process panel restart.
func (s *Server) StopPanelOnly() error {
return s.stop(false, false)
}
func (s *Server) stop(stopXray bool, stopTgBot bool) error {
s.cancel() s.cancel()
s.xrayService.StopXray() if stopXray {
s.xrayService.StopXray()
}
if s.cron != nil { if s.cron != nil {
s.cron.Stop() s.cron.Stop()
} }
service.StopTrafficWriter() if stopXray {
if s.tgbotService.IsRunning() { service.StopTrafficWriter()
}
if stopTgBot && s.tgbotService.IsRunning() {
s.tgbotService.Stop() s.tgbotService.Stop()
} }
// Gracefully stop WebSocket hub // Gracefully stop WebSocket hub