github.com/manicqin/nomad@v0.9.5/plugins/drivers/task_handle.go (about)

     1  package drivers
     2  
     3  import (
     4  	"github.com/hashicorp/nomad/plugins/base"
     5  )
     6  
     7  // TaskHandle is the state shared between a driver and the client.
     8  // It is returned to the client after starting the task and used
     9  // for recovery of tasks during a driver restart.
    10  type TaskHandle struct {
    11  	// Version is set by the driver an allows it to handle upgrading from
    12  	// an older DriverState struct. Prior to 0.9 the only state stored for
    13  	// driver was the reattach config for the executor. To allow upgrading to
    14  	// 0.9, Version 0 is handled as if it is the json encoded reattach config.
    15  	Version     int
    16  	Config      *TaskConfig
    17  	State       TaskState
    18  	DriverState []byte
    19  }
    20  
    21  func NewTaskHandle(version int) *TaskHandle {
    22  	return &TaskHandle{Version: version}
    23  }
    24  
    25  func (h *TaskHandle) SetDriverState(v interface{}) error {
    26  	h.DriverState = []byte{}
    27  	return base.MsgPackEncode(&h.DriverState, v)
    28  }
    29  
    30  func (h *TaskHandle) GetDriverState(v interface{}) error {
    31  	return base.MsgPackDecode(h.DriverState, v)
    32  
    33  }
    34  
    35  func (h *TaskHandle) Copy() *TaskHandle {
    36  	if h == nil {
    37  		return nil
    38  	}
    39  
    40  	handle := new(TaskHandle)
    41  	handle.Version = h.Version
    42  	handle.Config = h.Config.Copy()
    43  	handle.State = h.State
    44  	handle.DriverState = make([]byte, len(h.DriverState))
    45  	copy(handle.DriverState, h.DriverState)
    46  	return handle
    47  }