Destroying the plugin if we can't connect to it

This commit is contained in:
Diptanu Choudhury
2016-02-04 13:53:30 -08:00
parent d24f089273
commit 5ea6b85e76
5 changed files with 43 additions and 0 deletions

View File

@@ -37,6 +37,7 @@ type ExecDriverConfig struct {
type execHandle struct {
pluginClient *plugin.Client
executor plugins.Executor
userPid int
killTimeout time.Duration
logger *log.Logger
waitCh chan *cstructs.WaitResult
@@ -129,6 +130,7 @@ func (d *ExecDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle,
// Return a driver handle
h := &execHandle{
pluginClient: pluginClient,
userPid: ps.Pid,
executor: executor,
killTimeout: d.DriverContext.KillTimeout(task),
logger: d.logger,
@@ -141,6 +143,7 @@ func (d *ExecDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle,
type execId struct {
KillTimeout time.Duration
UserPid int
PluginConfig *plugins.ExecutorReattachConfig
}
@@ -156,6 +159,10 @@ func (d *ExecDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, erro
}
executor, client, err := createExecutor(pluginConfig, d.config.LogOutput)
if err != nil {
d.logger.Println("[ERROR] error connecting to plugin so destroying plugin pid and user pid")
if e := destroyPlugin(id.PluginConfig.Pid, id.UserPid); e != nil {
d.logger.Printf("[ERROR] error destrouing plugin and userpid: %v", e)
}
return nil, fmt.Errorf("error connecting to plugin: %v", err)
}
@@ -163,6 +170,7 @@ func (d *ExecDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, erro
h := &execHandle{
pluginClient: client,
executor: executor,
userPid: id.UserPid,
logger: d.logger,
killTimeout: id.KillTimeout,
doneCh: make(chan struct{}),
@@ -176,6 +184,7 @@ func (h *execHandle) ID() string {
id := execId{
KillTimeout: h.killTimeout,
PluginConfig: plugins.NewExecutorReattachConfig(h.pluginClient.ReattachConfig()),
UserPid: h.userPid,
}
data, err := json.Marshal(id)

View File

@@ -202,6 +202,10 @@ func (d *JavaDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, erro
}
executor, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput)
if err != nil {
d.logger.Println("[ERROR] error connecting to plugin so destroying plugin pid and user pid")
if e := destroyPlugin(id.PluginConfig.Pid, id.UserPid); e != nil {
d.logger.Printf("[ERROR] error destroying plugin and userpid: %v", e)
}
return nil, fmt.Errorf("error connecting to plugin: %v", err)
}

View File

@@ -248,6 +248,10 @@ func (d *QemuDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, erro
executor, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput)
if err != nil {
d.logger.Println("[ERROR] error connecting to plugin so destroying plugin pid and user pid")
if e := destroyPlugin(id.PluginConfig.Pid, id.UserPid); e != nil {
d.logger.Printf("[ERROR] error destroying plugin and userpid: %v", e)
}
return nil, fmt.Errorf("error connecting to plugin: %v", err)
}

View File

@@ -151,6 +151,10 @@ func (d *RawExecDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, e
}
executor, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput)
if err != nil {
d.logger.Println("[ERROR] error connecting to plugin so destroying plugin pid and user pid")
if e := destroyPlugin(id.PluginConfig.Pid, id.UserPid); e != nil {
d.logger.Printf("[ERROR] error destrouing plugin and userpid: %v", e)
}
return nil, fmt.Errorf("error connecting to plugin: %v", err)
}

View File

@@ -3,7 +3,9 @@ package driver
import (
"fmt"
"io"
"os"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/nomad/client/driver/plugins"
)
@@ -26,3 +28,23 @@ func createExecutor(config *plugin.ClientConfig, w io.Writer) (plugins.Executor,
executorPlugin := raw.(plugins.Executor)
return executorPlugin, executorClient, nil
}
func killProcess(pid int) error {
proc, err := os.FindProcess(pid)
if err != nil {
return err
}
return proc.Kill()
}
func destroyPlugin(pluginPid int, userPid int) error {
var merr error
if err := killProcess(pluginPid); err != nil {
merr = multierror.Append(merr, err)
}
if err := killProcess(userPid); err != nil {
merr = multierror.Append(merr, err)
}
return merr
}