taskbasket

package
v0.1.0-alpha.0...-0b1224c Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2019 License: Apache-2.0, Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package taskbasket is a persistent task manager.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec interface {
	Encode(Task) ([]byte, error)
	Decode([]byte) (Task, error)
}

Codec converts Tasks to and from byte slices.

type TB

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

TB is a taskbasket, a collection of abstract tasks that are persisted to a database. When the TB is running (see TB.Run) it launches each task in a goroutine that retries until the task succeeds, at which point it is removed from the database.

func New

func New(ctx context.Context, db *bolt.DB, bucket []byte, codec Codec) (*TB, error)

New creates a new taskbasket. It launches goroutines for any tasks already existing in the db.

func NewTx

func NewTx(ctx context.Context, tx *bolt.Tx, db *bolt.DB, bucket []byte, codec Codec) (*TB, error)

NewTx creates a new taskbasket in the context of an existing bolt Update transaction. It launches goroutines for any tasks already exiting in the db.

func (*TB) Add

func (tb *TB) Add(t Task) error

Add adds a task to the taskbasket. It is persisted to the database and then processed immediately. Note that if TB.Run has not been called, this function can block.

func (*TB) AddTx

func (tb *TB) AddTx(tx *bolt.Tx, t Task) error

AddTx adds a task to the taskbasket in the context of an existing bolt Update transaction. It is persisted to the database and processed when the transaction commits. Note that if TB.Run has not been called, this function can block.

func (*TB) Run

func (tb *TB) Run(ctx context.Context)

Run runs forever, processing the tasks in a taskbasket. When it starts, it reads all existing tasks from persistent storage and launches a goroutine for each. Thereafter it waits for new tasks to arrive via Add. It returns if its context is canceled.

type Task

type Task interface {
	// Run runs the task once.
	// It is called repeatedly by a running taskbasket,
	// with exponential backoff and jitter,
	// until it returns nil.
	Run(context.Context) error
}

Task is an item in a TB. The TB runs the task via its Run method. If that returns an error, it is retried after an interval.

Jump to

Keyboard shortcuts

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