improved OnDeactivate handling (#11988)

* Deactivate plugins in parallel to improve shutdown time
* Give plugins at most 10s to handle OnDeactivate before forcefully terminating
This commit is contained in:
Jesse Hallam
2019-09-05 17:27:36 -03:00
committed by GitHub
parent 9d937e7f1d
commit 451982f9d3
2 changed files with 98 additions and 3 deletions

View File

@@ -288,16 +288,42 @@ func (env *Environment) Shutdown() {
env.pluginHealthCheckJob.Cancel()
}
var wg sync.WaitGroup
env.registeredPlugins.Range(func(key, value interface{}) bool {
rp := value.(*registeredPlugin)
if rp.supervisor != nil {
if rp.supervisor == nil {
return true
}
wg.Add(1)
done := make(chan bool)
go func() {
defer close(done)
if err := rp.supervisor.Hooks().OnDeactivate(); err != nil {
env.logger.Error("Plugin OnDeactivate() error", mlog.String("plugin_id", rp.BundleInfo.Manifest.Id), mlog.Err(err))
}
rp.supervisor.Shutdown()
}
}()
go func() {
defer wg.Done()
select {
case <-time.After(10 * time.Second):
env.logger.Warn("Plugin OnDeactivate() failed to complete in 10 seconds", mlog.String("plugin_id", rp.BundleInfo.Manifest.Id))
case <-done:
}
rp.supervisor.Shutdown()
}()
return true
})
wg.Wait()
env.registeredPlugins.Range(func(key, value interface{}) bool {
env.registeredPlugins.Delete(key)
return true