jobs

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2021 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package jobs provides a set of task grouping tools for running sequences of tasks, halting on error, and displaying their results.

The four main entrypoints are Run, RunContext, Parallel, and ParallelContext. They accept implementations of the Job interface, which you can either implement in your own task types, or simply cast functions to the JobFunc type. They return Events, a synchronous bus of events that, for each job, always occur in the following order:

*EventQueued
*EventStarted
*EventProgressed
*EventFinished

Additionally, if a job spawns any sub-jobs, their events will arrive between *EventStarted and *EventFinished.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	context.Context
	// contains filtered or unexported fields
}

Context is passed as the argument to each job's Run function. It implements context.Context, and can be used to cancel jobs where possible.

It also allows jobs to spawn sub-jobs using its Run, RunContext, Parallel, and ParallelContext functions, which return their own Events busses. Events from sub-jobs are sent first to this bus, and then to the bus of the parent job.

func (*Context) Parallel

func (c *Context) Parallel(concurrency int, jobs ...Job) (events Events)

Parallel runs the given jobs using a queue and the number of worker specified by concurrency. Errors do not affect continuation.

func (*Context) ParallelContext

func (c *Context) ParallelContext(ctx context.Context, concurrency int, jobs ...Job) (events Events)

ParallelContext is identical to Parallel, but accepts a context.Context.

func (*Context) Progress

func (c *Context) Progress(payload interface{})

Progress can be used to generate additional EventProgressed events, with implementation-specific payloads.

func (*Context) Run

func (c *Context) Run(jobs ...Job) (events Events)

Run runs the given jobs one after the other. If any job returns an error, subsequent jobs are not run.

func (*Context) RunContext

func (c *Context) RunContext(ctx context.Context, jobs ...Job) (events Events)

RunContext is identical to Run, but accepts a context.Context.

type Event

type Event interface {
	// Job is the job for which the event has occurred.
	Job() Job
}

Event signals that a job has been queued, started, progressed, or finished.

type EventFinished

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

EventFinished occurs when a job finishes.

func (*EventFinished) Error

func (e *EventFinished) Error() error

Error is the error returned by the job.

func (*EventFinished) Job

func (e *EventFinished) Job() Job

Job is the job that finished.

type EventProgressed

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

EventProgressed occurs when a job progresses. It is triggered by a call from the job to its Context.Progress function.

func (*EventProgressed) Job

func (e *EventProgressed) Job() Job

Job is the job that has progressed.

func (*EventProgressed) Payload

func (e *EventProgressed) Payload() interface{}

Payload is the job-specific payload that contains information about the job's progress. It is the argument given by the job to its Context.Progress.

type EventQueued

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

EventQueued occurs when an event is queued by a call to Run, RunContext, Parallel, or ParallelContext. When multiple jobs are queued, EventQueued events occur in the same order.

func (*EventQueued) Job

func (e *EventQueued) Job() Job

Job is the job that was queued.

func (*EventQueued) Parent

func (e *EventQueued) Parent() Job

Parent is the job that queued Job, if it was queued using another job's Context. Otherwise, Parent is nil.

type EventStarted

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

EventStarted occurs when a Job starts.

func (*EventStarted) Job

func (e *EventStarted) Job() Job

Job is the job that started.

type Events

type Events chan Event

Events is a synchronous event bus for jobs and their sub-jobs.

func Parallel

func Parallel(concurrency int, jobs ...Job) Events

Parallel runs the given jobs using a queue and the number of worker specified by concurrency. Errors do not affect continuation.

func ParallelContext

func ParallelContext(ctx context.Context, concurrency int, jobs ...Job) Events

ParallelContext is identical to Parallel, but accepts a context.Context.

func Run

func Run(jobs ...Job) Events

Run runs the given jobs one after the other. If any job returns an error, subsequent jobs are not run.

func RunContext

func RunContext(ctx context.Context, jobs ...Job) Events

RunContext is identical to Run, but accepts a context.Context.

func (Events) Wait

func (c Events) Wait() (err error)

Wait consumes the entire event bus, and returns the last non-nil EventFinished.Error. Combine Wait with Run to run simple halt-on-error sequences. For example:

err := Run(firstJob, secondJob, thirdJob).Wait()

type Formatter

type Formatter func(event Event) (text string)

Formatter transforms an event into a description, to be displayed by a presenter.

type Job

type Job interface {
	Run(ctx *Context) (err error)
}

Job is a single unit of work.

type JobFuncContext

type JobFuncContext func(ctx *Context) (err error)

JobFuncContext converts a simple function that accepts a Context to a Job implementation.

func JobFunc

func JobFunc(job func() (err error)) JobFuncContext

JobFunc converts a simple function to a Job implementation.

func (JobFuncContext) Run

func (j JobFuncContext) Run(ctx *Context) (err error)

Run implements Job.

type Sequence

type Sequence []Job

Sequence is an implementation of Job that runs a sequence of jobs using its Context.Run.

func (Sequence) Run

func (s Sequence) Run(ctx *Context) (err error)

Run implements Job.

type TextPresenter

type TextPresenter struct {
	// When true, jobs that were queued but did not start will be included in TextPresenter's output.
	ShowSkipped bool

	// SuffixOk is appended to lines of jobs that succeed.
	SuffixOk string

	// SuffixFail is appended to lines of jobs that fail.
	SuffixFail string

	// SuffixSkipped is appended to lines of jobs that are queued but do not run.
	SuffixSkipped string

	// Indent is prepended to sub-jobs.
	Indent string
	// contains filtered or unexported fields
}

TextPresenter wraps a Job, and uses a Formatter to display each of Job's child jobs on a new line.

func NewTextPresenter

func NewTextPresenter(job Job, writer io.Writer, formatter Formatter) *TextPresenter

NewTextPresenter creates a TextPresenter .

func (*TextPresenter) Run

func (t *TextPresenter) Run(ctx *Context) (err error)

Run implements Job.

type TreeBinding

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

TreeBinding creates a trees.Node on the given trees.Node for every descendent Job spawned by the given Job, and provides them through the Node function. TreeBinding implements Job, and can be run like any other Job. A zero TreeBinding is not valid; use NewTreeBinding.

func NewTreeBinding

func NewTreeBinding(job Job, node trees.Node) *TreeBinding

NewTreeBinding creates a new TreeBinding for the given job and node.

func (*TreeBinding) Node

func (t *TreeBinding) Node(job Job) (node trees.Node)

Node returns the trees.Node for the given job, if the job has already been queued as the TreeBinding's job or one of its descendents. Otherwise, Node returns nil.

func (*TreeBinding) Run

func (t *TreeBinding) Run(ctx *Context) (err error)

Run implements Job.

type TreePresenter

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

TreePresenter wraps a Job, and uses a Formatter to display each of Job's descendants in a node of a trees.Node.

func NewTreePresenter

func NewTreePresenter(job Job, node trees.Node, formatter Formatter) *TreePresenter

NewTreePresenter creates TreePresenter.

func (*TreePresenter) Run

func (t *TreePresenter) Run(ctx *Context) (err error)

Run implements Job.

Jump to

Keyboard shortcuts

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