Skip to content

Commit

Permalink
修复自动宕机启动多个进程问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hu, jinbo committed Nov 24, 2023
1 parent abfe381 commit 0553698
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
4 changes: 2 additions & 2 deletions api/gameLevel2Api.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func (g *GameLevel2Api) Start(ctx *gin.Context) {
bin := cluster.Bin
beta := cluster.Beta
clusterName := cluster.ClusterName
gameService.StopLevel(clusterName, levelName)
gameService.LaunchLevel(clusterName, levelName, bin, beta)

gameService.StartLevel(clusterName, levelName, bin, beta)

ctx.JSON(http.StatusOK, vo.Response{
Code: 200,
Expand Down
8 changes: 3 additions & 5 deletions autoCheck/autoCheckManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ func (s *LevelModCheck) Check(clusterName, levelName string) bool {
func (s *LevelModCheck) Run(clusterName, levelName string) error {
log.Println("正在更新模组 ", clusterName, levelName)
SendAnnouncement2(clusterName, levelName)
gameService.StopLevel(clusterName, levelName)

cluster := clusterUtils.GetCluster(clusterName)
bin := cluster.Bin
beta := cluster.Beta
gameService.LaunchLevel(clusterName, levelName, bin, beta)
gameService.StartLevel(clusterName, levelName, bin, beta)
return nil
}

Expand All @@ -297,13 +297,11 @@ func (s *LevelDownCheck) Check(clusterName, levelName string) bool {

func (s *LevelDownCheck) Run(clusterName, levelName string) error {
log.Println("正在启动世界 ", clusterName, levelName)
// TODO 加锁
if !gameService.GetLevelStatus(clusterName, levelName) {
gameService.StopLevel(clusterName, levelName)
cluster := clusterUtils.GetCluster(clusterName)
bin := cluster.Bin
beta := cluster.Beta
gameService.LaunchLevel(clusterName, levelName, bin, beta)
gameService.StartLevel(clusterName, levelName, bin, beta)
}
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions schedule/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type StartStrategy struct{}
func (s *StartStrategy) Execute(clusterName string, levelName string) {
log.Println("正在定时启动游戏 clusterName: ", clusterName)
cluster := clusterUtils.GetCluster(clusterName)
gameService.LaunchLevel(clusterName, levelName, cluster.Bin, cluster.Beta)
gameService.StartLevel(clusterName, levelName, cluster.Bin, cluster.Beta)
}

type StopStrategy struct{}
Expand All @@ -64,9 +64,8 @@ type RestartStrategy struct{}

func (s *RestartStrategy) Execute(clusterName string, levelName string) {
log.Println("正在定时重启游戏 clusterName: ", clusterName)
gameService.StopLevel(clusterName, levelName)
cluster := clusterUtils.GetCluster(clusterName)
gameService.LaunchLevel(clusterName, levelName, cluster.Bin, cluster.Beta)
gameService.StartLevel(clusterName, levelName, cluster.Bin, cluster.Beta)
}

type RegenerateStrategy struct{}
Expand Down
23 changes: 22 additions & 1 deletion service/gameService.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"time"
)

var launchLock = sync.Mutex{}

type GameService struct {
lock sync.Mutex
c HomeService
Expand Down Expand Up @@ -139,7 +141,19 @@ func (g *GameService) killLevel(clusterName, level string) {
}
}

func (g *GameService) StartLevel(clusterName, level string, bin, beta int) {
g.StopLevel(clusterName, level)
g.LaunchLevel(clusterName, level, bin, beta)
ClearScreen()
}

func (g *GameService) LaunchLevel(clusterName, level string, bin, beta int) {
launchLock.Lock()
defer func() {
launchLock.Unlock()
if r := recover(); r != nil {
}
}()

cluster := clusterUtils.GetCluster(clusterName)
dstInstallDir := cluster.ForceInstallDir
Expand All @@ -161,8 +175,15 @@ func (g *GameService) LaunchLevel(clusterName, level string, bin, beta int) {
}

func (g *GameService) StopLevel(clusterName, level string) {
g.shutdownLevel(clusterName, level)

launchLock.Lock()
defer func() {
launchLock.Unlock()
if r := recover(); r != nil {
}
}()

g.shutdownLevel(clusterName, level)
time.Sleep(3 * time.Second)

if g.GetLevelStatus(clusterName, level) {
Expand Down

0 comments on commit 0553698

Please sign in to comment.