Quellcode durchsuchen

fix(upgrader): clean up old exe when restart

Jacky vor 20 Stunden
Ursprung
Commit
80df03fe48
2 geänderte Dateien mit 29 neuen und 4 gelöschten Zeilen
  1. 26 3
      internal/kernel/boot.go
  2. 3 1
      internal/upgrader/upgrade.go

+ 26 - 3
internal/kernel/boot.go

@@ -5,7 +5,9 @@ import (
 	"crypto/rand"
 	"encoding/hex"
 	"mime"
+	"os"
 	"path"
+	"path/filepath"
 	"runtime"
 	"sync"
 
@@ -45,7 +47,7 @@ func Boot(ctx context.Context) {
 		func() {
 			cache.Init(ctx)
 		},
-		CheckAndCleanupOTAContainers,
+		CheckAndCleanupOTA,
 	}
 
 	syncs := []func(ctx context.Context){
@@ -160,9 +162,30 @@ func InitJsExtensionType() {
 	_ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8")
 }
 
-// CheckAndCleanupOTAContainers Check and cleanup OTA update temporary containers
-func CheckAndCleanupOTAContainers() {
+// CheckAndCleanupOTA Check and cleanup OTA update temporary containers
+func CheckAndCleanupOTA() {
 	if !helper.InNginxUIOfficialDocker() {
+		// If running on Windows, clean up .nginx-ui.old.* files
+		if runtime.GOOS == "windows" {
+			execPath, err := os.Executable()
+			if err != nil {
+				logger.Error("Failed to get executable path:", err)
+				return
+			}
+
+			execDir := filepath.Dir(execPath)
+			logger.Info("Cleaning up .nginx-ui.old.* files on Windows in:", execDir)
+
+			pattern := filepath.Join(execDir, ".nginx-ui.old.*")
+			files, err := filepath.Glob(pattern)
+			if err != nil {
+				logger.Error("Failed to list .nginx-ui.old.* files:", err)
+			} else {
+				for _, file := range files {
+					_ = os.Remove(file)
+				}
+			}
+		}
 		return
 	}
 	// Execute the third step cleanup operation at startup

+ 3 - 1
internal/upgrader/upgrade.go

@@ -217,7 +217,9 @@ func (u *Upgrader) PerformCoreUpgrade(tarPath string) (err error) {
 	}
 	defer updateInProgress.Store(false)
 
-	opts := selfupdate.Options{}
+	opts := selfupdate.Options{
+		OldSavePath: fmt.Sprintf(".nginx-ui.old.%d", time.Now().Unix()),
+	}
 
 	if err = opts.CheckPermissions(); err != nil {
 		return err