Skip to content

Commit

Permalink
Make graceful shutdown optional (#3577)
Browse files Browse the repository at this point in the history
service.Collector is handling the signal management and implementing
a graceful shutdown. This is a problem for collectors that want to
wire their own signal handling in cases they want to add custom logic
and/or have resources other than the service.Collector to shutdown.
Making the automatic shutdown an optional behavior for users who
may need to disable it.

Fixes #3490
  • Loading branch information
rakyll authored Jul 13, 2021
1 parent b485b98 commit 56c7382
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
13 changes: 10 additions & 3 deletions service/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type Collector struct {
// signalsChannel is used to receive termination signals from the OS.
signalsChannel chan os.Signal

allowGracefulShutodwn bool

// asyncErrorChannel is used to signal a fatal error from any component.
asyncErrorChannel chan error
}
Expand All @@ -100,6 +102,9 @@ func New(set CollectorSettings) (*Collector, error) {
info: set.BuildInfo,
factories: set.Factories,
stateChannel: make(chan State, Closed+1),
// We use a negative in the settings not to break the existing
// behavior. Internally, allowGracefulShutodwn is more readable.
allowGracefulShutodwn: !set.DisableGracefulShutdown,
}

rootCmd := &cobra.Command{
Expand Down Expand Up @@ -194,9 +199,11 @@ func (col *Collector) setupTelemetry(ballastSizeBytes uint64) error {
func (col *Collector) runAndWaitForShutdownEvent() {
col.logger.Info("Everything is ready. Begin running and processing data.")

// plug SIGTERM signal into a channel.
col.signalsChannel = make(chan os.Signal, 1)
signal.Notify(col.signalsChannel, os.Interrupt, syscall.SIGTERM)
// Only notify with SIGTERM and SIGINT if graceful shutdown is enabled.
if col.allowGracefulShutodwn {
signal.Notify(col.signalsChannel, os.Interrupt, syscall.SIGTERM)
}

col.shutdownChan = make(chan struct{})
col.stateChannel <- Running
Expand All @@ -206,7 +213,7 @@ func (col *Collector) runAndWaitForShutdownEvent() {
case s := <-col.signalsChannel:
col.logger.Info("Received signal from OS", zap.String("signal", s.String()))
case <-col.shutdownChan:
col.logger.Info("Received stop test request")
col.logger.Info("Received shutdown request")
}
col.stateChannel <- Closing
}
Expand Down
6 changes: 6 additions & 0 deletions service/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ type CollectorSettings struct {
// BuildInfo provides collector start information.
BuildInfo component.BuildInfo

// DisableGracefulShutdown disables the automatic graceful shutdown
// of the collector on SIGINT or SIGTERM.
// Users who want to handle signals themselves can disable this behavior
// and manually handle the signals to shutdown the collector.
DisableGracefulShutdown bool

// ParserProvider provides the configuration's Parser.
// If it is not provided a default provider is used. The default provider loads the configuration
// from a config file define by the --config command line flag and overrides component's configuration
Expand Down

0 comments on commit 56c7382

Please sign in to comment.