Skip to content

Commit

Permalink
gracefull shutdown RPC even when build is in error
Browse files Browse the repository at this point in the history
  • Loading branch information
tejal29 committed Jun 24, 2020
1 parent 46badeb commit 5467e82
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
16 changes: 6 additions & 10 deletions cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ import (
)

var (
opts config.SkaffoldOptions
v string
forceColors bool
defaultColor int
overwrite bool
opts config.SkaffoldOptions
v string
forceColors bool
defaultColor int
overwrite bool
shutdownAPIServer func() error
)

func NewSkaffoldCommand(out, err io.Writer) *cobra.Command {
updateMsg := make(chan string)
surveyPrompt := make(chan bool)
var shutdownAPIServer func() error

rootCmd := &cobra.Command{
Use: "skaffold",
Expand Down Expand Up @@ -115,10 +115,6 @@ func NewSkaffoldCommand(out, err io.Writer) *cobra.Command {
}
default:
}

if shutdownAPIServer != nil {
shutdownAPIServer()
}
},
}

Expand Down
14 changes: 12 additions & 2 deletions cmd/skaffold/app/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,25 @@ func (b *builder) Hidden() Builder {
func (b *builder) ExactArgs(argCount int, action func(context.Context, io.Writer, []string) error) *cobra.Command {
b.cmd.Args = cobra.ExactArgs(argCount)
b.cmd.RunE = func(_ *cobra.Command, args []string) error {
return handleWellKnownErrors(action(b.cmd.Context(), b.cmd.OutOrStdout(), args))
err := handleWellKnownErrors(action(b.cmd.Context(), b.cmd.OutOrStdout(), args))
// clean up server.
if shutdownAPIServer != nil {
shutdownAPIServer()
}
return err
}
return &b.cmd
}

func (b *builder) NoArgs(action func(context.Context, io.Writer) error) *cobra.Command {
b.cmd.Args = cobra.NoArgs
b.cmd.RunE = func(*cobra.Command, []string) error {
return handleWellKnownErrors(action(b.cmd.Context(), b.cmd.OutOrStdout()))
err := handleWellKnownErrors(action(b.cmd.Context(), b.cmd.OutOrStdout()))
if shutdownAPIServer != nil {
shutdownAPIServer()
}
return err

}
return &b.cmd
}
Expand Down
36 changes: 32 additions & 4 deletions pkg/skaffold/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"net"
"net/http"
"time"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/sirupsen/logrus"
Expand All @@ -34,7 +35,12 @@ import (
"github.com/GoogleContainerTools/skaffold/proto"
)

var srv *server
var (
srv *server

// waits for 1 second before shutting down the server
secondTimeout = 1 * time.Second
)

type server struct {
buildIntentCallback func()
Expand Down Expand Up @@ -155,8 +161,21 @@ func newGRPCServer(port int) (func() error, error) {
}
}()
return func() error {
s.Stop()
return l.Close()
ctx, cancel := context.WithTimeout(context.Background(), secondTimeout)
defer cancel()
ch := make(chan bool, 1)
go func() {
s.GracefulStop()
ch <- true
}()
for {
select {
case <-ctx.Done():
return l.Close()
case <-ch:
return l.Close()
}
}
}, nil
}

Expand All @@ -174,9 +193,18 @@ func newHTTPServer(port, proxyPort int) (func() error, error) {
}
logrus.Infof("starting gRPC HTTP server on port %d", port)

server := &http.Server{
Handler: mux,
ReadTimeout: 10 * time.Second,
}

go http.Serve(l, mux)

return l.Close, nil
return func() error {
ctx, cancel := context.WithTimeout(context.Background(), secondTimeout)
defer cancel()
return server.Shutdown(ctx)
}, nil
}

type errResponse struct {
Expand Down

0 comments on commit 5467e82

Please sign in to comment.