Update plugin to use cleanup

This commit is contained in:
Chris Roberts 2022-04-25 15:44:06 -07:00
parent 5b04ac5c35
commit 6d45d07f3f
3 changed files with 37 additions and 54 deletions

View File

@ -130,7 +130,6 @@ func (b *Builtin) Factory(name string) PluginRegistration {
Location: fmt.Sprintf("builtin::%s", name),
Name: info.Name(),
Types: info.ComponentTypes(),
components: map[component.Type]*Instance{},
logger: log,
src: client,
}

View File

@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/vagrant-plugin-sdk/component"
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/cleanup"
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/pluginclient"
)
@ -104,7 +105,7 @@ func Factory(
Name: info.Name(),
Types: info.ComponentTypes(),
Mappers: mappers,
components: map[component.Type]*Instance{},
cleaner: cleanup.New(),
logger: nlog.Named(info.Name()),
src: client,
}
@ -143,8 +144,10 @@ func RubyFactory(
Location: "ruby-runtime",
Name: name,
Types: []component.Type{typ},
components: map[component.Type]*Instance{},
logger: log.ResetNamed("vagrant.legacy." + strings.ToLower(typ.String())),
cleaner: cleanup.New(),
logger: log.ResetNamed(
fmt.Sprintf("vagrant.legacy-plugin.%s.%s", strings.ToLower(typ.String()), name),
),
}, nil
}
}
@ -172,7 +175,7 @@ type Instance struct {
// Closer is a function that should be called to clean up resources
// associated with this plugin.
Close func()
Close func() error
}
func (i *Instance) Parents() []string {

View File

@ -2,18 +2,19 @@ package plugin
import (
"fmt"
"io"
"strings"
"sync"
"github.com/hashicorp/go-argmapper"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-plugin"
sdk "github.com/hashicorp/vagrant-plugin-sdk"
"github.com/hashicorp/vagrant-plugin-sdk/component"
"github.com/hashicorp/vagrant-plugin-sdk/core"
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/cacher"
"github.com/hashicorp/vagrant-plugin-sdk/internal-shared/cleanup"
"github.com/hashicorp/vagrant/builtin/myplugin"
"github.com/hashicorp/vagrant/builtin/otherplugin"
)
@ -44,8 +45,7 @@ type Plugin struct {
Name string // Name of the plugin
Types []component.Type // Component types supported by this plugin
closers []func() error // Functions to be called when manager is closed
components map[component.Type]*Instance // Map of created instances
cleaner cleanup.Cleanup // Cleanup tasks to perform on closing
logger hclog.Logger
m sync.Mutex
manager *Manager // Plugin manager this plugin belongs to
@ -88,7 +88,7 @@ func (p *Plugin) HasType(
// Add a callback to execute when plugin is closed
func (p *Plugin) Closer(c func() error) {
p.closers = append(p.closers, c)
p.cleaner.Do(c)
}
// Calls all registered close callbacks
@ -96,17 +96,13 @@ func (p *Plugin) Close() (err error) {
p.m.Lock()
defer p.m.Unlock()
for _, c := range p.closers {
if e := c(); e != nil {
multierror.Append(err, e)
}
}
return
return p.cleaner.Close()
}
// Get specific component type from plugin
func (p *Plugin) InstanceOf(
c component.Type,
cfns []PluginConfigurator,
) (i *Instance, err error) {
p.m.Lock()
defer p.m.Unlock()
@ -134,21 +130,6 @@ func (p *Plugin) InstanceOf(
return
}
// Register cleanup for the instance
p.Closer(func() error {
p.logger.Warn("closing plugin instance",
"plugin", p.Name,
"instance", hclog.Fmt("%T", raw),
)
if c, ok := raw.(interface {
Close() error
}); ok {
return c.Close()
}
return nil
})
// Extract the GRPC broker if possible
b, ok := raw.(HasGRPCBroker)
if !ok {