github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/drivers/mock/handle.go (about)

     1  package mock
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  	"time"
     7  
     8  	hclog "github.com/hashicorp/go-hclog"
     9  	"github.com/hashicorp/nomad/client/lib/fifo"
    10  	"github.com/hashicorp/nomad/plugins/drivers"
    11  )
    12  
    13  // taskHandle supervises a mock task
    14  type taskHandle struct {
    15  	logger hclog.Logger
    16  
    17  	pluginExitAfter time.Duration
    18  	killAfter       time.Duration
    19  	waitCh          chan interface{}
    20  
    21  	taskConfig  *drivers.TaskConfig
    22  	command     Command
    23  	execCommand *Command
    24  
    25  	// stateLock guards the procState field
    26  	stateLock sync.RWMutex
    27  	procState drivers.TaskState
    28  
    29  	startedAt   time.Time
    30  	completedAt time.Time
    31  	exitResult  *drivers.ExitResult
    32  
    33  	// Calling kill closes killCh if it is not already closed
    34  	kill   context.CancelFunc
    35  	killCh <-chan struct{}
    36  
    37  	// Recovered is set to true if the handle was created while being recovered
    38  	Recovered bool
    39  }
    40  
    41  func (h *taskHandle) TaskStatus() *drivers.TaskStatus {
    42  	h.stateLock.RLock()
    43  	defer h.stateLock.RUnlock()
    44  
    45  	return &drivers.TaskStatus{
    46  		ID:               h.taskConfig.ID,
    47  		Name:             h.taskConfig.Name,
    48  		State:            h.procState,
    49  		StartedAt:        h.startedAt,
    50  		CompletedAt:      h.completedAt,
    51  		ExitResult:       h.exitResult,
    52  		DriverAttributes: map[string]string{},
    53  	}
    54  }
    55  
    56  func (h *taskHandle) IsRunning() bool {
    57  	h.stateLock.Lock()
    58  	defer h.stateLock.Unlock()
    59  	return h.procState == drivers.TaskStateRunning
    60  }
    61  
    62  func (h *taskHandle) run() {
    63  	defer func() {
    64  		h.stateLock.Lock()
    65  		h.procState = drivers.TaskStateExited
    66  		h.stateLock.Unlock()
    67  
    68  		h.completedAt = time.Now()
    69  		close(h.waitCh)
    70  	}()
    71  
    72  	h.stateLock.Lock()
    73  	h.procState = drivers.TaskStateRunning
    74  	h.stateLock.Unlock()
    75  
    76  	var pluginExitTimer <-chan time.Time
    77  	if h.pluginExitAfter != 0 {
    78  		timer := time.NewTimer(h.pluginExitAfter)
    79  		defer timer.Stop()
    80  		pluginExitTimer = timer.C
    81  	}
    82  
    83  	stdout, err := fifo.OpenWriter(h.taskConfig.StdoutPath)
    84  	if err != nil {
    85  		h.logger.Error("failed to write to stdout", "error", err)
    86  		h.exitResult = &drivers.ExitResult{Err: err}
    87  		return
    88  	}
    89  	stderr, err := fifo.OpenWriter(h.taskConfig.StderrPath)
    90  	if err != nil {
    91  		h.logger.Error("failed to write to stderr", "error", err)
    92  		h.exitResult = &drivers.ExitResult{Err: err}
    93  		return
    94  	}
    95  
    96  	h.exitResult = runCommand(h.command, stdout, stderr, h.killCh, pluginExitTimer, h.logger)
    97  	return
    98  }