Skip to content

Commit

Permalink
feat: health status of individual agents
Browse files Browse the repository at this point in the history
  • Loading branch information
mcharytoniuk committed Jul 30, 2024
1 parent 3e7a940 commit 40ac6af
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
26 changes: 26 additions & 0 deletions agent/RespondToHealth.go
Original file line number Diff line number Diff line change
@@ -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
}
}
37 changes: 37 additions & 0 deletions agent/StatusServer.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
}
7 changes: 7 additions & 0 deletions agent/StatusServerConfiguration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package agent

import "github.com/distantmagic/paddler/netcfg"

type StatusServerConfiguration struct {
HttpAddress *netcfg.HttpAddressConfiguration
}
10 changes: 10 additions & 0 deletions cmd/Agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down
30 changes: 26 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func main() {
ManagementServerConfiguration: &management.ManagementServerConfiguration{
HttpAddress: &netcfg.HttpAddressConfiguration{},
},
StatusServerConfiguration: &agent.StatusServerConfiguration{
HttpAddress: &netcfg.HttpAddressConfiguration{},
},
}

balancer := &cmd.Balancer{
Expand All @@ -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",
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
},
},
},
{
Expand Down

0 comments on commit 40ac6af

Please sign in to comment.