From 40ac6af15644acc2ab6d4e89ecd42ce57bbb23db Mon Sep 17 00:00:00 2001 From: Mateusz Charytoniuk Date: Tue, 30 Jul 2024 20:28:13 +0200 Subject: [PATCH] feat: health status of individual agents --- agent/RespondToHealth.go | 26 +++++++++++++++++++++ agent/StatusServer.go | 37 ++++++++++++++++++++++++++++++ agent/StatusServerConfiguration.go | 7 ++++++ cmd/Agent.go | 10 ++++++++ main.go | 30 ++++++++++++++++++++---- 5 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 agent/RespondToHealth.go create mode 100644 agent/StatusServer.go create mode 100644 agent/StatusServerConfiguration.go diff --git a/agent/RespondToHealth.go b/agent/RespondToHealth.go new file mode 100644 index 0000000..7493720 --- /dev/null +++ b/agent/RespondToHealth.go @@ -0,0 +1,26 @@ +package agent + +import ( + "net/http" + + "github.com/distantmagic/paddler/goroutine" +) + +type RespondToHealth struct { + ServerEventsChannel chan<- goroutine.ResultMessage +} + +func (self *RespondToHealth) ServeHTTP(response http.ResponseWriter, request *http.Request) { + response.Header().Set("Content-Type", "text/plain") + response.WriteHeader(http.StatusOK) + + _, err := response.Write([]byte("OK")) + + if err != nil { + self.ServerEventsChannel <- goroutine.ResultMessage{ + Error: err, + } + + return + } +} diff --git a/agent/StatusServer.go b/agent/StatusServer.go new file mode 100644 index 0000000..cd93a32 --- /dev/null +++ b/agent/StatusServer.go @@ -0,0 +1,37 @@ +package agent + +import ( + "net/http" + + "github.com/distantmagic/paddler/goroutine" + "github.com/hashicorp/go-hclog" +) + +type StatusServer struct { + StatusServerConfiguration *StatusServerConfiguration + Logger hclog.Logger + RespondToHealth http.Handler +} + +func (self *StatusServer) Serve(serverEventsChannel chan<- goroutine.ResultMessage) { + self.Logger.Debug( + "listen", + "host", self.StatusServerConfiguration.HttpAddress.GetHostWithPort(), + ) + + mux := http.NewServeMux() + + mux.Handle("/health", self.RespondToHealth) + + err := http.ListenAndServe( + self.StatusServerConfiguration.HttpAddress.GetHostWithPort(), + mux, + ) + + if err != nil { + serverEventsChannel <- goroutine.ResultMessage{ + Comment: "failed to listen", + Error: err, + } + } +} diff --git a/agent/StatusServerConfiguration.go b/agent/StatusServerConfiguration.go new file mode 100644 index 0000000..c983879 --- /dev/null +++ b/agent/StatusServerConfiguration.go @@ -0,0 +1,7 @@ +package agent + +import "github.com/distantmagic/paddler/netcfg" + +type StatusServerConfiguration struct { + HttpAddress *netcfg.HttpAddressConfiguration +} diff --git a/cmd/Agent.go b/cmd/Agent.go index 95d828f..be68f3c 100644 --- a/cmd/Agent.go +++ b/cmd/Agent.go @@ -17,6 +17,7 @@ type Agent struct { LocalLlamaCppConfiguration *llamacpp.LlamaCppConfiguration Logger hclog.Logger ManagementServerConfiguration *management.ManagementServerConfiguration + StatusServerConfiguration *agent.StatusServerConfiguration } func (self *Agent) Action(cliContext *cli.Context) error { @@ -38,7 +39,16 @@ func (self *Agent) Action(cliContext *cli.Context) error { }, } + statusServer := &agent.StatusServer{ + Logger: self.Logger.Named("StatusServer"), + RespondToHealth: &agent.RespondToHealth{ + ServerEventsChannel: serverEventsChannel, + }, + StatusServerConfiguration: self.StatusServerConfiguration, + } + go llamaCppObserver.ObserveAndReport(serverEventsChannel) + go statusServer.Serve(serverEventsChannel) for serverEvent := range serverEventsChannel { self.Logger.Info( diff --git a/main.go b/main.go index 591077a..248f6ae 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,9 @@ func main() { ManagementServerConfiguration: &management.ManagementServerConfiguration{ HttpAddress: &netcfg.HttpAddressConfiguration{}, }, + StatusServerConfiguration: &agent.StatusServerConfiguration{ + HttpAddress: &netcfg.HttpAddressConfiguration{}, + }, } balancer := &cmd.Balancer{ @@ -60,10 +63,6 @@ func main() { }, } - // buffer := &cmd.Buffer{ - // Logger: logger.Named("Buffer"), - // } - app := &cli.App{ Name: "paddler", Usage: "llama.cpp load balaner and reverse proxy server", @@ -100,6 +99,14 @@ func main() { agent.ManagementServerConfiguration.HttpAddress.Host = host + host, err = hostResolver.ResolveHost(backgroundContext, agent.StatusServerConfiguration.HttpAddress.Host) + + if err != nil { + return err + } + + agent.StatusServerConfiguration.HttpAddress.Host = host + return nil }, Flags: []cli.Flag{ @@ -153,6 +160,21 @@ func main() { Value: DefaultManagementScheme, Destination: &agent.ManagementServerConfiguration.HttpAddress.Scheme, }, + &cli.StringFlag{ + Name: "status-server-host", + Value: "127.0.0.1", + Destination: &agent.StatusServerConfiguration.HttpAddress.Host, + }, + &cli.UintFlag{ + Name: "status-server-port", + Value: 8087, + Destination: &agent.StatusServerConfiguration.HttpAddress.Port, + }, + &cli.StringFlag{ + Name: "status-server-scheme", + Value: "http", + Destination: &agent.StatusServerConfiguration.HttpAddress.Scheme, + }, }, }, {