Skip to content

Commit 8f3a0b5

Browse files
andystimeclaude
andcommitted
fix: stop service and cleanup dirs before CDN script to handle old script versions
The CDN uninstall.sh is deployed separately and old versions still kill all ggo processes. Move stopAgentService and cleanupDataDirs before the CDN script so all critical work completes even if the process is killed. Flow: unregister → stop service → cleanup dirs → CDN script Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 91d0105 commit 8f3a0b5

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

cmd/ggo/system/commands.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,14 @@ func NewUninstallCmd() *cobra.Command {
112112
// Step 1: Unregister agent from server (must happen before config is removed)
113113
tryUnregisterAgent()
114114

115-
// Step 2: Run CDN uninstall script (stops service, removes service files and binary)
115+
// Step 2: Stop agent service so it doesn't recreate files during shutdown
116+
stopAgentService()
117+
118+
// Step 3: Clean up data directories (must complete before CDN script,
119+
// because the old CDN script may kill this process)
120+
cleanupDataDirs()
121+
122+
// Step 4: Run CDN uninstall script (removes service files and binary)
116123
command, cmdArgs, err := buildScriptCommand(scriptActionUninstall, runtime.GOOS)
117124
if err != nil {
118125
return err
@@ -125,9 +132,6 @@ func NewUninstallCmd() *cobra.Command {
125132
klog.Warningf("Uninstall script failed: %v", err)
126133
}
127134

128-
// Step 3: Clean up data directories (after service is stopped by the script)
129-
cleanupDataDirs()
130-
131135
return nil
132136
},
133137
}
@@ -193,6 +197,32 @@ func rootHomeDir() string {
193197
return "/root"
194198
}
195199

200+
// stopAgentService stops the ggo-agent system service so it doesn't recreate
201+
// files (e.g. workers.json) during shutdown while we're cleaning up directories.
202+
func stopAgentService() {
203+
switch {
204+
case platform.IsLinux():
205+
cmd := exec.Command("sudo", "-n", "systemctl", "stop", "ggo-agent")
206+
if os.Getuid() == 0 {
207+
cmd = exec.Command("systemctl", "stop", "ggo-agent")
208+
}
209+
if err := cmd.Run(); err != nil {
210+
klog.V(4).Infof("Could not stop ggo-agent service (may not exist): %v", err)
211+
}
212+
case platform.IsDarwin():
213+
for _, plist := range []string{
214+
filepath.Join(os.Getenv("HOME"), "Library", "LaunchAgents", "com.gpugo.agent.plist"),
215+
"/Library/LaunchDaemons/com.gpugo.agent.plist",
216+
} {
217+
cmd := exec.Command("sudo", "-n", "launchctl", "unload", plist)
218+
if os.Getuid() == 0 {
219+
cmd = exec.Command("launchctl", "unload", plist)
220+
}
221+
_ = cmd.Run()
222+
}
223+
}
224+
}
225+
196226
// tryUnregisterAgent attempts to unregister the agent from the server before cleanup.
197227
// It checks the current user's config first, then root's config on Unix systems
198228
// (since the agent typically runs as root via systemd/launchd).

0 commit comments

Comments
 (0)