Documentation ¶
Overview ¶
Package state implements the representation of system state.
Index ¶
- Constants
- Variables
- func MockTime(now time.Time) (restore func())
- type Backend
- type Change
- func (c *Change) Abort()
- func (c *Change) AddAll(ts *TaskSet)
- func (c *Change) AddTask(t *Task)
- func (c *Change) Err() error
- func (c *Change) Get(key string, value interface{}) error
- func (c *Change) ID() string
- func (c *Change) Kind() string
- func (c *Change) MarshalJSON() ([]byte, error)
- func (c *Change) Ready() <-chan struct{}
- func (c *Change) ReadyTime() time.Time
- func (c *Change) Set(key string, value interface{})
- func (c *Change) SetStatus(s Status)
- func (c *Change) SpawnTime() time.Time
- func (c *Change) State() *State
- func (c *Change) Status() Status
- func (c *Change) Summary() string
- func (c *Change) Tasks() []*Task
- func (c *Change) UnmarshalJSON(data []byte) error
- type HandlerFunc
- type Retry
- type State
- func (s *State) Cache(key, value interface{})
- func (s *State) Cached(key interface{}) interface{}
- func (s *State) Change(id string) *Change
- func (s *State) Changes() []*Change
- func (s *State) EnsureBefore(d time.Duration)
- func (s *State) Get(key string, value interface{}) error
- func (s *State) Lock()
- func (s *State) MarshalJSON() ([]byte, error)
- func (s *State) Modified() bool
- func (s *State) NewChange(kind, summary string) *Change
- func (s *State) NewTask(kind, summary string) *Task
- func (s *State) NumTask() int
- func (s *State) Prune(pruneWait, abortWait time.Duration)
- func (s *State) RequestRestart()
- func (s *State) Set(key string, value interface{})
- func (s *State) Task(id string) *Task
- func (s *State) Tasks() []*Task
- func (s *State) Unlock()
- func (s *State) UnmarshalJSON(data []byte) error
- type Status
- type Task
- func (t *Task) At(when time.Time)
- func (t *Task) AtTime() time.Time
- func (t *Task) Change() *Change
- func (t *Task) Errorf(format string, args ...interface{})
- func (t *Task) Get(key string, value interface{}) error
- func (t *Task) HaltTasks() []*Task
- func (t *Task) ID() string
- func (t *Task) Kind() string
- func (t *Task) Log() []string
- func (t *Task) Logf(format string, args ...interface{})
- func (t *Task) MarshalJSON() ([]byte, error)
- func (t *Task) Progress() (done, total int)
- func (t *Task) ReadyTime() time.Time
- func (t *Task) Set(key string, value interface{})
- func (t *Task) SetProgress(done, total int)
- func (t *Task) SetStatus(new Status)
- func (t *Task) SpawnTime() time.Time
- func (t *Task) State() *State
- func (t *Task) Status() Status
- func (t *Task) Summary() string
- func (t *Task) UnmarshalJSON(data []byte) error
- func (t *Task) WaitAll(ts *TaskSet)
- func (t *Task) WaitFor(another *Task)
- func (t *Task) WaitTasks() []*Task
- type TaskRunner
- type TaskSet
Constants ¶
const ( // Messages logged in tasks are guaranteed to use the time formatted // per RFC3339 plus the following strings as a prefix, so these may // be handled programatically and parsed or stripped for presentation. LogInfo = "INFO" LogError = "ERROR" )
Variables ¶
var ErrNoState = errors.New("no state entry for key")
ErrNoState represents the case of no state entry for a given key.
Functions ¶
Types ¶
type Backend ¶
type Backend interface { Checkpoint(data []byte) error EnsureBefore(d time.Duration) // TODO: take flags to ask for reboot vs restart? RequestRestart() }
A Backend is used by State to checkpoint on every unlock operation and to mediate requests to ensure the state sooner or request restarts.
type Change ¶
type Change struct {
// contains filtered or unexported fields
}
Change represents a tracked modification to the system state.
The Change provides both the justification for individual tasks to be performed and the grouping of them.
As an example, if an administrator requests an interface connection, multiple hooks might be individually run to accomplish the task. The Change summary would reflect the request for an interface connection, while the individual Task values would track the running of the hooks themselves.
func (*Change) Abort ¶
func (c *Change) Abort()
Abort flags the change for cancellation, whether in progress or not. Cancellation will proceed at the next ensure pass.
func (*Change) AddAll ¶
AddAll registers all tasks in the set as required for the state change to be accomplished.
func (*Change) AddTask ¶
AddTask registers a task as required for the state change to be accomplished.
func (*Change) Err ¶
Err returns an error value based on errors that were logged for tasks registered in this change, or nil if the change is not in ErrorStatus.
func (*Change) Get ¶
Get unmarshals the stored value associated with the provided key into the value parameter.
func (*Change) MarshalJSON ¶
MarshalJSON makes Change a json.Marshaller
func (*Change) Ready ¶
func (c *Change) Ready() <-chan struct{}
Ready returns a channel that is closed the first time the change becomes ready.
func (*Change) Set ¶
Set associates value with key for future consulting by managers. The provided value must properly marshal and unmarshal with encoding/json.
func (*Change) SetStatus ¶
SetStatus sets the change status, overriding the default behavior (see Status method).
func (*Change) Status ¶
Status returns the current status of the change. If the status was not explicitly set the result is derived from the status of the individual tasks related to the change, according to the following decision sequence:
- With at least one task in DoStatus, return DoStatus
- With at least one task in ErrorStatus, return ErrorStatus
- Otherwise, return DoneStatus
func (*Change) UnmarshalJSON ¶
UnmarshalJSON makes Change a json.Unmarshaller
type HandlerFunc ¶
HandlerFunc is the type of function for the handlers
type Retry ¶
Retry is returned from a handler to signal that is ok to rerun the task at a later point. It's to be used also when a task goroutine is asked to stop through its tomb. After can be used to indicate how much to postpone the retry, 0 (the default) means at the next ensure pass and is what should be used if stopped through its tomb.
type State ¶
type State struct {
// contains filtered or unexported fields
}
State represents an evolving system state that persists across restarts.
The State is concurrency-safe, and all reads and writes to it must be performed with the state locked. It's a runtime error (panic) to perform operations without it.
The state is persisted on every unlock operation via the StateBackend it was initialized with.
func (*State) Cache ¶
func (s *State) Cache(key, value interface{})
Cache associates value with key for future consulting by managers. The cached value is not persisted.
func (*State) Cached ¶
func (s *State) Cached(key interface{}) interface{}
Cached returns the cached value associated with the provided key. It returns nil if there is no entry for key.
func (*State) EnsureBefore ¶
EnsureBefore asks for an ensure pass to happen sooner within duration from now.
func (*State) Get ¶
Get unmarshals the stored value associated with the provided key into the value parameter. It returns ErrNoState if there is no entry for key.
func (*State) MarshalJSON ¶
MarshalJSON makes State a json.Marshaller
func (*State) NewTask ¶
NewTask creates a new task. It usually will be registered with a Change using AddTask or through a TaskSet.
func (*State) NumTask ¶
NumTask returns the number of tasks that currently exist in the state (both linked or not yet linked to changes), useful for sanity checking.
func (*State) Prune ¶
Prune removes changes that became ready for more than pruneWait and aborts tasks spawned for more than abortWait. It also removes tasks unlinked to changes after pruneWait.
func (*State) RequestRestart ¶
func (s *State) RequestRestart()
RequestRestart asks for a restart of the managing process.
func (*State) Set ¶
Set associates value with key for future consulting by managers. The provided value must properly marshal and unmarshal with encoding/json.
func (*State) Task ¶
Task returns the task for the given ID if the task has been linked to a change.
func (*State) Unlock ¶
func (s *State) Unlock()
Unlock releases the state lock and checkpoints the state. It does not return until the state is correctly checkpointed. After too many unsuccessful checkpoint attempts, it panics.
func (*State) UnmarshalJSON ¶
UnmarshalJSON makes State a json.Unmarshaller
type Status ¶
type Status int
Status is used for status values for changes and tasks.
const ( // DefaultStatus is the standard computed status for a change or task. // For tasks it's always mapped to DoStatus, and for change its mapped // to an aggregation of its tasks' statuses. See Change.Status for details. DefaultStatus Status = 0 // HoldStatus means the task should not run, perhaps as a consequence of an error on another task. HoldStatus Status = 1 // DoStatus means the change or task is ready to start. DoStatus Status = 2 // DoingStatus means the change or task is running or an attempt was made to run it. DoingStatus Status = 3 // DoneStatus means the change or task was accomplished successfully. DoneStatus Status = 4 // AbortStatus means the task should stop doing its activities and then undo. AbortStatus Status = 5 // UndoStatus means the change or task should be undone, probably due to an error elsewhere. UndoStatus Status = 6 // UndoingStatus means the change or task is being undone or an attempt was made to undo it. UndoingStatus Status = 7 // UndoneStatus means a task was first done and then undone after an error elsewhere. // Changes go directly into the error status instead of being marked as undone. UndoneStatus Status = 8 // ErrorStatus means the change or task has errored out while running or being undone. ErrorStatus Status = 9 )
Admitted status values for changes and tasks.
type Task ¶
type Task struct {
// contains filtered or unexported fields
}
Task represents an individual operation to be performed for accomplishing one or more state changes.
See Change for more details.
func (*Task) At ¶
At schedules the task, if it's not ready, to happen no earlier than when, if when is the zero time any previous special scheduling is supressed.
func (*Task) AtTime ¶
AtTime returns the time at which the task is scheduled to run. A zero time means no special schedule, i.e. run as soon as prerequisites are met.
func (*Task) Get ¶
Get unmarshals the stored value associated with the provided key into the value parameter.
func (*Task) Log ¶
Log returns the most recent messages logged into the task.
Only the most recent entries logged are returned, potentially with different behavior for different task statuses. How many entries are returned is an implementation detail and may change over time.
Messages are prefixed with one of the known message kinds. See details about LogInfo and LogError.
The returned slice should not be read from without the state lock held, and should not be written to.
func (*Task) MarshalJSON ¶
MarshalJSON makes Task a json.Marshaller
func (*Task) Progress ¶
Progress returns the current progress for the task. If progress is not explicitly set, it returns (0, 1) if the status is DoStatus and (1, 1) otherwise.
func (*Task) Set ¶
Set associates value with key for future consulting by managers. The provided value must properly marshal and unmarshal with encoding/json.
func (*Task) SetProgress ¶
SetProgress sets the task progress to cur out of total steps.
func (*Task) SetStatus ¶
SetStatus sets the task status, overriding the default behavior (see Status method).
func (*Task) UnmarshalJSON ¶
UnmarshalJSON makes Task a json.Unmarshaller
func (*Task) WaitAll ¶
WaitAll registers all the tasks in the set as a requirement for t to make progress.
type TaskRunner ¶
type TaskRunner struct {
// contains filtered or unexported fields
}
TaskRunner controls the running of goroutines to execute known task kinds.
func NewTaskRunner ¶
func NewTaskRunner(s *State) *TaskRunner
NewTaskRunner creates a new TaskRunner
func (*TaskRunner) AddHandler ¶
func (r *TaskRunner) AddHandler(kind string, do, undo HandlerFunc)
AddHandler registers the functions to concurrently call for doing and undoing tasks of the given kind. The undo handler may be nil.
func (*TaskRunner) Ensure ¶
func (r *TaskRunner) Ensure()
Ensure starts new goroutines for all known tasks with no pending dependencies. Note that Ensure will lock the state.
func (*TaskRunner) Stop ¶
func (r *TaskRunner) Stop()
Stop kills all concurrent activities and returns after that's done.
func (*TaskRunner) Wait ¶
func (r *TaskRunner) Wait()
Wait waits for all concurrent activities and returns after that's done.
type TaskSet ¶
type TaskSet struct {
// contains filtered or unexported fields
}
A TaskSet holds a set of tasks.
func NewTaskSet ¶
NewTaskSet returns a new TaskSet comprising the given tasks.