2018-11-27 04:40:55 +00:00
|
|
|
package pluginmanager
|
|
|
|
|
|
|
|
import (
|
2018-12-08 06:04:52 +00:00
|
|
|
"context"
|
2018-11-27 04:40:55 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
|
|
)
|
|
|
|
|
2018-11-27 21:48:59 +00:00
|
|
|
// PluginGroup is a utility struct to manage a collectively orchestrate a
|
2018-11-27 04:40:55 +00:00
|
|
|
// set of PluginManagers
|
|
|
|
type PluginGroup struct {
|
|
|
|
// managers is the set of managers managed, access is synced by mLock
|
|
|
|
managers []PluginManager
|
|
|
|
|
|
|
|
// shutdown indicates if shutdown has been called, access is synced by mLock
|
|
|
|
shutdown bool
|
|
|
|
|
2019-03-04 18:25:54 +00:00
|
|
|
// mLock gaurds managers and shutdown
|
2018-11-27 04:40:55 +00:00
|
|
|
mLock sync.Mutex
|
|
|
|
|
|
|
|
logger log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns an initialized PluginGroup
|
|
|
|
func New(logger log.Logger) *PluginGroup {
|
|
|
|
return &PluginGroup{
|
|
|
|
managers: []PluginManager{},
|
|
|
|
logger: logger.Named("plugin"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 21:48:59 +00:00
|
|
|
// RegisterAndRun registers the manager and starts it in a separate goroutine
|
2018-11-27 04:40:55 +00:00
|
|
|
func (m *PluginGroup) RegisterAndRun(manager PluginManager) error {
|
|
|
|
m.mLock.Lock()
|
2018-11-28 03:42:22 +00:00
|
|
|
defer m.mLock.Unlock()
|
2018-11-27 04:40:55 +00:00
|
|
|
if m.shutdown {
|
|
|
|
return fmt.Errorf("plugin group already shutdown")
|
|
|
|
}
|
|
|
|
m.managers = append(m.managers, manager)
|
|
|
|
|
2018-12-13 06:41:44 +00:00
|
|
|
m.logger.Info("starting plugin manager", "plugin-type", manager.PluginType())
|
|
|
|
manager.Run()
|
2018-11-27 04:40:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-30 09:08:12 +00:00
|
|
|
// WaitForFirstFingerprint returns a channel which will be closed once all
|
|
|
|
// plugin managers are ready. A timeout for waiting on each manager is given
|
2018-12-13 06:41:44 +00:00
|
|
|
func (m *PluginGroup) WaitForFirstFingerprint(ctx context.Context) (<-chan struct{}, error) {
|
2018-11-28 03:42:22 +00:00
|
|
|
m.mLock.Lock()
|
|
|
|
defer m.mLock.Unlock()
|
|
|
|
if m.shutdown {
|
|
|
|
return nil, fmt.Errorf("plugin group already shutdown")
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := range m.managers {
|
2018-12-13 06:41:44 +00:00
|
|
|
manager, ok := m.managers[i].(FingerprintingPluginManager)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logger := m.logger.With("plugin-type", manager.PluginType())
|
2018-11-28 03:42:22 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2019-03-04 18:25:54 +00:00
|
|
|
logger.Debug("waiting on plugin manager initial fingerprint")
|
2020-12-10 15:27:15 +00:00
|
|
|
|
2018-11-28 03:42:22 +00:00
|
|
|
select {
|
2020-12-10 15:27:15 +00:00
|
|
|
case <-manager.WaitForFirstFingerprint(ctx):
|
|
|
|
logger.Debug("finished plugin manager initial fingerprint")
|
2020-12-09 19:05:18 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
logger.Warn("timeout waiting for plugin manager to be ready")
|
2018-11-28 03:42:22 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(ret)
|
|
|
|
}()
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2018-11-27 04:40:55 +00:00
|
|
|
// Shutdown shutsdown all registered PluginManagers in reverse order of how
|
|
|
|
// they were started.
|
|
|
|
func (m *PluginGroup) Shutdown() {
|
|
|
|
m.mLock.Lock()
|
|
|
|
defer m.mLock.Unlock()
|
|
|
|
for i := len(m.managers) - 1; i >= 0; i-- {
|
|
|
|
m.logger.Info("shutting down plugin manager", "plugin-type", m.managers[i].PluginType())
|
|
|
|
m.managers[i].Shutdown()
|
2018-12-13 06:41:44 +00:00
|
|
|
m.logger.Info("plugin manager finished", "plugin-type", m.managers[i].PluginType())
|
2018-11-27 04:40:55 +00:00
|
|
|
}
|
|
|
|
m.shutdown = true
|
|
|
|
}
|