From 319a0ae2bf7cada375b39a3055be81f0ec42d33f Mon Sep 17 00:00:00 2001 From: Siva Date: Wed, 20 Jun 2018 14:30:17 -0400 Subject: [PATCH] Graceful exits added --- command/agent/agent.go | 3 +++ service_os/service.go | 6 ++++++ service_os/service_windows.go | 7 +++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index 90a040db80..a23b93860c 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -21,6 +21,7 @@ import ( "github.com/hashicorp/consul/command/flags" "github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/logger" + "github.com/hashicorp/consul/service_os" "github.com/hashicorp/go-checkpoint" multierror "github.com/hashicorp/go-multierror" "github.com/hashicorp/logutils" @@ -411,6 +412,8 @@ func (c *cmd) run(args []string) int { case ch := <-agent.ReloadCh(): sig = syscall.SIGHUP reloadErrCh = ch + case <-service_os.Shutdown_Channel(): + sig = os.Interrupt case <-c.shutdownCh: sig = os.Interrupt case err := <-agent.RetryJoinCh(): diff --git a/service_os/service.go b/service_os/service.go index 6db6263b6f..432baaf2ad 100644 --- a/service_os/service.go +++ b/service_os/service.go @@ -1 +1,7 @@ package service_os + +var chanGraceExit = make(chan int) + +func Shutdown_Channel() <-chan int { + return chanGraceExit +} diff --git a/service_os/service_windows.go b/service_os/service_windows.go index fa3e8b30c8..a160d2bcae 100644 --- a/service_os/service_windows.go +++ b/service_os/service_windows.go @@ -3,11 +3,12 @@ package service_os import ( - "os" wsvc "golang.org/x/sys/windows/svc" + "os" + "time" ) -type serviceWindows struct {} +type serviceWindows struct{} func init() { interactive, err := wsvc.IsAnInteractiveSession() @@ -19,6 +20,7 @@ func init() { } go func() { _ = wsvc.Run("", serviceWindows{}) + time.Sleep(4 * time.Second) os.Exit(0) }() } @@ -34,6 +36,7 @@ func (serviceWindows) Execute(args []string, r <-chan wsvc.ChangeRequest, s chan case wsvc.Interrogate: s <- c.CurrentStatus case wsvc.Stop, wsvc.Shutdown: + chanGraceExit <- 1 s <- wsvc.Status{State: wsvc.StopPending} return false, 0 }