tasks

package
v0.0.0-...-66c0959 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2017 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// StatePending - initial state of a task
	StatePending = "PENDING"
	// StateReceived - when task is received by a worker
	StateReceived = "RECEIVED"
	// StateStarted - when the worker starts processing the task
	StateStarted = "STARTED"
	// StateRetry - when failed task has been scheduled for retry
	StateRetry = "RETRY"
	// StateSuccess - when the task is processed successfully
	StateSuccess = "SUCCESS"
	// StateFailure - when processing of the task fails
	StateFailure = "FAILURE"
)

Variables

View Source
var (
	// ErrTaskMustBeFunc ...
	ErrTaskMustBeFunc = errors.New("Task must be a func type")
	// ErrTaskReturnsNoValue ...
	ErrTaskReturnsNoValue = errors.New("Taks must return at least a single value")
	// ErrLastReturnValueMustBeError ..
	ErrLastReturnValueMustBeError = errors.New("Last return value of a task must be error")
)
View Source
var ErrTaskPanicked = errors.New("Invoking task caused a panic")

ErrTaskPanicked ...

Functions

func IsContextType

func IsContextType(t reflect.Type) bool

IsContextType checks to see if the type is a context.Context

func ReflectValue

func ReflectValue(valueType string, value interface{}) (reflect.Value, error)

ReflectValue converts interface{} to reflect.Value based on string type

func ValidateTask

func ValidateTask(task interface{}) error

ValidateTask validates task function using reflection and makes sure it has a proper signature. Functions used as tasks must return at least a single value and the last return type must be error

Types

type Arg

type Arg struct {
	Type  string
	Value interface{}
}

Arg represents a single argument passed to invocation fo a task

type Chain

type Chain struct {
	Tasks []*Signature
}

Chain creates a chain of tasks to be executed one after another

func NewChain

func NewChain(signatures ...*Signature) *Chain

NewChain creates a new chain of tasks to be processed one by one, passing results unless task signatures are set to be immutable

type Chord

type Chord struct {
	Group    *Group
	Callback *Signature
}

Chord adds an optional callback to the group to be executed after all tasks in the group finished

func NewChord

func NewChord(group *Group, callback *Signature) *Chord

NewChord creates a new chord (a group of tasks with a single callback to be executed after all tasks in the group has completed)

type ErrUnsupportedType

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

ErrUnsupportedType ...

func NewErrUnsupportedType

func NewErrUnsupportedType(valueType string) ErrUnsupportedType

NewErrUnsupportedType returns new ErrUnsupportedType

func (ErrUnsupportedType) Error

func (e ErrUnsupportedType) Error() string

Error method so we implement the error interface

type Group

type Group struct {
	GroupUUID string
	Tasks     []*Signature
}

Group creates a set of tasks to be executed in parallel

func NewGroup

func NewGroup(signatures ...*Signature) *Group

NewGroup creates a new group of tasks to be processed in parallel

func (*Group) GetUUIDs

func (group *Group) GetUUIDs() []string

GetUUIDs returns slice of task UUIDS

type GroupMeta

type GroupMeta struct {
	GroupUUID      string   `bson:"_id"`
	TaskUUIDs      []string `bson:"task_uuids"`
	ChordTriggered bool     `bson:"chord_trigerred"`
	Lock           bool     `bson:"lock"`
}

GroupMeta stores useful metadata about tasks within the same group E.g. UUIDs of all tasks which are used in order to check if all tasks completed successfully or not and thus whether to trigger chord callback

type Headers

type Headers map[string]interface{}

Headers represents the headers which should be used to direct the task

type Signature

type Signature struct {
	UUID           string
	Name           string
	RoutingKey     string
	ETA            *time.Time
	GroupUUID      string
	GroupTaskCount int
	Args           []Arg
	Headers        Headers
	Immutable      bool
	RetryCount     int
	RetryTimeout   int
	OnSuccess      []*Signature
	OnError        []*Signature
	ChordCallback  *Signature
}

Signature represents a single task invocation

func NewSignature

func NewSignature(name string, args []Arg) *Signature

NewSignature creates a new task signature

type Task

type Task struct {
	TaskFunc   reflect.Value
	UseContext bool
	Args       []reflect.Value
}

Task wraps a signature and methods used to reflect task arguments and return values after invoking the task

func New

func New(taskFunc interface{}, args []Arg) (*Task, error)

New tries to use reflection to convert the function and arguments into a reflect.Value and prepare it for invocation

func (*Task) Call

func (t *Task) Call() (taskResults []*TaskResult, err error)

Call attempts to call the task with the supplied arguments.

`err` is set in the return value in two cases:

  1. The reflected function invocation panics (e.g. due to a mismatched argument list).
  2. The task func itself returns a non-nil error.

func (*Task) ReflectArgs

func (t *Task) ReflectArgs(args []Arg) error

ReflectArgs converts []TaskArg to []reflect.Value

type TaskResult

type TaskResult struct {
	Type  string      `bson:"type"`
	Value interface{} `bson:"value"`
}

TaskResult represents an actual return value of a processed task

type TaskState

type TaskState struct {
	TaskUUID string        `bson:"_id"`
	State    string        `bson:"state"`
	Results  []*TaskResult `bson:"results"`
	Error    string        `bson:"error"`
}

TaskState represents a state of a task

func NewFailureTaskState

func NewFailureTaskState(signature *Signature, err string) *TaskState

NewFailureTaskState ...

func NewPendingTaskState

func NewPendingTaskState(signature *Signature) *TaskState

NewPendingTaskState ...

func NewReceivedTaskState

func NewReceivedTaskState(signature *Signature) *TaskState

NewReceivedTaskState ...

func NewRetryTaskState

func NewRetryTaskState(signature *Signature) *TaskState

NewRetryTaskState ...

func NewStartedTaskState

func NewStartedTaskState(signature *Signature) *TaskState

NewStartedTaskState ...

func NewSuccessTaskState

func NewSuccessTaskState(signature *Signature, results []*TaskResult) *TaskState

NewSuccessTaskState ...

func (*TaskState) IsCompleted

func (taskState *TaskState) IsCompleted() bool

IsCompleted returns true if state is SUCCESS or FAILURE, i.e. the task has finished processing and either succeeded or failed.

func (*TaskState) IsFailure

func (taskState *TaskState) IsFailure() bool

IsFailure returns true if state is FAILURE

func (*TaskState) IsSuccess

func (taskState *TaskState) IsSuccess() bool

IsSuccess returns true if state is SUCCESS

Jump to

Keyboard shortcuts

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