github.com/smithx10/nomad@v0.9.1-rc1/client/allocrunner/taskrunner/driver_handle.go (about)

     1  package taskrunner
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	cstructs "github.com/hashicorp/nomad/client/structs"
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  	"github.com/hashicorp/nomad/plugins/drivers"
    10  )
    11  
    12  // NewDriverHandle returns a handle for task operations on a specific task
    13  func NewDriverHandle(driver drivers.DriverPlugin, taskID string, task *structs.Task, net *drivers.DriverNetwork) *DriverHandle {
    14  	return &DriverHandle{
    15  		driver: driver,
    16  		net:    net,
    17  		taskID: taskID,
    18  		task:   task,
    19  	}
    20  }
    21  
    22  // DriverHandle encapsulates a driver plugin client and task identifier and exposes
    23  // an api to perform driver operations on the task
    24  type DriverHandle struct {
    25  	driver drivers.DriverPlugin
    26  	net    *drivers.DriverNetwork
    27  	task   *structs.Task
    28  	taskID string
    29  }
    30  
    31  func (h *DriverHandle) ID() string {
    32  	return h.taskID
    33  }
    34  
    35  func (h *DriverHandle) WaitCh(ctx context.Context) (<-chan *drivers.ExitResult, error) {
    36  	return h.driver.WaitTask(ctx, h.taskID)
    37  }
    38  
    39  func (h *DriverHandle) Update(task *structs.Task) error {
    40  	return nil
    41  }
    42  
    43  func (h *DriverHandle) Kill() error {
    44  	return h.driver.StopTask(h.taskID, h.task.KillTimeout, h.task.KillSignal)
    45  }
    46  
    47  func (h *DriverHandle) Stats(ctx context.Context, interval time.Duration) (<-chan *cstructs.TaskResourceUsage, error) {
    48  	return h.driver.TaskStats(ctx, h.taskID, interval)
    49  }
    50  
    51  func (h *DriverHandle) Signal(s string) error {
    52  	return h.driver.SignalTask(h.taskID, s)
    53  }
    54  
    55  func (h *DriverHandle) Exec(timeout time.Duration, cmd string, args []string) ([]byte, int, error) {
    56  	command := append([]string{cmd}, args...)
    57  	res, err := h.driver.ExecTask(h.taskID, command, timeout)
    58  	if err != nil {
    59  		return nil, 0, err
    60  	}
    61  	return res.Stdout, res.ExitResult.ExitCode, res.ExitResult.Err
    62  }
    63  
    64  func (h *DriverHandle) Network() *drivers.DriverNetwork {
    65  	return h.net
    66  }