Skip to content

Commit

Permalink
SIGHUP configuration reload
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Porter <portertech@gmail.com>
  • Loading branch information
portertech committed Aug 30, 2022
1 parent 5772e58 commit 935c463
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions service/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,21 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
return nil
}

func (col *Collector) reloadConfiguration(ctx context.Context) error {
col.service.telemetrySettings.Logger.Warn("Config updated, restart service")
col.setCollectorState(Closing)

if err := col.service.Shutdown(ctx); err != nil {
return fmt.Errorf("failed to shutdown the retiring config: %w", err)
}

if err := col.setupConfigurationComponents(ctx); err != nil {
return fmt.Errorf("failed to setup configuration components: %w", err)
}

return nil
}

// Run starts the collector according to the given configuration, and waits for it to complete.
// Consecutive calls to Run are not allowed, Run shouldn't be called once a collector is shut down.
func (col *Collector) Run(ctx context.Context) error {
Expand All @@ -164,7 +179,7 @@ func (col *Collector) Run(ctx context.Context) error {

// Only notify with SIGTERM and SIGINT if graceful shutdown is enabled.
if !col.set.DisableGracefulShutdown {
signal.Notify(col.signalsChannel, os.Interrupt, syscall.SIGTERM)
signal.Notify(col.signalsChannel, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
}

LOOP:
Expand All @@ -176,21 +191,22 @@ LOOP:
break LOOP
}

col.service.telemetrySettings.Logger.Warn("Config updated, restart service")
col.setCollectorState(Closing)

if err = col.service.Shutdown(ctx); err != nil {
return fmt.Errorf("failed to shutdown the retiring config: %w", err)
}
if err = col.setupConfigurationComponents(ctx); err != nil {
return fmt.Errorf("failed to setup configuration components: %w", err)
if err = col.reloadConfiguration(ctx); err != nil {
return err
}
case err := <-col.asyncErrorChannel:
col.service.telemetrySettings.Logger.Error("Asynchronous error received, terminating process", zap.Error(err))
break LOOP
case s := <-col.signalsChannel:
col.service.telemetrySettings.Logger.Info("Received signal from OS", zap.String("signal", s.String()))
break LOOP
switch s {
case syscall.SIGHUP:
if err := col.reloadConfiguration(ctx); err != nil {
return err
}
default:
break LOOP
}
case <-col.shutdownChan:
col.service.telemetrySettings.Logger.Info("Received shutdown request")
break LOOP
Expand Down

0 comments on commit 935c463

Please sign in to comment.