|
| 1 | +package node |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "runtime" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + cronMarker = "# push-validator-peer-refresh" |
| 14 | + cronInterval = "* * * * *" // Every 1 minute |
| 15 | + launchdPlistName = "com.push.validator.peerrefresh.plist" |
| 16 | +) |
| 17 | + |
| 18 | +// InstallPeerRefreshCron installs a cron job (or launchd on macOS) that |
| 19 | +// runs every minute to refresh peers and restart the node if needed. |
| 20 | +// This is idempotent - safe to call multiple times. |
| 21 | +func InstallPeerRefreshCron(homeDir string) error { |
| 22 | + // Find the push-validator binary path |
| 23 | + binPath, err := os.Executable() |
| 24 | + if err != nil { |
| 25 | + binPath = "push-validator" // fallback |
| 26 | + } |
| 27 | + |
| 28 | + if runtime.GOOS == "darwin" { |
| 29 | + return installLaunchd(binPath, homeDir) |
| 30 | + } |
| 31 | + return installCron(binPath, homeDir) |
| 32 | +} |
| 33 | + |
| 34 | +// UninstallPeerRefreshCron removes the peer refresh cron job. |
| 35 | +func UninstallPeerRefreshCron(homeDir string) error { |
| 36 | + if runtime.GOOS == "darwin" { |
| 37 | + return uninstallLaunchd() |
| 38 | + } |
| 39 | + return uninstallCron() |
| 40 | +} |
| 41 | + |
| 42 | +// IsPeerRefreshCronInstalled checks if the cron job is installed. |
| 43 | +func IsPeerRefreshCronInstalled(homeDir string) bool { |
| 44 | + if runtime.GOOS == "darwin" { |
| 45 | + return isLaunchdInstalled() |
| 46 | + } |
| 47 | + return isCronInstalled() |
| 48 | +} |
| 49 | + |
| 50 | +// --- Cron (Linux) --- |
| 51 | + |
| 52 | +func installCron(binPath, homeDir string) error { |
| 53 | + // Check if already installed |
| 54 | + if isCronInstalled() { |
| 55 | + return nil |
| 56 | + } |
| 57 | + |
| 58 | + // Get current crontab |
| 59 | + cmd := exec.Command("crontab", "-l") |
| 60 | + existing, _ := cmd.Output() // ignore error (no crontab yet) |
| 61 | + |
| 62 | + // Build new cron entry |
| 63 | + cronLine := fmt.Sprintf("%s %s _internal-refresh-peers --home %s %s", |
| 64 | + cronInterval, binPath, homeDir, cronMarker) |
| 65 | + |
| 66 | + // Append to existing crontab |
| 67 | + newCrontab := string(existing) |
| 68 | + if !strings.HasSuffix(newCrontab, "\n") && len(newCrontab) > 0 { |
| 69 | + newCrontab += "\n" |
| 70 | + } |
| 71 | + newCrontab += cronLine + "\n" |
| 72 | + |
| 73 | + // Install new crontab |
| 74 | + cmd = exec.Command("crontab", "-") |
| 75 | + cmd.Stdin = strings.NewReader(newCrontab) |
| 76 | + return cmd.Run() |
| 77 | +} |
| 78 | + |
| 79 | +func uninstallCron() error { |
| 80 | + // Get current crontab |
| 81 | + cmd := exec.Command("crontab", "-l") |
| 82 | + existing, err := cmd.Output() |
| 83 | + if err != nil { |
| 84 | + return nil // no crontab |
| 85 | + } |
| 86 | + |
| 87 | + // Filter out our marker line |
| 88 | + lines := strings.Split(string(existing), "\n") |
| 89 | + var filtered []string |
| 90 | + for _, line := range lines { |
| 91 | + if !strings.Contains(line, cronMarker) { |
| 92 | + filtered = append(filtered, line) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + newCrontab := strings.Join(filtered, "\n") |
| 97 | + |
| 98 | + // Install filtered crontab |
| 99 | + cmd = exec.Command("crontab", "-") |
| 100 | + cmd.Stdin = strings.NewReader(newCrontab) |
| 101 | + return cmd.Run() |
| 102 | +} |
| 103 | + |
| 104 | +func isCronInstalled() bool { |
| 105 | + cmd := exec.Command("crontab", "-l") |
| 106 | + output, err := cmd.Output() |
| 107 | + if err != nil { |
| 108 | + return false |
| 109 | + } |
| 110 | + return strings.Contains(string(output), cronMarker) |
| 111 | +} |
| 112 | + |
| 113 | +// --- Launchd (macOS) --- |
| 114 | + |
| 115 | +func launchdPlistPath() string { |
| 116 | + home, _ := os.UserHomeDir() |
| 117 | + return filepath.Join(home, "Library", "LaunchAgents", launchdPlistName) |
| 118 | +} |
| 119 | + |
| 120 | +func installLaunchd(binPath, homeDir string) error { |
| 121 | + // Check if already installed |
| 122 | + if isLaunchdInstalled() { |
| 123 | + return nil |
| 124 | + } |
| 125 | + |
| 126 | + plistPath := launchdPlistPath() |
| 127 | + |
| 128 | + // Ensure LaunchAgents directory exists |
| 129 | + if err := os.MkdirAll(filepath.Dir(plistPath), 0o755); err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + // Create plist content - runs every 60 seconds |
| 134 | + plist := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?> |
| 135 | +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 136 | +<plist version="1.0"> |
| 137 | +<dict> |
| 138 | + <key>Label</key> |
| 139 | + <string>com.push.validator.peerrefresh</string> |
| 140 | + <key>ProgramArguments</key> |
| 141 | + <array> |
| 142 | + <string>%s</string> |
| 143 | + <string>_internal-refresh-peers</string> |
| 144 | + <string>--home</string> |
| 145 | + <string>%s</string> |
| 146 | + </array> |
| 147 | + <key>StartInterval</key> |
| 148 | + <integer>60</integer> |
| 149 | + <key>RunAtLoad</key> |
| 150 | + <true/> |
| 151 | + <key>StandardOutPath</key> |
| 152 | + <string>%s/logs/peer-refresh.log</string> |
| 153 | + <key>StandardErrorPath</key> |
| 154 | + <string>%s/logs/peer-refresh.log</string> |
| 155 | +</dict> |
| 156 | +</plist> |
| 157 | +`, binPath, homeDir, homeDir, homeDir) |
| 158 | + |
| 159 | + // Write plist file |
| 160 | + if err := os.WriteFile(plistPath, []byte(plist), 0o644); err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + |
| 164 | + // Load the agent |
| 165 | + cmd := exec.Command("launchctl", "load", plistPath) |
| 166 | + return cmd.Run() |
| 167 | +} |
| 168 | + |
| 169 | +func uninstallLaunchd() error { |
| 170 | + plistPath := launchdPlistPath() |
| 171 | + |
| 172 | + // Unload the agent (ignore error if not loaded) |
| 173 | + cmd := exec.Command("launchctl", "unload", plistPath) |
| 174 | + _ = cmd.Run() |
| 175 | + |
| 176 | + // Remove the plist file |
| 177 | + return os.Remove(plistPath) |
| 178 | +} |
| 179 | + |
| 180 | +func isLaunchdInstalled() bool { |
| 181 | + _, err := os.Stat(launchdPlistPath()) |
| 182 | + return err == nil |
| 183 | +} |
0 commit comments