This commit is contained in:
parent
d63d5ac778
commit
017c1de197
@ -1,6 +1,7 @@
|
|||||||
package daemon
|
package daemon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/rpc"
|
"net/rpc"
|
||||||
@ -15,33 +16,40 @@ import (
|
|||||||
|
|
||||||
func Run(c *commands.Commander, conf *config.Config, s fx.Shutdowner) {
|
func Run(c *commands.Commander, conf *config.Config, s fx.Shutdowner) {
|
||||||
for {
|
for {
|
||||||
func() {
|
err := startServer(c, conf)
|
||||||
defer func() {
|
if err != nil {
|
||||||
if r := recover(); r != nil {
|
log.Printf("Server error: %v", err)
|
||||||
log.Printf("Recovered in Run: %v", r)
|
time.Sleep(time.Second)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}()
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func startServer(c *commands.Commander, conf *config.Config) error {
|
||||||
socketPath := conf.SocketPath
|
socketPath := conf.SocketPath
|
||||||
|
|
||||||
if _, err := os.Stat(socketPath); err == nil {
|
if _, err := os.Stat(socketPath); err == nil {
|
||||||
os.Remove(socketPath)
|
if err := os.Remove(socketPath); err != nil {
|
||||||
}
|
return fmt.Errorf("failed to remove existing socket: %w", err)
|
||||||
|
}
|
||||||
CommandHandler := Handler{
|
|
||||||
Commander: c,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
commandHandler := &Handler{Commander: c}
|
||||||
server := rpc.NewServer()
|
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)
|
listener, err := net.Listen("unix", socketPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Listen error:", err)
|
return fmt.Errorf("listen error: %w", err)
|
||||||
time.Sleep(time.Second)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer listener.Close()
|
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)
|
log.Println("Daemon is listening on", socketPath)
|
||||||
|
|
||||||
@ -52,16 +60,15 @@ func Run(c *commands.Commander, conf *config.Config, s fx.Shutdowner) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
codec := NewLoggingServerCodec(conn)
|
codec := NewLoggingServerCodec(conn)
|
||||||
go func() {
|
go handleConnection(server, codec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleConnection(server *rpc.Server, codec rpc.ServerCodec) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
log.Printf("Recovered in ServeCodec goroutine: %v", r)
|
log.Printf("Recovered in handleConnection: %v", r)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
server.ServeCodec(codec)
|
server.ServeCodec(codec)
|
||||||
}()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user