daemon refactor
Some checks failed
builder / build (push) Failing after 25s

This commit is contained in:
abs3nt 2024-10-27 12:32:52 -07:00
parent d63d5ac778
commit 017c1de197
Signed by: abs3nt
GPG Key ID: A7BD96A8BAB04C09

View File

@ -1,6 +1,7 @@
package daemon
import (
"fmt"
"log"
"net"
"net/rpc"
@ -15,33 +16,40 @@ import (
func Run(c *commands.Commander, conf *config.Config, s fx.Shutdowner) {
for {
func() {
defer func() {
if r := recover(); r != nil {
log.Printf("Recovered in Run: %v", r)
err := startServer(c, conf)
if err != nil {
log.Printf("Server error: %v", err)
time.Sleep(time.Second)
continue
}
break
}
}
}()
func startServer(c *commands.Commander, conf *config.Config) error {
socketPath := conf.SocketPath
if _, err := os.Stat(socketPath); err == nil {
os.Remove(socketPath)
}
CommandHandler := Handler{
Commander: c,
if err := os.Remove(socketPath); err != nil {
return fmt.Errorf("failed to remove existing socket: %w", err)
}
}
commandHandler := &Handler{Commander: c}
server := rpc.NewServer()
server.Register(&CommandHandler)
if err := server.Register(commandHandler); err != nil {
return fmt.Errorf("failed to register RPC handler: %w", err)
}
listener, err := net.Listen("unix", socketPath)
if err != nil {
log.Println("Listen error:", err)
time.Sleep(time.Second)
return
return fmt.Errorf("listen error: %w", err)
}
defer listener.Close()
os.Chmod(socketPath, 0o666)
if err := os.Chmod(socketPath, 0o666); err != nil {
return fmt.Errorf("failed to set socket permissions: %w", err)
}
log.Println("Daemon is listening on", socketPath)
@ -52,16 +60,15 @@ func Run(c *commands.Commander, conf *config.Config, s fx.Shutdowner) {
continue
}
codec := NewLoggingServerCodec(conn)
go func() {
go handleConnection(server, codec)
}
}
func handleConnection(server *rpc.Server, codec rpc.ServerCodec) {
defer func() {
if r := recover(); r != nil {
log.Printf("Recovered in ServeCodec goroutine: %v", r)
log.Printf("Recovered in handleConnection: %v", r)
}
}()
server.ServeCodec(codec)
}()
}
}()
time.Sleep(time.Second)
}
}