worker

package
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 31, 2019 License: Apache-2.0 Imports: 14 Imported by: 8

Documentation

Overview

Package worker is a set of helpers for implementing common features of robot workers (see package job for more information about what a worker is). It is not required to use this package, but it helps for the simple cases.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EquivalentAction

func EquivalentAction(a, b Action) bool

EquivalentAction returns true if an action is the same task being performed on the same devices.

func NeedsRetry added in v0.6.0

func NeedsRetry(reason string, retryStrings ...string) error

NeedsRetry is used to check if a string signals a retry attempt in a task. "text file busy" is the main cause for retry attempts, but if extra checks are necessary they can be passed into they variadic argument. This is the idiomatic method of creating a TaskRetryError and if no need for a retry exists it returns nil.

func RetryFunction added in v0.6.0

func RetryFunction(ctx context.Context, maxAttempts int, delay time.Duration, task func() error) error

RetryFunction wraps a function that can return TaskRetryError and handles performing a delay and retrying up to maxAttempts

Types

type Action

type Action interface {
	proto.Message
	// Init is called as you add input and target device pairs to the manager.
	// It is invoked with the action id to use, the input the manager was given, and
	// the worker that the task is going to be posted to.
	Init(id string, input Input, w *job.Worker)
	// JobID must return the id that was handed to Init.
	JobID() string
	// JobHost must return the host of the worker that was handed to Init.
	JobHost() string
	// JobTarget must return the target of the worker that was handed to Init.
	JobTarget() string
	// JobInput must return the input that was handed to Init.
	JobInput() Input
}

Action is the interface to the specific action type for a given of the Actions store.

type Actions

type Actions struct {
	// contains filtered or unexported fields
}

Actions is a struct to manage a persistent set of actions.

func (*Actions) ByID

func (a *Actions) ByID(ctx context.Context, id string) Action

ByID returns the action with the specified id

func (*Actions) ByInput

func (a *Actions) ByInput(ctx context.Context, input proto.Message) Action

ByInput finds the action that matches the given input

func (*Actions) Search

func (a *Actions) Search(ctx context.Context, query *search.Query, handler interface{}) error

Search runs the query for each entry in the action list, and hands the matches to the action handler.

type Input

type Input interface {
	proto.Message
}

Input is the interface that must be implemented by anything that wants to be the input to an action.

type Manager

type Manager struct {
	//Actions holds the persisted list of actions the service has had added to it.
	Actions Actions
	// Workers holds the dynamic list of live workers for the service.
	Workers Workers
}

Manager is a helper for writing local manager implementation of worker types (such as trace and replay). It has all the code needed to manage and persist the list of workers and actions those workers are performing.

func (*Manager) Do

func (m *Manager) Do(ctx context.Context, device string, input Input) (string, error)

Do is called to add a new action to the service. It is handed the id of the target device to run it on, and the inputs to the action. The active worker that owns the target device will be looked up, and then a new action created for the target worker and provided input will be added to the store.

func (*Manager) Init

func (m *Manager) Init(ctx context.Context, library record.Library, jobs job.Manager, op job.Operation, nullAction Action, nullTask Task) error

Init is called to initialised the action store from the supplied library. It loads the action ledger and prepares the empty worker list. The op is the type of action this manager supports, the nullAction is the empty version of the action the manager holds, and the nullTask is the empty version of the task sent to the managers workers.

func (*Manager) Update

func (m *Manager) Update(ctx context.Context, action Action) error

Update is used to update an existing action, normally to update it status and/or set it's outputs.

type Task

type Task interface {
	proto.Message
	// Init is called as you add input and target device pairs to the manager.
	// It is invoked with the action id to post results to, the input the manager was given, and
	// the worker that the task is going to be posted to.
	Init(id string, input Input, w *job.Worker)
}

Task is the interface to something that can be sent to a worker. It should always contain the inputs to the action, and the id of the action to attach results to.

type TaskRetryError added in v0.6.0

type TaskRetryError struct {
	Reason string
}

TaskRetryError is used to communicate that an action Failed in such a way that the task should attempt a retry. It simply contains the reason for the retry.

func (TaskRetryError) Error added in v0.6.0

func (e TaskRetryError) Error() string

Error returns the reason for the retry

type Worker

type Worker struct {

	// Info is the persistable information about a worker.
	Info *job.Worker
	// contains filtered or unexported fields
}

Worker is the representation of a live object that can perform actions.

type Workers

type Workers struct {
	// contains filtered or unexported fields
}

Workers is the non persisted list of live workers.

func (*Workers) Find

func (w *Workers) Find(ctx context.Context, device string) *Worker

Find searches the live worker set to find the one that is managing a given target device.

func (*Workers) Register

func (w *Workers) Register(ctx context.Context, host *device.Instance, target *device.Instance, handler interface{}) error

Register is called to add a new worker to the active set. It takes the host and target device for the worker, which may be the same, and a handler that will be passed all the task objects that are sent to the worker.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL