Worker graceful shutdown (#2455)

* add graceful shutdown to worker
This commit is contained in:
Irena Rindos authored and GitHub committed 2022-09-19 09:59:18 -04:00
1 parent a2ef14283b
commit d951e1ebc1
16 files changed
+241 -229

No files matched your search

+21 -6
View File
@@ -76,9 +76,10 @@ var reRemoveWhitespace = regexp.MustCompile(`[\s]+`)
var DevOnlyControllerFlags = func(*Command, *FlagSet) {}
type Command struct {
Context context.Context
UI cli.Ui
ShutdownCh chan struct{}
Context context.Context
ContextCancel context.CancelFunc
UI cli.Ui
ShutdownCh chan struct{}
flags *FlagSets
flagsOnce sync.Once
@@ -143,7 +144,6 @@ func NewCommand(ui cli.Ui) *Command {
ShutdownCh: MakeShutdownCh(),
Context: ctx,
}
go func() {
<-ret.ShutdownCh
cancel()
@@ -152,6 +152,19 @@ func NewCommand(ui cli.Ui) *Command {
return ret
}
// New returns a new instance of a base.Command type that does not intercept the shutdown channel
func NewServerCommand(ui cli.Ui) *Command {
ctx, cancel := context.WithCancel(context.Background())
ret := &Command{
UI: ui,
ShutdownCh: MakeShutdownCh(),
Context: ctx,
ContextCancel: cancel,
}
return ret
}
// MakeShutdownCh returns a channel that can be used for shutdown
// notifications for commands. This channel will send a message for every
// SIGINT or SIGTERM received.
@@ -161,8 +174,10 @@ func MakeShutdownCh() chan struct{} {
shutdownCh := make(chan os.Signal, 4)
signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-shutdownCh
close(resultCh)
for {
<-shutdownCh
resultCh <- struct{}{}
}
}()
return resultCh
}