task

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2022 License: MIT Imports: 4 Imported by: 1

Documentation

Overview

Package task provides an alternative to errgroup package with builtin context cancellation support.

This packages uses go.uber.org/multierr to combine errors from failed tasks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CancelCondition added in v0.0.3

type CancelCondition func(err error) (cancel bool)

CancelCondition is a condition for canceling the task group.

func CancelOnError added in v0.0.3

func CancelOnError() CancelCondition

CancelOnError indicates that task group should return on non-nil error. This is the same behavior as the golang.org/x/sync/errgroup package.

func CancelOnReturn added in v0.0.3

func CancelOnReturn() CancelCondition

CancelOnReturn indicates that task group should be canceled if any subtask returns.

func NeverCancel added in v0.0.3

func NeverCancel() CancelCondition

NeverCancel indicates that task group should not be canceled until all subtasks complete.

type Executor added in v0.0.3

type Executor interface {
	Execute(ctx context.Context, cond CancelCondition, tasks ...Task) error
}

Executor abstracts the execution order for task groups.

func ParallelExecutor added in v0.0.3

func ParallelExecutor() Executor

ParallelExecutor returns an Executor instance for parallel execution.

func SequentialExecutor added in v0.0.3

func SequentialExecutor() Executor

SequentialExecutor returns an Executor instance for sequential execution.

type Task

type Task func(ctx context.Context) error

Task is a function that performs some work in foreground.

func Contextify

func Contextify(task func() error, shutdown func()) Task

Contextify adds context cancellation support to the task using the given shutdown function.

func Named

func Named(name string, run Task) Task

Named returns a task that returns an error prefixed with name on failure.

func Parallel added in v0.0.3

func Parallel(cond CancelCondition, tasks ...Task) Task

Parallel returns a function that executes a group of tasks in parallel using the given cancellation condition. The resulting Task returns combined errors for all failed subtasks.

func Sequential added in v0.0.3

func Sequential(cond CancelCondition, tasks ...Task) Task

Sequential returns a function that executes a group of tasks sequentially using the given cancellation condition. The resulting Task returns combined errors for all failed subtasks.

func (Task) Run

func (t Task) Run(ctx context.Context) error

Run executes the task.

Jump to

Keyboard shortcuts

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