From d4038a568eec0e2c6d170638fb37da09826b9b4b Mon Sep 17 00:00:00 2001 From: lixiangwuxian Date: Sat, 3 May 2025 23:37:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=E4=BA=92=E6=96=A5?= =?UTF-8?q?=E9=94=81=E4=BB=A5=E9=81=BF=E5=85=8D=E5=B9=B6=E5=8F=91=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=AF=BC=E8=87=B4=E7=9A=84=E6=9E=84=E5=BB=BA=E6=88=96?= =?UTF-8?q?=E9=87=8D=E5=90=AF=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/restart/restart.go | 10 ++++++++++ 1 file changed, 10 insertions(+) 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 {