Skip to content

Commit

Permalink
fixup comments, logging, and missing method impls
Browse files Browse the repository at this point in the history
from #4777 comments
  • Loading branch information
schmichael committed Oct 15, 2018
1 parent 88e38d3 commit 071ec14
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
33 changes: 18 additions & 15 deletions drivers/mock/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ const (
)

var (
// When the package is loaded the driver is registered as an internal plugin
// with the plugin catalog
// PluginID is the mock driver plugin metadata registered in the plugin
// catalog.
PluginID = loader.PluginID{
Name: pluginName,
PluginType: base.PluginTypeDriver,
}

// PluginConfig is the mock driver factory function registered in the
// plugin catalog.
PluginConfig = &loader.InternalPluginConfig{
Config: map[string]interface{}{},
Factory: func(l hclog.Logger) interface{} { return NewMockDriver(l) },
Expand Down Expand Up @@ -338,13 +340,13 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstru
handle := drivers.NewTaskHandle(pluginName)
handle.Config = cfg
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)
}

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()
return handle, net, nil

Expand Down Expand Up @@ -380,27 +382,28 @@ func (d *Driver) StopTask(taskID string, timeout time.Duration, signal string) e
return drivers.ErrTaskNotFound
}

d.logger.Debug("killing task",
"task_name", h.task.Name,
"kill_after", h.killAfter,
"kill_timeout", h.killTimeout,
)
d.logger.Debug("killing task", "task_name", h.task.Name, "kill_after", h.killAfter)

select {
case <-h.waitCh:
d.logger.Debug("not killing task: already exited", "task_name", h.task.Name)
case <-time.After(h.killAfter):
d.logger.Debug("killing task due to kill_after", "task_name", h.task.Name)
h.kill()
case <-time.After(h.killTimeout):
d.logger.Debug("killing task after kill_timeout", "task_name", h.task.Name)
h.kill()
}
return nil
}

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)
return nil
}
Expand All @@ -414,8 +417,8 @@ func (d *Driver) TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) {
return nil, nil
}

func (d *Driver) TaskEvents(netctx.Context) (<-chan *drivers.TaskEvent, error) {
panic("not implemented")
func (d *Driver) TaskEvents(ctx netctx.Context) (<-chan *drivers.TaskEvent, error) {
return d.eventer.TaskEvents(ctx)
}

func (d *Driver) SignalTask(taskID string, signal string) error {
Expand Down
29 changes: 25 additions & 4 deletions drivers/mock/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mock
import (
"context"
"io"
"sync"
"time"

hclog "github.com/hashicorp/go-hclog"
Expand All @@ -16,7 +17,6 @@ type mockTaskHandle struct {

runFor time.Duration
killAfter time.Duration
killTimeout time.Duration
waitCh chan struct{}
exitCode int
exitSignal int
Expand All @@ -26,8 +26,12 @@ type mockTaskHandle struct {
stdoutRepeat int
stdoutRepeatDur time.Duration

task *drivers.TaskConfig
procState drivers.TaskState
task *drivers.TaskConfig

// stateLock guards the procState field
stateLock sync.Mutex
procState drivers.TaskState

startedAt time.Time
completedAt time.Time
exitResult *drivers.ExitResult
Expand All @@ -37,8 +41,25 @@ type mockTaskHandle 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() {
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)

Expand Down
22 changes: 12 additions & 10 deletions drivers/rawexec/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,24 @@ import (
"golang.org/x/net/context"
)

// When the package is loaded the driver is registered as an internal plugin
// with the plugin catalog
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 (
// PluginID is the rawexec plugin metadata registered in the plugin
// catalog.
PluginID = loader.PluginID{
Name: pluginName,
PluginType: base.PluginTypeDriver,
}

// PluginConfig is the rawexec factory function registered in the
// plugin catalog.
PluginConfig = &loader.InternalPluginConfig{
Config: map[string]interface{}{},
Factory: func(l hclog.Logger) interface{} { return NewRawExecDriver(l) },
Expand All @@ -47,14 +57,6 @@ func PluginLoader(opts map[string]string) (map[string]interface{}, error) {
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 (
// pluginInfo is the response returned for the PluginInfo RPC
pluginInfo = &base.PluginInfoResponse{
Expand Down

0 comments on commit 071ec14

Please sign in to comment.