open-nomad/client/allocrunner/interfaces/task_lifecycle.go

151 lines
4.2 KiB
Go
Raw Normal View History

2018-06-22 00:35:07 +00:00
package interfaces
import (
2018-07-11 22:42:35 +00:00
"context"
"github.com/hashicorp/nomad/client/driver"
2018-06-27 23:57:31 +00:00
"github.com/hashicorp/nomad/client/driver/env"
cstructs "github.com/hashicorp/nomad/client/structs"
2018-06-22 00:35:07 +00:00
"github.com/hashicorp/nomad/nomad/structs"
)
/*
Restart
+--------------------------------------------------------+
| |
| *Update |
| +-------+ |
| | | |
| | | |
| +---v-------+----+ |
+----v----+ | Running | +----+-----+ +--------------+
| | *Prestart |----------------| *Exited | | *Stop | |
| Pending +-------------> *Poststart run +---^-----------> Exited +-----------> Terminal |
| | | upon entering | | | | NoRestart | |
+---------+ | running | | +----------+ +--------------+
| | |
+--------+-------+ |
| |
+-----------+
*Kill
(forces terminal)
Link: http://stable.ascii-flow.appspot.com/#Draw4489375405966393064/1824429135
2018-06-22 00:35:07 +00:00
*/
// TaskHook is a lifecycle hook into the life cycle of a task runner.
type TaskHook interface {
Name() string
}
2018-07-17 00:19:56 +00:00
type TaskPrestartRequest struct {
2018-06-22 00:35:07 +00:00
// HookData is previously set data by the hook
HookData map[string]string
// Task is the task to run
Task *structs.Task
// Vault token may optionally be set if a Vault token is available
VaultToken string
// TaskDir is the task's directory on the host
TaskDir string
2018-06-27 23:57:31 +00:00
// TaskEnv is the task's environment
TaskEnv *env.TaskEnv
2018-06-22 00:35:07 +00:00
}
2018-07-17 00:19:56 +00:00
type TaskPrestartResponse struct {
2018-06-22 00:35:07 +00:00
// Env is the environment variables to set for the task
Env map[string]string
// HookData allows the hook to emit data to be passed in the next time it is
// run
HookData map[string]string
// Done lets the hook indicate that it should only be run once
Done bool
2018-06-22 00:35:07 +00:00
}
2018-07-17 00:19:56 +00:00
type TaskPrestartHook interface {
2018-06-27 23:57:31 +00:00
TaskHook
2018-07-17 00:40:04 +00:00
// Prestart is called before the task is started.
2018-07-17 00:19:56 +00:00
Prestart(context.Context, *TaskPrestartRequest, *TaskPrestartResponse) error
2018-06-22 00:35:07 +00:00
}
// DriverStats is the interface implemented by DriverHandles to return task stats.
type DriverStats interface {
Stats() (*cstructs.TaskResourceUsage, error)
}
2018-07-17 00:19:56 +00:00
type TaskPoststartRequest struct {
// Exec hook (may be nil)
DriverExec driver.ScriptExecutor
// Network info (may be nil)
DriverNetwork *cstructs.DriverNetwork
// TaskEnv is the task's environment
TaskEnv *env.TaskEnv
// Stats collector
DriverStats DriverStats
2018-06-22 00:35:07 +00:00
}
2018-07-17 00:19:56 +00:00
type TaskPoststartResponse struct{}
2018-06-22 00:35:07 +00:00
2018-07-17 00:19:56 +00:00
type TaskPoststartHook interface {
2018-06-27 23:57:31 +00:00
TaskHook
2018-07-17 00:40:04 +00:00
// Poststart is called after the task has started.
2018-07-17 00:19:56 +00:00
Poststart(context.Context, *TaskPoststartRequest, *TaskPoststartResponse) error
2018-06-22 00:35:07 +00:00
}
2018-07-17 00:19:56 +00:00
type TaskKillRequest struct{}
type TaskKillResponse struct{}
2018-06-22 00:35:07 +00:00
2018-07-17 00:19:56 +00:00
type TaskKillHook interface {
2018-06-27 23:57:31 +00:00
TaskHook
2018-07-17 00:40:04 +00:00
// Kill is called when a task is going to be killed.
2018-07-17 00:19:56 +00:00
Kill(context.Context, *TaskKillRequest, *TaskKillResponse) error
2018-06-22 00:35:07 +00:00
}
2018-07-17 00:19:56 +00:00
type TaskExitedRequest struct{}
type TaskExitedResponse struct{}
2018-06-22 00:35:07 +00:00
2018-07-17 00:19:56 +00:00
type TaskExitedHook interface {
2018-06-27 23:57:31 +00:00
TaskHook
2018-07-17 00:40:04 +00:00
// Exited is called when a task exits and may or may not be restarted.
2018-07-17 00:19:56 +00:00
Exited(context.Context, *TaskExitedRequest, *TaskExitedResponse) error
2018-06-22 00:35:07 +00:00
}
type TaskUpdateRequest struct {
2018-07-12 23:15:33 +00:00
VaultToken string
// Alloc is the current version of the allocation (may have been
// updated since the hook was created)
Alloc *structs.Allocation
// TaskEnv is the task's environment
TaskEnv *env.TaskEnv
2018-06-22 00:35:07 +00:00
}
type TaskUpdateResponse struct{}
type TaskUpdateHook interface {
2018-06-27 23:57:31 +00:00
TaskHook
2018-07-12 23:15:33 +00:00
Update(context.Context, *TaskUpdateRequest, *TaskUpdateResponse) error
2018-06-22 00:35:07 +00:00
}
2018-07-17 00:19:56 +00:00
type TaskStopRequest struct{}
type TaskStopResponse struct{}
type TaskStopHook interface {
TaskHook
2018-07-17 00:40:04 +00:00
// Stop is called after the task has exited and will not be started again.
2018-07-17 00:19:56 +00:00
Stop(context.Context, *TaskStopRequest, *TaskStopResponse) error
}