diff --git a/drivers/mock/driver.go b/drivers/mock/driver.go index 3a3f3afeb..04a0c91cb 100644 --- a/drivers/mock/driver.go +++ b/drivers/mock/driver.go @@ -311,8 +311,8 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstru killCtx, killCancel := context.WithCancel(context.Background()) - h := &mockTaskHandle{ - task: cfg, + h := &taskHandle{ + taskConfig: cfg, runFor: driverConfig.RunFor, killAfter: driverConfig.KillAfter, exitCode: driverConfig.ExitCode, @@ -363,7 +363,7 @@ func (d *Driver) WaitTask(ctx netctx.Context, taskID string) (<-chan *drivers.Ex return ch, nil } -func (d *Driver) handleWait(ctx context.Context, handle *mockTaskHandle, ch chan *drivers.ExitResult) { +func (d *Driver) handleWait(ctx context.Context, handle *taskHandle, ch chan *drivers.ExitResult) { defer close(ch) select { @@ -381,13 +381,13 @@ 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) + d.logger.Debug("killing task", "task_name", h.taskConfig.Name, "kill_after", h.killAfter) select { case <-h.waitCh: - d.logger.Debug("not killing task: already exited", "task_name", h.task.Name) + d.logger.Debug("not killing task: already exited", "task_name", h.taskConfig.Name) 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.taskConfig.Name) h.kill() } return nil @@ -436,7 +436,7 @@ func (d *Driver) ExecTask(taskID string, cmd []string, timeout time.Duration) (* } res := drivers.ExecTaskResult{ - Stdout: []byte(fmt.Sprintf("Exec(%q, %q)", h.task.Name, cmd)), + Stdout: []byte(fmt.Sprintf("Exec(%q, %q)", h.taskConfig.Name, cmd)), ExitResult: &drivers.ExitResult{}, } return &res, nil diff --git a/drivers/mock/handle.go b/drivers/mock/handle.go index e20215943..67297c8f0 100644 --- a/drivers/mock/handle.go +++ b/drivers/mock/handle.go @@ -11,8 +11,8 @@ import ( "github.com/hashicorp/nomad/plugins/drivers" ) -// mockTaskHandle is a task handler which supervises a mock task -type mockTaskHandle struct { +// taskHandle supervises a mock task +type taskHandle struct { logger hclog.Logger runFor time.Duration @@ -26,10 +26,10 @@ type mockTaskHandle struct { stdoutRepeat int stdoutRepeatDur time.Duration - task *drivers.TaskConfig + taskConfig *drivers.TaskConfig // stateLock guards the procState field - stateLock sync.Mutex + stateLock sync.RWMutex procState drivers.TaskState startedAt time.Time @@ -41,13 +41,28 @@ type mockTaskHandle struct { killCh <-chan struct{} } -func (h *mockTaskHandle) IsRunning() bool { +func (h *taskHandle) TaskStatus() *drivers.TaskStatus { + h.stateLock.RLock() + defer h.stateLock.RUnlock() + + return &drivers.TaskStatus{ + ID: h.taskConfig.ID, + Name: h.taskConfig.Name, + State: h.procState, + StartedAt: h.startedAt, + CompletedAt: h.completedAt, + ExitResult: h.exitResult, + DriverAttributes: map[string]string{}, + } +} + +func (h *taskHandle) IsRunning() bool { h.stateLock.Lock() defer h.stateLock.Unlock() return h.procState == drivers.TaskStateRunning } -func (h *mockTaskHandle) run() { +func (h *taskHandle) run() { defer func() { h.stateLock.Lock() h.procState = drivers.TaskStateExited @@ -92,8 +107,8 @@ func (h *mockTaskHandle) run() { return } -func (h *mockTaskHandle) handleLogging(errCh chan<- error) { - stdout, err := fifo.Open(h.task.StdoutPath) +func (h *taskHandle) handleLogging(errCh chan<- error) { + stdout, err := fifo.Open(h.taskConfig.StdoutPath) if err != nil { h.logger.Error("failed to write to stdout", "error", err) errCh <- err diff --git a/drivers/mock/state.go b/drivers/mock/state.go index b3da44016..e6ab74638 100644 --- a/drivers/mock/state.go +++ b/drivers/mock/state.go @@ -5,21 +5,21 @@ import ( ) type taskStore struct { - store map[string]*mockTaskHandle + store map[string]*taskHandle lock sync.RWMutex } func newTaskStore() *taskStore { - return &taskStore{store: map[string]*mockTaskHandle{}} + return &taskStore{store: map[string]*taskHandle{}} } -func (ts *taskStore) Set(id string, handle *mockTaskHandle) { +func (ts *taskStore) Set(id string, handle *taskHandle) { ts.lock.Lock() defer ts.lock.Unlock() ts.store[id] = handle } -func (ts *taskStore) Get(id string) (*mockTaskHandle, bool) { +func (ts *taskStore) Get(id string) (*taskHandle, bool) { ts.lock.RLock() defer ts.lock.RUnlock() t, ok := ts.store[id]