diff --git a/handler/restart/restart.go b/handler/restart/restart.go index c3a84bd..be5e352 100644 --- a/handler/restart/restart.go +++ b/handler/restart/restart.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "os/exec" + "sync" "time" "git.lxtend.com/qqbot/config" @@ -19,6 +20,9 @@ import ( var hasVaildBuild = true +// 避免被并发请求构建或重启 +var stuffMutex sync.Mutex + func init() { handler.RegisterHandler("/重启bot", restart, constants.LEVEL_ADMIN) handler.RegisterHelpInform("/重启bot", "热更新", "重启bot") @@ -169,6 +173,8 @@ func buildBot() error { } func PullCodeHandler(c *gin.Context) { + stuffMutex.Lock() + defer stuffMutex.Unlock() err := util.GitPull() log.Println("拉取代码...") if err != nil { @@ -180,6 +186,8 @@ func PullCodeHandler(c *gin.Context) { } func BuildBotHandler(c *gin.Context) { + stuffMutex.Lock() + defer stuffMutex.Unlock() err := buildBot() log.Println("构建程序...") if err != nil { @@ -196,6 +204,8 @@ func RestartBotHandler(c *gin.Context) { c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"}) return } + stuffMutex.Lock() + defer stuffMutex.Unlock() err := restartProgram() log.Println("重启程序...") if err != nil {