-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jesus Vazquez <jesus.vazquez@grafana.com>
- Loading branch information
1 parent
26b6197
commit 216e6ac
Showing
5 changed files
with
71 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package stopsignal | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
) | ||
|
||
func NewSignalHandler(shutdownDelay time.Duration, logger log.Logger) *SignalHandler { | ||
return &SignalHandler{ | ||
quit: make(chan struct{}), | ||
shutdownDelay: shutdownDelay, | ||
logger: logger, | ||
} | ||
} | ||
|
||
type SignalHandler struct { | ||
quit chan struct{} | ||
ready atomic.Bool | ||
shutdownDelay time.Duration | ||
logger log.Logger | ||
} | ||
|
||
func (sh *SignalHandler) Handler(signals ...os.Signal) (run func() error, stop func(error)) { | ||
sh.ready.Store(true) | ||
|
||
sigs := make(chan os.Signal, 1) | ||
signal.Notify(sigs, signals...) | ||
|
||
return func() error { | ||
level.Info(sh.logger).Log("msg", "Waiting for stop signal...") | ||
select { | ||
case <-sh.quit: | ||
return nil | ||
|
||
case <-sigs: | ||
level.Info(sh.logger).Log("msg", "=== received SIGINT/SIGTERM ===", "sleep", sh.shutdownDelay) | ||
|
||
// Not ready anymore. | ||
sh.ready.Store(false) | ||
if sh.shutdownDelay > 0 { | ||
time.Sleep(sh.shutdownDelay) | ||
} | ||
|
||
level.Info(sh.logger).Log("msg", "shutting down") | ||
return nil | ||
} | ||
}, | ||
func(_ error) { | ||
sh.Stop() | ||
} | ||
} | ||
|
||
func (sh *SignalHandler) Stop() { | ||
close(sh.quit) | ||
} | ||
|
||
func (sh *SignalHandler) Ready() bool { | ||
return sh.ready.Load() | ||
} |
This file was deleted.
Oops, something went wrong.