fix(upgrader): clean up old exe when restart

This commit is contained in:
Jacky 2025-05-05 08:25:12 +00:00
parent 17c3d21f6e
commit 80df03fe48
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
2 changed files with 29 additions and 4 deletions

View file

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

View file

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