fixup comments, logging, and missing method impls

from #4777 comments
This commit is contained in:
Michael Schurter 2018-10-15 14:08:12 -07:00
parent 1a29337e48
commit ed5641055f
3 changed files with 55 additions and 29 deletions

View file

@ -28,13 +28,15 @@ const (
) )
var ( var (
// When the package is loaded the driver is registered as an internal plugin // PluginID is the mock driver plugin metadata registered in the plugin
// with the plugin catalog // catalog.
PluginID = loader.PluginID{ PluginID = loader.PluginID{
Name: pluginName, Name: pluginName,
PluginType: base.PluginTypeDriver, PluginType: base.PluginTypeDriver,
} }
// PluginConfig is the mock driver factory function registered in the
// plugin catalog.
PluginConfig = &loader.InternalPluginConfig{ PluginConfig = &loader.InternalPluginConfig{
Config: map[string]interface{}{}, Config: map[string]interface{}{},
Factory: func(l hclog.Logger) interface{} { return NewMockDriver(l) }, Factory: func(l hclog.Logger) interface{} { return NewMockDriver(l) },
@ -338,13 +340,13 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstru
handle := drivers.NewTaskHandle(pluginName) handle := drivers.NewTaskHandle(pluginName)
handle.Config = cfg handle.Config = cfg
if err := handle.SetDriverState(&driverState); err != nil { if err := handle.SetDriverState(&driverState); err != nil {
d.logger.Error("failed to start task, error setting driver state", "error", err) d.logger.Error("failed to start task, error setting driver state", "error", err, "task_name", cfg.Name)
return nil, nil, fmt.Errorf("failed to set driver state: %v", err) return nil, nil, fmt.Errorf("failed to set driver state: %v", err)
} }
d.tasks.Set(cfg.ID, h) d.tasks.Set(cfg.ID, h)
d.logger.Debug("starting task", "name", cfg.Name) d.logger.Debug("starting task", "task_name", cfg.Name)
go h.run() go h.run()
return handle, net, nil return handle, net, nil
@ -380,11 +382,7 @@ func (d *Driver) StopTask(taskID string, timeout time.Duration, signal string) e
return drivers.ErrTaskNotFound return drivers.ErrTaskNotFound
} }
d.logger.Debug("killing task", d.logger.Debug("killing task", "task_name", h.task.Name, "kill_after", h.killAfter)
"task_name", h.task.Name,
"kill_after", h.killAfter,
"kill_timeout", h.killTimeout,
)
select { select {
case <-h.waitCh: case <-h.waitCh:
@ -392,15 +390,20 @@ func (d *Driver) StopTask(taskID string, timeout time.Duration, signal string) e
case <-time.After(h.killAfter): case <-time.After(h.killAfter):
d.logger.Debug("killing task due to kill_after", "task_name", h.task.Name) d.logger.Debug("killing task due to kill_after", "task_name", h.task.Name)
h.kill() h.kill()
case <-time.After(h.killTimeout):
d.logger.Debug("killing task after kill_timeout", "task_name", h.task.Name)
h.kill()
} }
return nil return nil
} }
func (d *Driver) DestroyTask(taskID string, force bool) error { func (d *Driver) DestroyTask(taskID string, force bool) error {
//TODO is there anything else to do here? handle, ok := d.tasks.Get(taskID)
if !ok {
return drivers.ErrTaskNotFound
}
if handle.IsRunning() && !force {
return fmt.Errorf("cannot destroy running task")
}
d.tasks.Delete(taskID) d.tasks.Delete(taskID)
return nil return nil
} }
@ -414,8 +417,8 @@ func (d *Driver) TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) {
return nil, nil return nil, nil
} }
func (d *Driver) TaskEvents(netctx.Context) (<-chan *drivers.TaskEvent, error) { func (d *Driver) TaskEvents(ctx netctx.Context) (<-chan *drivers.TaskEvent, error) {
panic("not implemented") return d.eventer.TaskEvents(ctx)
} }
func (d *Driver) SignalTask(taskID string, signal string) error { func (d *Driver) SignalTask(taskID string, signal string) error {

View file

@ -3,6 +3,7 @@ package mock
import ( import (
"context" "context"
"io" "io"
"sync"
"time" "time"
hclog "github.com/hashicorp/go-hclog" hclog "github.com/hashicorp/go-hclog"
@ -16,7 +17,6 @@ type mockTaskHandle struct {
runFor time.Duration runFor time.Duration
killAfter time.Duration killAfter time.Duration
killTimeout time.Duration
waitCh chan struct{} waitCh chan struct{}
exitCode int exitCode int
exitSignal int exitSignal int
@ -26,8 +26,12 @@ type mockTaskHandle struct {
stdoutRepeat int stdoutRepeat int
stdoutRepeatDur time.Duration stdoutRepeatDur time.Duration
task *drivers.TaskConfig task *drivers.TaskConfig
procState drivers.TaskState
// stateLock guards the procState field
stateLock sync.Mutex
procState drivers.TaskState
startedAt time.Time startedAt time.Time
completedAt time.Time completedAt time.Time
exitResult *drivers.ExitResult exitResult *drivers.ExitResult
@ -37,8 +41,25 @@ type mockTaskHandle struct {
killCh <-chan struct{} killCh <-chan struct{}
} }
func (h *mockTaskHandle) IsRunning() bool {
h.stateLock.Lock()
defer h.stateLock.Unlock()
return h.procState == drivers.TaskStateRunning
}
func (h *mockTaskHandle) run() { func (h *mockTaskHandle) run() {
defer close(h.waitCh) defer func() {
h.stateLock.Lock()
h.procState = drivers.TaskStateExited
h.stateLock.Unlock()
h.completedAt = time.Now()
close(h.waitCh)
}()
h.stateLock.Lock()
h.procState = drivers.TaskStateRunning
h.stateLock.Unlock()
errCh := make(chan error, 1) errCh := make(chan error, 1)

View file

@ -22,14 +22,24 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
) )
// When the package is loaded the driver is registered as an internal plugin const (
// with the plugin catalog // pluginName is the name of the plugin
pluginName = "raw_exec"
// fingerprintPeriod is the interval at which the driver will send fingerprint responses
fingerprintPeriod = 30 * time.Second
)
var ( var (
// PluginID is the rawexec plugin metadata registered in the plugin
// catalog.
PluginID = loader.PluginID{ PluginID = loader.PluginID{
Name: pluginName, Name: pluginName,
PluginType: base.PluginTypeDriver, PluginType: base.PluginTypeDriver,
} }
// PluginConfig is the rawexec factory function registered in the
// plugin catalog.
PluginConfig = &loader.InternalPluginConfig{ PluginConfig = &loader.InternalPluginConfig{
Config: map[string]interface{}{}, Config: map[string]interface{}{},
Factory: func(l hclog.Logger) interface{} { return NewRawExecDriver(l) }, Factory: func(l hclog.Logger) interface{} { return NewRawExecDriver(l) },
@ -47,14 +57,6 @@ func PluginLoader(opts map[string]string) (map[string]interface{}, error) {
return conf, nil return conf, nil
} }
const (
// pluginName is the name of the plugin
pluginName = "raw_exec"
// fingerprintPeriod is the interval at which the driver will send fingerprint responses
fingerprintPeriod = 30 * time.Second
)
var ( var (
// pluginInfo is the response returned for the PluginInfo RPC // pluginInfo is the response returned for the PluginInfo RPC
pluginInfo = &base.PluginInfoResponse{ pluginInfo = &base.PluginInfoResponse{