Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

refactor(daemon): separate Unix/Windows platform code with build tags

yushen4372c5f4fab0
diff --git a/server/cmd/multica/cmd_daemon.go b/server/cmd/multica/cmd_daemon.go
index 089ea1a7b0..1eae904d94 100644
--- a/server/cmd/multica/cmd_daemon.go
+++ b/server/cmd/multica/cmd_daemon.go
@@ -8,10 +8,8 @@ import (
"net/http"
"os"
"os/exec"
- "os/signal"
"strconv"
"strings"
- "syscall"
"time"
"github.com/spf13/cobra"
@@ -156,7 +154,7 @@ func runDaemonBackground(cmd *cobra.Command) error {
child := exec.Command(exePath, args...)
child.Stdout = logFile
child.Stderr = logFile
- child.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
+ child.SysProcAttr = daemonSysProcAttr()
if err := child.Start(); err != nil {
logFile.Close()
@@ -274,7 +272,7 @@ func runDaemonForeground(cmd *cobra.Command) error {
}
cfg.CLIVersion = version
- ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
+ ctx, stop := notifyShutdownContext(context.Background())
defer stop()
logger := logger_pkg.NewLogger("daemon")
@@ -306,7 +304,7 @@ func runDaemonForeground(cmd *cobra.Command) error {
}
child.Stdout = logFile
child.Stderr = logFile
- child.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
+ child.SysProcAttr = daemonSysProcAttr()
if err := child.Start(); err != nil {
logFile.Close()
@@ -355,7 +353,7 @@ func runDaemonStop(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("find process %d: %w", int(pid), err)
}
- if err := process.Signal(syscall.SIGTERM); err != nil {
+ if err := stopDaemonProcess(process); err != nil {
return fmt.Errorf("stop daemon (pid %d): %w", int(pid), err)
}
@@ -430,16 +428,7 @@ func runDaemonLogs(cmd *cobra.Command, _ []string) error {
follow, _ := cmd.Flags().GetBool("follow")
lines, _ := cmd.Flags().GetInt("lines")
- args := []string{"-n", strconv.Itoa(lines)}
- if follow {
- args = append(args, "-f")
- }
- args = append(args, logPath)
-
- tail := exec.Command("tail", args...)
- tail.Stdout = os.Stdout
- tail.Stderr = os.Stderr
- return tail.Run()
+ return tailLogFile(logPath, lines, follow)
}
// checkDaemonHealthOnPort calls the daemon's local health endpoint on the given port.
diff --git a/server/cmd/multica/cmd_daemon_unix.go b/server/cmd/multica/cmd_daemon_unix.go
new file mode 100644
index 0000000000..320e1047a3
--- /dev/null
+++ b/server/cmd/multica/cmd_daemon_unix.go
@@ -0,0 +1,37 @@
+//go:build !windows
+
+package main
+
+import (
+ "context"
+ "os"
+ "os/exec"
+ "os/signal"
+ "strconv"
+ "syscall"
+)
+
+func daemonSysProcAttr() *syscall.SysProcAttr {
+ return &syscall.SysProcAttr{Setsid: true}
+}
+
+func stopDaemonProcess(process *os.Process) error {
+ return process.Signal(syscall.SIGTERM)
+}
+
+func notifyShutdownContext(parent context.Context) (context.Context, context.CancelFunc) {
+ return signal.NotifyContext(parent, syscall.SIGINT, syscall.SIGTERM)
+}
+
+func tailLogFile(logPath string, lines int, follow bool) error {
+ args := []string{"-n", strconv.Itoa(lines)}
+ if follow {
+ args = append(args, "-f")
+ }
+ args = append(args, logPath)
+
+ tail := exec.Command("tail", args...)
+ tail.Stdout = os.Stdout
+ tail.Stderr = os.Stderr
+ return tail.Run()
+}
diff --git a/server/cmd/multica/cmd_daemon_windows.go b/server/cmd/multica/cmd_daemon_windows.go
new file mode 100644
index 0000000000..0d0ee935d6
--- /dev/null
+++ b/server/cmd/multica/cmd_daemon_windows.go
@@ -0,0 +1,72 @@
+//go:build windows
+
+package main
+
+import (
+ "context"
+ "fmt"
+ "io"
+ "os"
+ "os/signal"
+ "strings"
+ "syscall"
+ "time"
+)
+
+func daemonSysProcAttr() *syscall.SysProcAttr {
+ return &syscall.SysProcAttr{CreationFlags: 0x00000200} // CREATE_NEW_PROCESS_GROUP
+}
+
+func stopDaemonProcess(process *os.Process) error {
+ return process.Kill()
+}
+
+func notifyShutdownContext(parent context.Context) (context.Context, context.CancelFunc) {
+ return signal.NotifyContext(parent, os.Interrupt)
+}
+
+func tailLogFile(logPath string, lines int, follow bool) error {
+ content, err := os.ReadFile(logPath)
+ if err != nil {
+ return err
+ }
+
+ allLines := strings.Split(string(content), "\n")
+ if len(allLines) > 0 && allLines[len(allLines)-1] == "" {
+ allLines = allLines[:len(allLines)-1]
+ }
+
+ start := 0
+ if len(allLines) > lines {
+ start = len(allLines) - lines
+ }
+ for i := start; i < len(allLines); i++ {
+ fmt.Println(allLines[i])
+ }
+
+ if !follow {
+ return nil
+ }
+
+ f, err := os.Open(logPath)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+
+ if _, err := f.Seek(int64(len(content)), io.SeekStart); err != nil {
+ return err
+ }
+
+ buf := make([]byte, 4096)
+ for {
+ time.Sleep(500 * time.Millisecond)
+ n, readErr := f.Read(buf)
+ if n > 0 {
+ os.Stdout.Write(buf[:n])
+ }
+ if readErr != nil && readErr != io.EOF {
+ return readErr
+ }
+ }
+}