boom

package module
v0.0.0-...-cdebdc2 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2018 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

boom

GoDoc Build Status Code Coverage Go Report Card

Boom is a Go library for easily handling tasks and the results of those tasks.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrTimeout is returned when a function's execution times out
	ErrTimeout = errors.New("Execution timed out")

	// ErrExecuting is returned when a task is requested to start but is
	// already started
	ErrExecuting = errors.New("Execution has already started")

	// ErrNotExecuting is returned when a task has not started executing yet
	ErrNotExecuting = errors.New("Task has not started executing yet")

	// ErrFinished is returned when execution for tasks has already finished
	ErrFinished = errors.New("Execution has already finished")
)

Functions

This section is empty.

Types

type AsyncCollector

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

AsyncCollector can take a number of tasks, execute them in parallel, and then collect the results of those tasks once all have been completed.

func NewAsyncCollector

func NewAsyncCollector(configs ...TaskConfig) *AsyncCollector

NewAsyncCollector creates a new AsyncCollector instance

func (*AsyncCollector) Run

func (c *AsyncCollector) Run(f TaskFunc, args ...interface{})

Run takes a TaskFunc to execute and zero or more parameters to pass to that function and immediately starts executing the function.

func (*AsyncCollector) RunWithContext

func (c *AsyncCollector) RunWithContext(ctx context.Context, f TaskFunc, args ...interface{})

RunWithContext calls Run and uses the provided context.Context to run the task.

func (*AsyncCollector) Wait

func (c *AsyncCollector) Wait(timeout time.Duration) ([]TaskResult, error)

Wait will wait until all tasks associated with the Collector have finished and then will return the results of those functions. Wait will wait for results until it has not received a task result in 'timeout' amount of time. If timeout is 0, Wait will wait indefinitely for tasks to finish.

func (*AsyncCollector) WaitCloser

func (c *AsyncCollector) WaitCloser(timeout time.Duration, closer CollectorCloser) ([]TaskResult, error)

WaitCloser will wait similar to Wait except if an error occurs while waiting for tasks to finish, closer will be called on each task result as they finish.

type Collector

type Collector interface {
	Run(TaskFunc, ...interface{})
	RunWithContext(context.Context, TaskFunc, ...interface{})
	Wait(time.Duration) ([]TaskResult, error)
	WaitCloser(time.Duration, CollectorCloser) ([]TaskResult, error)
}

type CollectorCloser

type CollectorCloser func(result TaskResult)

CollectorCloser is the signature for the function a Collector runs when WaitCloser is called and an error occurs in the Collector.

type ErrorResult

type ErrorResult struct {
	Error error
}

func NewErrorResult

func NewErrorResult(err error) *ErrorResult

func (*ErrorResult) Err

func (r *ErrorResult) Err() error

type Task

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

Task represents a task being executed within boom

Example
// Create a new task but don't start execution right away
t := newTask(context.Background(), newTaskConfig(), func(task *Task, args ...interface{}) TaskResult {
	// Run the task until something requests that we stop
	<-task.Stopping()
	return NewValueResult(args, nil)
}, "first", "second", 3)

// Start the Task. Another way to do this in a single command is to use
// runTask(newTaskConfig(), ) to create a new task and start it right away
t.Start()

// Let the task run a little bit
time.Sleep(100 * time.Millisecond)

// Ask the task to stop
t.Stop()

// Wait forever for the task to finish running and get
// the result from it
res, _ := t.Wait(0)

valRes := res.(*ValueResult)

fmt.Printf("Value: %+v\n", valRes.Value)
fmt.Printf("Error: %+v\n", valRes.Error)
Output:
Value: [first second 3]
Error: <nil>

func (*Task) Context

func (t *Task) Context() context.Context

Context returns the context for the task

func (*Task) Discard

func (t *Task) Discard()

Discard will discard the result returned from the task. Either Discard or Wait must be called or the task's goroutine will leak.

func (*Task) Finished

func (t *Task) Finished() <-chan struct{}

Finished returns a channel that will be closed if the task has finished running.

func (*Task) Running

func (t *Task) Running() <-chan struct{}

Running returns a channel that is closed if the task has been set to the 'Running' state at some point. See SetRunning for how to set the value.

func (*Task) SetRunning

func (t *Task) SetRunning(running bool)

SetRunning is a utility provided to users to signal whether a task is actively running or not. Typically this would be used within the task function itself to signal that it has completed any setup it needed to do and has started processing data.

func (*Task) Start

func (t *Task) Start() error

Start will begin execution of the task in a separate goroutine.

func (*Task) StartSync

func (t *Task) StartSync() (TaskResult, error)

StartSync will run a task synchronously

func (*Task) Started

func (t *Task) Started() <-chan struct{}

Started returns a channel that is closed if the task has been started.

func (*Task) Stop

func (t *Task) Stop() error

Stop signals a started task to stop. It is up to the task itself to check Task.Stopping() to see if it should stop.

func (*Task) StopAndWait

func (t *Task) StopAndWait(timeout time.Duration) (TaskResult, error)

StopAndWait is a convenience function for calling both Stop() and Wait() in a single call.

func (*Task) Stopping

func (t *Task) Stopping() <-chan struct{}

Stopping returns a channel that is closed if a task is stopping (set by the Stop method)

func (*Task) Wait

func (t *Task) Wait(timeout time.Duration) (TaskResult, error)

Wait will wait for a task to end and return the TaskResult from the task over the returned channel. If a non-zero timeout is provided, Wait will wait until the timeout duration and close the channel. Either Discard or Wait must be called or the task's goroutine will leak.

func (*Task) WaitForRunning

func (t *Task) WaitForRunning(timeout time.Duration) error

WaitForRunning will block until a task enters the 'Running' state or will return immediately if the task is already in the 'Running' state.

type TaskConfig

type TaskConfig func(*taskConfig)

func WithClock

func WithClock(clock glock.Clock) TaskConfig

type TaskFunc

type TaskFunc func(task *Task, data ...interface{}) TaskResult

TaskFunc is the signature for the function executed to perform a Task

type TaskResult

type TaskResult interface {
	Err() error
}

TaskResult is the result expected from a boom task when it's finished executing

type TaskRunner

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

TaskRunner is a way to start one-off tasks where the collection of results does not matter, only each individual task.

func NewTaskRunner

func NewTaskRunner(configs ...TaskConfig) *TaskRunner

NewTaskRunner creates a new TaskRunner instance.

func (*TaskRunner) New

func (tr *TaskRunner) New(f TaskFunc, args ...interface{}) *Task

New creates a new task with the given function and arguments

func (*TaskRunner) NewWithContext

func (tr *TaskRunner) NewWithContext(ctx context.Context, f TaskFunc, args ...interface{}) *Task

NewWithContext creates a new task with the given context, function and arguments

func (*TaskRunner) Run

func (tr *TaskRunner) Run(f TaskFunc, args ...interface{}) *Task

Run will create a new task and immediately call Start to begin execution of the task.

func (*TaskRunner) RunWithContext

func (tr *TaskRunner) RunWithContext(ctx context.Context, f TaskFunc, args ...interface{}) *Task

RunWithContext calls Run using the provided context.Context for the task

type ValueResult

type ValueResult struct {
	Value interface{}
	Error error
}

func NewValueResult

func NewValueResult(val interface{}, err error) *ValueResult

NewValueResult is a convenience function for creating a ValueResult

func (*ValueResult) Err

func (r *ValueResult) Err() error

Jump to

Keyboard shortcuts

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