github.com/ilhicas/nomad@v0.11.8/drivers/exec/handle.go (about)

     1  package exec
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  	"sync"
     7  	"time"
     8  
     9  	hclog "github.com/hashicorp/go-hclog"
    10  	plugin "github.com/hashicorp/go-plugin"
    11  	"github.com/hashicorp/nomad/drivers/shared/executor"
    12  	"github.com/hashicorp/nomad/plugins/drivers"
    13  )
    14  
    15  type taskHandle struct {
    16  	exec         executor.Executor
    17  	pid          int
    18  	pluginClient *plugin.Client
    19  	logger       hclog.Logger
    20  
    21  	// stateLock syncs access to all fields below
    22  	stateLock sync.RWMutex
    23  
    24  	taskConfig  *drivers.TaskConfig
    25  	procState   drivers.TaskState
    26  	startedAt   time.Time
    27  	completedAt time.Time
    28  	exitResult  *drivers.ExitResult
    29  }
    30  
    31  func (h *taskHandle) TaskStatus() *drivers.TaskStatus {
    32  	h.stateLock.RLock()
    33  	defer h.stateLock.RUnlock()
    34  
    35  	return &drivers.TaskStatus{
    36  		ID:          h.taskConfig.ID,
    37  		Name:        h.taskConfig.Name,
    38  		State:       h.procState,
    39  		StartedAt:   h.startedAt,
    40  		CompletedAt: h.completedAt,
    41  		ExitResult:  h.exitResult,
    42  		DriverAttributes: map[string]string{
    43  			"pid": strconv.Itoa(h.pid),
    44  		},
    45  	}
    46  }
    47  
    48  func (h *taskHandle) IsRunning() bool {
    49  	h.stateLock.RLock()
    50  	defer h.stateLock.RUnlock()
    51  	return h.procState == drivers.TaskStateRunning
    52  }
    53  
    54  func (h *taskHandle) run() {
    55  	h.stateLock.Lock()
    56  	if h.exitResult == nil {
    57  		h.exitResult = &drivers.ExitResult{}
    58  	}
    59  	h.stateLock.Unlock()
    60  
    61  	// Block until process exits
    62  	ps, err := h.exec.Wait(context.Background())
    63  
    64  	h.stateLock.Lock()
    65  	defer h.stateLock.Unlock()
    66  
    67  	if err != nil {
    68  		h.exitResult.Err = err
    69  		h.procState = drivers.TaskStateUnknown
    70  		h.completedAt = time.Now()
    71  		return
    72  	}
    73  	h.procState = drivers.TaskStateExited
    74  	h.exitResult.ExitCode = ps.ExitCode
    75  	h.exitResult.Signal = ps.Signal
    76  	h.completedAt = ps.Time
    77  
    78  	// TODO: detect if the task OOMed
    79  }