Skip to content

Commit

Permalink
move restart cron to web.go
Browse files Browse the repository at this point in the history
Co-Authored-By: Alireza Ahmadi <alireza7@gmail.com>
  • Loading branch information
MHSanaei and alireza0 committed Sep 6, 2023
1 parent 4e6484c commit be4a68b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
16 changes: 0 additions & 16 deletions web/controller/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"strconv"
"x-ui/database/model"
"x-ui/logger"
"x-ui/web/global"
"x-ui/web/service"
"x-ui/web/session"

Expand All @@ -20,7 +18,6 @@ type InboundController struct {
func NewInboundController(g *gin.RouterGroup) *InboundController {
a := &InboundController{}
a.initRouter(g)
a.startTask()
return a
}

Expand All @@ -42,19 +39,6 @@ func (a *InboundController) initRouter(g *gin.RouterGroup) {
g.POST("/delDepletedClients/:id", a.delDepletedClients)
}

func (a *InboundController) startTask() {
webServer := global.GetWebServer()
c := webServer.GetCron()
c.AddFunc("@every 10s", func() {
if a.xrayService.IsNeedRestartAndSetFalse() {
err := a.xrayService.RestartXray(false)
if err != nil {
logger.Error("restart xray failed:", err)
}
}
})
}

func (a *InboundController) getInbounds(c *gin.Context) {
user := session.GetLoginUser(c)
inbounds, err := a.inboundService.GetInbounds(user.Id)
Expand Down
10 changes: 10 additions & 0 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ func (s *Server) startTask() {
// Check whether xray is running every 30 seconds
s.cron.AddJob("@every 30s", job.NewCheckXrayRunningJob())

// Check if xray needs to be restarted
s.cron.AddFunc("@every 10s", func() {
if s.xrayService.IsNeedRestartAndSetFalse() {
err := s.xrayService.RestartXray(false)
if err != nil {
logger.Error("restart xray failed:", err)
}
}
})

go func() {
time.Sleep(time.Second * 5)
// Statistics every 10 seconds, start the delay for 5 seconds for the first time, and staggered with the time to restart xray
Expand Down

0 comments on commit be4a68b

Please sign in to comment.