Skip to content

Commit

Permalink
add health check
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Sep 11, 2024
1 parent 029bc6d commit 502d0ef
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
9 changes: 9 additions & 0 deletions backend/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func Start(ctx context.Context, config Config) error {
return rpc.Serve(ctx, config.Bind,
rpc.GRPC(ftlv1connect.NewVerbServiceHandler, svc),
rpc.HTTP("/", svc),
rpc.HealthCheck(svc.healthCheck),
)
}

Expand Down Expand Up @@ -484,3 +485,11 @@ func (s *Service) getDeploymentLogger(ctx context.Context, deploymentKey model.D
sink := newDeploymentLogsSink(s.deploymentLogQueue)
return log.FromContext(ctx).AddSink(sink).Attrs(attrs)
}

func (s *Service) healthCheck(writer http.ResponseWriter, request *http.Request) {
if s.state.Load() == ftlv1.RunnerState_RUNNER_ASSIGNED {
writer.WriteHeader(http.StatusOK)
return
}
writer.WriteHeader(http.StatusServiceUnavailable)
}
16 changes: 12 additions & 4 deletions internal/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const ShutdownGracePeriod = time.Second * 5
type serverOptions struct {
mux *http.ServeMux
reflectionPaths []string
healthCheck http.HandlerFunc
}

type Option func(*serverOptions)
Expand All @@ -49,6 +50,12 @@ func PProf() Option {
}
}

func HealthCheck(check http.HandlerFunc) Option {
return func(options *serverOptions) {
options.healthCheck = check
}
}

// RawGRPC is a convenience function for registering a GRPC server with default options without Pingable.
func RawGRPC[Iface, Impl any](constructor RawGRPCServerConstructor[Iface], impl Impl, options ...connect.HandlerOption) Option {
return func(o *serverOptions) {
Expand All @@ -75,16 +82,17 @@ type Server struct {
func NewServer(ctx context.Context, listen *url.URL, options ...Option) (*Server, error) {
opts := &serverOptions{
mux: http.NewServeMux(),
healthCheck: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}),
}

opts.mux.Handle("/healthz", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))

for _, option := range options {
option(opts)
}

opts.mux.Handle("/healthz", opts.healthCheck)

// Register reflection services.
reflector := grpcreflect.NewStaticReflector(opts.reflectionPaths...)
opts.mux.Handle(grpcreflect.NewHandlerV1(reflector))
Expand Down

0 comments on commit 502d0ef

Please sign in to comment.