mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Make PureRunner an Agent so that it encapsulates its grpc server (#834)
* Refactor PureRunner as an Agent so that it encapsulates its grpc server * Maintain a list of extra contexts for the server to select on to handle errors and cancellations
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
@@ -107,6 +108,8 @@ type Server struct {
|
||||
rootMiddlewares []fnext.Middleware
|
||||
apiMiddlewares []fnext.Middleware
|
||||
promExporter *prometheus.Exporter
|
||||
// Extensions can append to this list of contexts so that cancellations are properly handled.
|
||||
extraCtxs []context.Context
|
||||
}
|
||||
|
||||
func nodeTypeFromString(value string) ServerNodeType {
|
||||
@@ -392,7 +395,15 @@ func WithAgentFromEnv() ServerOption {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.agent = agent.NewSyncOnly(agent.NewCachedDataAccess(ds))
|
||||
grpcAddr := fmt.Sprintf(":%d", s.grpcListenPort)
|
||||
delegatedAgent := agent.NewSyncOnly(agent.NewCachedDataAccess(ds))
|
||||
cancelCtx, cancel := context.WithCancel(ctx)
|
||||
prAgent, err := agent.NewPureRunner(cancel, grpcAddr, delegatedAgent, s.cert, s.certKey, s.certAuthority)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.agent = prAgent
|
||||
s.extraCtxs = append(s.extraCtxs, cancelCtx)
|
||||
case ServerTypeLB:
|
||||
s.nodeType = ServerTypeLB
|
||||
runnerURL := getEnv(EnvRunnerURL, "")
|
||||
@@ -451,6 +462,14 @@ func WithAgentFromEnv() ServerOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithExtraCtx appends a context to the list of contexts the server will watch for cancellations / errors / signals.
|
||||
func WithExtraCtx(extraCtx context.Context) ServerOption {
|
||||
return func(ctx context.Context, s *Server) error {
|
||||
s.extraCtxs = append(s.extraCtxs, extraCtx)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// New creates a new Functions server with the opts given. For convenience, users may
|
||||
// prefer to use NewFromEnv but New is more flexible if needed.
|
||||
func New(ctx context.Context, opts ...ServerOption) *Server {
|
||||
@@ -600,24 +619,6 @@ func (s *Server) startGears(ctx context.Context, cancel context.CancelFunc) {
|
||||
|
||||
installChildReaper()
|
||||
|
||||
if s.nodeType == ServerTypePureRunner {
|
||||
// Run grpc too
|
||||
grpcAddr := fmt.Sprintf(":%d", s.grpcListenPort)
|
||||
pr, err := agent.CreatePureRunner(grpcAddr, s.agent, s.cert, s.certKey, s.certAuthority)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("Pure runner server creation error")
|
||||
cancel()
|
||||
} else {
|
||||
go func() {
|
||||
err := pr.Start()
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("fail to start pure runner")
|
||||
cancel()
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
server := http.Server{
|
||||
Addr: listen,
|
||||
Handler: &ochttp.Handler{Handler: s.Router},
|
||||
@@ -635,8 +636,23 @@ func (s *Server) startGears(ctx context.Context, cancel context.CancelFunc) {
|
||||
}
|
||||
}()
|
||||
|
||||
// listening for signals or listener errors...
|
||||
<-ctx.Done()
|
||||
// listening for signals or listener errors or cancellations on all registered contexts.
|
||||
s.extraCtxs = append(s.extraCtxs, ctx)
|
||||
cases := make([]reflect.SelectCase, len(s.extraCtxs))
|
||||
for i, ctx := range s.extraCtxs {
|
||||
cases[i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(ctx.Done())}
|
||||
}
|
||||
nth, recv, wasSend := reflect.Select(cases)
|
||||
if wasSend {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"ctxNumber": nth,
|
||||
"receivedValue": recv.String(),
|
||||
}).Debug("Stopping because of received value from done context.")
|
||||
} else {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"ctxNumber": nth,
|
||||
}).Debug("Stopping because of closed channel from done context.")
|
||||
}
|
||||
|
||||
// TODO: do not wait forever during graceful shutdown (add graceful shutdown timeout)
|
||||
if err := server.Shutdown(context.Background()); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user