github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/allocrunner/interfaces/task_lifecycle.go (about)

     1  package interfaces
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/hashicorp/nomad/client/allocdir"
     8  	"github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces"
     9  	cstructs "github.com/hashicorp/nomad/client/structs"
    10  	"github.com/hashicorp/nomad/client/taskenv"
    11  	"github.com/hashicorp/nomad/nomad/structs"
    12  	"github.com/hashicorp/nomad/plugins/drivers"
    13  )
    14  
    15  /*
    16  
    17                              Restart
    18        +--------------------------------------------------------+
    19        |                                                        |
    20        |                      *Update                           |
    21        |                     +-------+                          |
    22        |                     |       |                          |
    23        |                     |       |                          |
    24        |                  +---v-------+----+                    |
    25   +----v----+             |    Running     |               +----+-----+           +--------------+
    26   |         | *Prestart   |----------------|      *Exited  |          |  *Stop    |              |
    27   | Pending +-------------> *Poststart run +---^-----------> Exited   +----------->  Terminal    |
    28   |         |             |  upon entering |   |           |          | NoRestart |              |
    29   +---------+             |  running       |   |           +----------+           +--------------+
    30                           |                |   |
    31                           +--------+-------+   |
    32                                    |           |
    33                                    +-----------+
    34                                       *Kill
    35                                  (forces terminal)
    36  
    37  Link: http://stable.ascii-flow.appspot.com/#Draw4489375405966393064/1824429135
    38  */
    39  
    40  // TaskHook is a lifecycle hook into the life cycle of a task runner.
    41  type TaskHook interface {
    42  	Name() string
    43  }
    44  
    45  type TaskPrestartRequest struct {
    46  	// PreviousState is previously set data by the hook. It must be copied
    47  	// to State below to be maintained across restarts.
    48  	PreviousState map[string]string
    49  
    50  	// Task is the task to run
    51  	Task *structs.Task
    52  
    53  	// TaskResources is the resources assigned to the task
    54  	TaskResources *structs.AllocatedTaskResources
    55  
    56  	// Vault token may optionally be set if a Vault token is available
    57  	VaultToken string
    58  
    59  	// NomadToken token may optionally be set if a Nomad token is available
    60  	NomadToken string
    61  
    62  	// TaskDir contains the task's directory tree on the host
    63  	TaskDir *allocdir.TaskDir
    64  
    65  	// TaskEnv is the task's environment
    66  	TaskEnv *taskenv.TaskEnv
    67  }
    68  
    69  type TaskPrestartResponse struct {
    70  	// Env is the environment variables to set for the task
    71  	Env map[string]string
    72  
    73  	// Mounts is the set of host volumes to mount into the task
    74  	Mounts []*drivers.MountConfig
    75  
    76  	// Devices are the set of devices to mount into the task
    77  	Devices []*drivers.DeviceConfig
    78  
    79  	// State allows the hook to emit data to be passed in the next time it is
    80  	// run. Hooks must copy relevant PreviousState to State to maintain it
    81  	// across restarts.
    82  	State map[string]string
    83  
    84  	// Done lets the hook indicate that it completed successfully and
    85  	// should not be run again.
    86  	Done bool
    87  }
    88  
    89  type TaskPrestartHook interface {
    90  	TaskHook
    91  
    92  	// Prestart is called before the task is started including after every
    93  	// restart. Prestart is not called if the allocation is terminal.
    94  	//
    95  	// The context is cancelled if the task is killed or shutdown but
    96  	// should not be stored any persistent goroutines this Prestart
    97  	// creates.
    98  	Prestart(context.Context, *TaskPrestartRequest, *TaskPrestartResponse) error
    99  }
   100  
   101  // DriverStats is the interface implemented by DriverHandles to return task stats.
   102  type DriverStats interface {
   103  	Stats(context.Context, time.Duration) (<-chan *cstructs.TaskResourceUsage, error)
   104  }
   105  
   106  type TaskPoststartRequest struct {
   107  	// Exec hook (may be nil)
   108  	DriverExec interfaces.ScriptExecutor
   109  
   110  	// Network info (may be nil)
   111  	DriverNetwork *drivers.DriverNetwork
   112  
   113  	// TaskEnv is the task's environment
   114  	TaskEnv *taskenv.TaskEnv
   115  
   116  	// Stats collector
   117  	DriverStats DriverStats
   118  }
   119  type TaskPoststartResponse struct{}
   120  
   121  type TaskPoststartHook interface {
   122  	TaskHook
   123  
   124  	// Poststart is called after the task has started. Poststart is not
   125  	// called if the allocation is terminal.
   126  	//
   127  	// The context is cancelled if the task is killed.
   128  	Poststart(context.Context, *TaskPoststartRequest, *TaskPoststartResponse) error
   129  }
   130  
   131  type TaskPreKillRequest struct{}
   132  type TaskPreKillResponse struct{}
   133  
   134  type TaskPreKillHook interface {
   135  	TaskHook
   136  
   137  	// PreKilling is called right before a task is going to be killed or
   138  	// restarted. They are called concurrently with TaskRunner.Run and may
   139  	// be called without Prestart being called.
   140  	PreKilling(context.Context, *TaskPreKillRequest, *TaskPreKillResponse) error
   141  }
   142  
   143  type TaskExitedRequest struct{}
   144  type TaskExitedResponse struct{}
   145  
   146  type TaskExitedHook interface {
   147  	TaskHook
   148  
   149  	// Exited is called after a task exits and may or may not be restarted.
   150  	// Prestart may or may not have been called.
   151  	//
   152  	// The context is cancelled if the task is killed.
   153  	Exited(context.Context, *TaskExitedRequest, *TaskExitedResponse) error
   154  }
   155  
   156  type TaskUpdateRequest struct {
   157  	VaultToken string
   158  
   159  	NomadToken string
   160  
   161  	// Alloc is the current version of the allocation (may have been
   162  	// updated since the hook was created)
   163  	Alloc *structs.Allocation
   164  
   165  	// TaskEnv is the task's environment
   166  	TaskEnv *taskenv.TaskEnv
   167  }
   168  type TaskUpdateResponse struct{}
   169  
   170  type TaskUpdateHook interface {
   171  	TaskHook
   172  
   173  	// Update is called when the servers have updated the Allocation for
   174  	// this task. Updates are concurrent with all other task hooks and
   175  	// therefore hooks that implement this interface must be completely
   176  	// safe for concurrent access.
   177  	//
   178  	// The context is cancelled if the task is killed.
   179  	Update(context.Context, *TaskUpdateRequest, *TaskUpdateResponse) error
   180  }
   181  
   182  type TaskStopRequest struct {
   183  	// ExistingState is previously set hook data and should only be
   184  	// read. Stop hooks cannot alter state.
   185  	ExistingState map[string]string
   186  }
   187  
   188  type TaskStopResponse struct{}
   189  
   190  type TaskStopHook interface {
   191  	TaskHook
   192  
   193  	// Stop is called after the task has exited and will not be started
   194  	// again. It is the only hook guaranteed to be executed whenever
   195  	// TaskRunner.Run is called (and not gracefully shutting down).
   196  	// Therefore it may be called even when prestart and the other hooks
   197  	// have not.
   198  	//
   199  	// Stop hooks must be idempotent. The context is cancelled if the task
   200  	// is killed.
   201  	Stop(context.Context, *TaskStopRequest, *TaskStopResponse) error
   202  }