Skip to content

Commit f308b7e

Browse files
committed
ensure to stop the backend before deleting it
1 parent a3ad1fa commit f308b7e

1 file changed

Lines changed: 36 additions & 1 deletion

File tree

updater/main.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,49 @@ func updateFromGit() {
8383
launchExecutable("../backend/cmd/cmd")
8484
}
8585

86+
// Check if a process is running by its name
87+
func isProcessRunning(processName string) (bool, error) {
88+
cmd := exec.Command("tasklist", "/FI", fmt.Sprintf("IMAGENAME eq %s", processName))
89+
output, err := cmd.Output()
90+
if err != nil {
91+
return false, err
92+
}
93+
return strings.Contains(string(output), processName), nil
94+
}
95+
96+
// Stop a process by its name
97+
func stopProcess(processName string) error {
98+
cmd := exec.Command("taskkill", "/IM", processName, "/F")
99+
cmd.Stdout = os.Stdout
100+
cmd.Stderr = os.Stderr
101+
return cmd.Run()
102+
}
103+
86104
func updateFromBinaries(osType string) {
87-
// Search for and delete old binaries in the same directory as updater.exe
105+
88106
binaries := []string{"backend-windows-64.exe", "backend-linux-64", "backend-macos-64", "backend-macos-m1-64"}
89107
for _, binary := range binaries {
90108
if _, err := os.Stat("./" + binary); err == nil {
109+
// Check if the backend process is running
110+
isRunning, err := isProcessRunning(binary)
111+
if err != nil {
112+
fmt.Fprintf(os.Stderr, "Error checking if process is running: %v\n", err)
113+
os.Exit(1)
114+
}
115+
116+
// Stop the process if it's running
117+
if isRunning {
118+
fmt.Printf("Process %s is running. Stopping it...\n", binary)
119+
if err := stopProcess(binary); err != nil {
120+
fmt.Fprintf(os.Stderr, "Error stopping process %s: %v\n", binary, err)
121+
os.Exit(1)
122+
}
123+
}
124+
91125
fmt.Printf("Deleting old binary: %s\n", binary)
92126
if err := os.Remove("./" + binary); err != nil {
93127
fmt.Fprintf(os.Stderr, "Error deleting old binary: %v\n", err)
128+
os.Exit(1)
94129
}
95130
}
96131
}

0 commit comments

Comments
 (0)