open-nomad/plugins/serve.go
Mahmood Ali 2588b3bc98 cleanup driver eventor goroutines
This fixes few cases where driver eventor goroutines are leaked during
normal operations, but especially so in tests.

This change makes few modifications:

First, it switches drivers to use `Context`s to manage shutdown events.
Previously, it relied on callers invoking `.Shutdown()` function that is
specific to internal drivers only and require casting.  Using `Contexts`
provide a consistent idiomatic way to manage lifecycle for both internal
and external drivers.

Also, I discovered few places where we don't clean up a temporary driver
instance in the plugin catalog code, where we dispense a driver to
inspect and validate the schema config without properly cleaning it up.
2020-05-26 11:04:04 -04:00

52 lines
1.1 KiB
Go

package plugins
import (
"context"
"fmt"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/plugins/device"
"github.com/hashicorp/nomad/plugins/drivers"
)
// PluginFactory returns a new plugin instance
type PluginFactory func(log log.Logger) interface{}
// PluginFactory returns a new plugin instance, that takes in a context
type PluginCtxFactory func(ctx context.Context, log log.Logger) interface{}
// Serve is used to serve a new Nomad plugin
func Serve(f PluginFactory) {
logger := log.New(&log.LoggerOptions{
Level: log.Trace,
JSONFormat: true,
})
plugin := f(logger)
serve(plugin, logger)
}
// Serve is used to serve a new Nomad plugin
func ServeCtx(f PluginCtxFactory) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
logger := log.New(&log.LoggerOptions{
Level: log.Trace,
JSONFormat: true,
})
plugin := f(ctx, logger)
serve(plugin, logger)
}
func serve(plugin interface{}, logger log.Logger) {
switch p := plugin.(type) {
case device.DevicePlugin:
device.Serve(p, logger)
case drivers.DriverPlugin:
drivers.Serve(p, logger)
default:
fmt.Println("Unsupported plugin type")
}
}