task

package
v0.0.0-...-a216e23 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2016 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Overview

Package task provides a task system to handle synchronous tasks in a asynchronous manner.

Index

Constants

View Source
const (
	// ContextTaskID is the key for the current task-id stored in the context.Context when executing tasks.
	ContextTaskID = "task-id"
)

Variables

This section is empty.

Functions

func HasFailedStatus

func HasFailedStatus(taskObject *Task) bool

HasFailedStatus determines whether a task has failed or not. Note that this is about a final status.

func HasFinalStatus

func HasFinalStatus(taskObject *Task) bool

HasFinalStatus determines whether a task has a final status or not.

func HasSucceededStatus

func HasSucceededStatus(taskObject *Task) bool

HasSucceededStatus determines whether a task has succeeded or not. Note that this is about a final status.

func IsTaskObjectNotFound

func IsTaskObjectNotFound(err error) bool

IsTaskObjectNotFound checks whether the given error indicates the problem of an task object not being found or not. In case you want to lookup a task object that cannot be found in the underlying storage, an error that you can identify using this method is returned.

Types

type Action

type Action func(ctx context.Context) error

Action represents any work to be done when executing a task.

type ActiveStatus

type ActiveStatus string

ActiveStatus represents a status indicating activation or deactivation.

const (
	// StatusStarted represents a running task
	StatusStarted ActiveStatus = "started"
	// StatusStopped represents a stopped task, that has not been started yet
	StatusStopped ActiveStatus = "stopped"
)

type Config

type Config struct {
	Storage Storage

	// WaitSleep represents the time to sleep between state-check cycles.
	WaitSleep time.Duration

	// Logger provides an initialised logger.
	Logger logging.Logger
}

Config represents the configurations for the task service that is going to be created.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a best effort default configuration for the task service.

type FinalStatus

type FinalStatus string

FinalStatus represents any status that is final. A task having this status will not change its status anymore.

const (
	// StatusFailed represents a task where the action return an error.
	StatusFailed FinalStatus = "failed"
	// StatusSucceeded represents a task where the action returned nil.
	StatusSucceeded FinalStatus = "succeeded"
)

type Service

type Service interface {
	// Create creates a new task object configured with the given action. The
	// task object is immediately returned and its corresponding action is
	// executed asynchronously.
	Create(ctx context.Context, action Action) (*Task, error)

	// FetchState fetches and returns the current state and status for the given
	// task ID.
	FetchState(ctx context.Context, taskID string) (*Task, error)

	// MarkAsSucceeded marks the task object as succeeded and persists its state.
	// The returned task object is actually the refreshed version of the provided
	// one.
	MarkAsSucceeded(ctx context.Context, taskObject *Task) (*Task, error)

	// MarkAsFailedWithError marks the task object as failed, adds information of
	// thegiven error and persists the task objects's state. The returned task
	// object is actually the refreshed version of the provided one.
	MarkAsFailedWithError(ctx context.Context, taskObject *Task, err error) (*Task, error)

	// PersistState writes the given task object to the configured Storage.
	PersistState(ctx context.Context, taskObject *Task) error

	// WaitForFinalStatus blocks and waits for the given task to reach a final
	// status. The given closer can end the waiting and thus stop blocking the
	// call to WaitForFinalStatus.
	WaitForFinalStatus(ctx context.Context, taskID string, closer <-chan struct{}) (*Task, error)
}

Service represents a task managing unit being able to act on task objects.

func NewTaskService

func NewTaskService(config Config) Service

NewTaskService returns a new configured task service instance.

type Storage

type Storage interface {
	// Get fetches the corresponding task object for the given task ID.
	Get(taskID string) (*Task, error)

	// Set persists the given task object for its corresponding task ID.
	Set(taskObject *Task) error
}

Storage represents some storage solution to persist task objects.

func NewMemoryStorage

func NewMemoryStorage() Storage

NewMemoryStorage creates a backend implementation for pseudo in-memory persistence.

type Task

type Task struct {
	// ActiveStatus represents a status indicating activation or deactivation.
	ActiveStatus ActiveStatus

	// Error represents the message of an error occurred during task execution, if
	// any.
	Error error

	// FinalStatus represents any status that is final. A task having this status
	// will not change its status anymore.
	FinalStatus FinalStatus

	// ID represents the task identifier.
	ID string
}

Task represents a task that is executable.

Jump to

Keyboard shortcuts

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