Skip to content

Commit

Permalink
Fix autoupdate context shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Maksimov committed Oct 29, 2020
1 parent 2c41f20 commit e1ffaa5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.8.0
v1.8.1
17 changes: 9 additions & 8 deletions services/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func Execute(ctx context.Context, flags *config.Flags, logger log.Logger) (govnr
return nil, fmt.Errorf("--keys is a required parameter for provisioning flow")
}

// crucial for a proper shutdown
ctxWithCancel, cancelAndExit := context.WithCancel(ctx)
supervisor := &govnr.TreeSupervisor{}

// clean up old files
Expand All @@ -29,14 +31,13 @@ func Execute(ctx context.Context, flags *config.Flags, logger log.Logger) (govnr
if flags.StatusFilePath == "" && flags.MetricsFilePath == "" {
logger.Info("status file path and metrics file path are empty, periodical report disabled")
} else {
supervisor.Supervise(WatchAndReportStatusAndMetrics(ctx, logger, flags.StatusFilePath, flags.MetricsFilePath))
supervisor.Supervise(WatchAndReportStatusAndMetrics(ctxWithCancel, logger, flags.StatusFilePath, flags.MetricsFilePath))
}

cfgFetcher := NewConfigurationPollService(flags, logger)
coreBoyar := NewCoreBoyarService(logger)

// wire cfg and boyar
ctxWithCancel, cancelAndExit := context.WithCancel(ctx)
supervisor.Supervise(govnr.Forever(ctxWithCancel, "apply config changes", utils.NewLogErrors("apply config changes", logger), func() {
var cfg config.NodeConfiguration = nil
select {
Expand All @@ -49,7 +50,7 @@ func Execute(ctx context.Context, flags *config.Flags, logger log.Logger) (govnr
}
// random delay when provisioning change (that is, not bootstrap flow or repairing broken system)
if coreBoyar.healthy {
maybeDelayConfigUpdate(ctx, cfg, flags.MaxReloadTimeDelay, coreBoyar.logger)
maybeDelayConfigUpdate(ctxWithCancel, cfg, flags.MaxReloadTimeDelay, coreBoyar.logger)
} else {
logger.Info("applying new configuration immediately")
}
Expand All @@ -60,22 +61,22 @@ func Execute(ctx context.Context, flags *config.Flags, logger log.Logger) (govnr
return
}

ctx, cancel := context.WithTimeout(ctx, flags.Timeout)
ctxWithTimeout, cancel := context.WithTimeout(ctxWithCancel, flags.Timeout)
defer cancel()

err := coreBoyar.OnConfigChange(ctx, cfg)
err := coreBoyar.OnConfigChange(ctxWithTimeout, cfg)
if err != nil {
logger.Error("error executing configuration", log.Error(err))
cfgFetcher.Resend()
}

if ctx.Err() != nil {
logger.Error("failed to apply new configuration", log.Error(ctx.Err()))
if ctxWithTimeout.Err() != nil {
logger.Error("failed to apply new configuration", log.Error(ctxWithTimeout.Err()))
return
}
}))

supervisor.Supervise(cfgFetcher.Start(ctx))
supervisor.Supervise(cfgFetcher.Start(ctxWithCancel))

return supervisor, nil
}

0 comments on commit e1ffaa5

Please sign in to comment.