fix: 添加互斥锁以避免并发请求导致的构建或重启冲突

This commit is contained in:
lixiangwuxian 2025-05-03 23:37:28 +08:00
parent 40a336af36
commit d4038a568e

View File

@ -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 {