concurrency

package
v21.11.1 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeyForTaskInContext taskContextKey = "task"
)

Variables

View Source
var (

	// FailEarly tells the TaskGroup to fail as soon as a child fails
	FailEarly = data.NewImmutableKeyValue("fail", "early")
	// FailLately tells the TaskGroup to end all children before determine if TaskGroup has failed
	FailLately = data.NewImmutableKeyValue("fail", "lately")
)
View Source
var (
	InheritParentIDOption = data.NewImmutableKeyValue(keywordInheritParentIDOption, true)
)

Functions

func AmendID

func AmendID(id string) data.ImmutableKeyValue

AmendID returns a data.ImmutableKeyValue containing string to add to task ID to be used as option on NewXXX functions that accept such options

func NewTaskGroup

func NewTaskGroup(options ...data.ImmutableKeyValue) (*taskGroup, fail.Error)

NewTaskGroup ...

func NewTaskGroupWithContext

func NewTaskGroupWithContext(ctx context.Context, options ...data.ImmutableKeyValue) (*taskGroup, fail.Error)

NewTaskGroupWithContext ...

func NewTaskGroupWithParent

func NewTaskGroupWithParent(parentTask Task, options ...data.ImmutableKeyValue) (*taskGroup, fail.Error)

NewTaskGroupWithParent ...

Types

type Task

type Task interface {
	TaskCore
	TaskGuard
}

Task is the interface of a task running in goroutine, allowing to identity (indirectly) goroutines

func NewTask

func NewTask() (Task, fail.Error)

NewTask creates a new instance of Task

func NewTaskWithContext

func NewTaskWithContext(ctx context.Context, options ...data.ImmutableKeyValue) (Task, fail.Error)

NewTaskWithContext creates an instance of Task with context

func NewTaskWithParent

func NewTaskWithParent(parentTask Task, options ...data.ImmutableKeyValue) (Task, fail.Error)

NewTaskWithParent creates a subtask Such a task can be aborted if the parent one can be

func NewUnbreakableTask

func NewUnbreakableTask() (Task, fail.Error)

NewUnbreakableTask is a new task that cannot be aborted by default (but this can be changed with IgnoreAbortSignal(false))

func RootTask

func RootTask() (rt Task, ferr fail.Error)

RootTask is the "task to rule them all"

func TaskFromContext

func TaskFromContext(ctx context.Context) (Task, fail.Error)

TaskFromContext extracts the task instance from context returns:

  • Task, nil: Task found in 'ctx'
  • nil, *fail.ErrNotAvailable: there is no Task value in 'ctx'
  • nil, *fail.ErrInconsistent: value stored as Task in "ctx' is not of type Task
  • nil, *ErrInvalidParameter: 'ctx' is nil

func TaskFromContextOrVoid added in v21.11.1

func TaskFromContextOrVoid(ctx context.Context) (Task, fail.Error)

TaskFromContextOrVoid extracts the task instance from context. If there is no task in the context, returns a VoidTask() returns:

  • Task, nil: Task found in 'ctx' or VoidTask() is returned

func VoidTask

func VoidTask() (Task, fail.Error)

VoidTask is a new task that do nothing

type TaskAction

type TaskAction func(t Task, parameters TaskParameters) (TaskResult, fail.Error)

TaskAction defines the type of the function that can be started by a Task. NOTE: you have to check if task is aborted inside this function using method t.ErrAborted(),

to be able to stop the process when task is aborted (no matter what
the abort reason is), and permit ending properly. Otherwise, this may lead to goroutine leak
(there is no good way to stop forcibly a goroutine).

Example: task.Start(func(task concurrency.Task, p TaskParameters) (concurrency.TaskResult, fail.Error) { ...

   for {
       if task.ErrAborted() {
           break // or return
       }
       ...
   }
   return nil
}, nil)

type TaskCore

type TaskCore interface {
	Abort() fail.Error
	AbortWithCause(fail.Error) fail.Error
	Abortable() (bool, fail.Error)
	Aborted() bool
	DisarmAbortSignal() func()
	ID() (string, fail.Error)
	Signature() string
	Status() (TaskStatus, fail.Error)
	Context() context.Context
	LastError() (error, fail.Error)
	Result() (TaskResult, fail.Error)
	SetID(string) fail.Error

	Run(fn TaskAction, params TaskParameters, options ...data.ImmutableKeyValue) (TaskResult, fail.Error)
	Start(fn TaskAction, params TaskParameters, options ...data.ImmutableKeyValue) (Task, fail.Error)
	StartWithTimeout(fn TaskAction, params TaskParameters, timeout time.Duration, options ...data.ImmutableKeyValue) (Task, fail.Error)
}

TaskCore is the interface of core methods to control Task and TaskGroup

type TaskGroup

type TaskGroup interface {
	TaskCore
	TaskGuard
	TaskGroupGuard

	GroupStatus() (map[TaskStatus][]string, fail.Error)
}

TaskGroup is the task group interface

type TaskGroupGuard

type TaskGroupGuard interface {
	Started() (uint, fail.Error)
	TryWaitGroup() (bool, TaskGroupResult, fail.Error)
	WaitGroup() (TaskGroupResult, fail.Error)
	WaitGroupFor(time.Duration) (bool, TaskGroupResult, fail.Error)
}

TaskGroupGuard is the task group interface defining method to wait the TaskGroup

type TaskGroupResult

type TaskGroupResult = map[string]TaskResult

TaskGroupResult is a map of the TaskResult of each task The index is the ID of the sub-Task running the action.

type TaskGuard

type TaskGuard interface {
	TryWait() (bool, TaskResult, fail.Error)
	Wait() (TaskResult, fail.Error)
	WaitFor(time.Duration) (bool, TaskResult, fail.Error)
}

TaskGuard ...

type TaskParameters

type TaskParameters interface{}

TaskParameters ...

type TaskResult

type TaskResult interface{}

TaskResult ...

type TaskStatus

type TaskStatus int

TaskStatus ...

const (
	UNKNOWN TaskStatus = iota // status is unknown
	READY                     // the task is ready to start
	RUNNING                   // the task is running
	DONE                      // the task has run and is done
	ABORTED                   // the task has been aborted
	TIMEOUT                   // the task ran out of time
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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