From 80df03fe480bfe8aab4ce40e3e50951b1d95f7e6 Mon Sep 17 00:00:00 2001 From: Jacky Date: Mon, 5 May 2025 08:25:12 +0000 Subject: [PATCH] fix(upgrader): clean up old exe when restart --- internal/kernel/boot.go | 29 ++++++++++++++++++++++++++--- internal/upgrader/upgrade.go | 4 +++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/internal/kernel/boot.go b/internal/kernel/boot.go index 82fe7713..6f982db1 100644 --- a/internal/kernel/boot.go +++ b/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 diff --git a/internal/upgrader/upgrade.go b/internal/upgrader/upgrade.go index 80f3829f..da1eb553 100644 --- a/internal/upgrader/upgrade.go +++ b/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