xgraph

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

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

Go to latest
Published: Jul 2, 2018 License: MIT Imports: 9 Imported by: 4

README

xgraph GoDocBuild Status

Documentation

Overview

Package xgraph runs a sequence of user defined jobs in an appropriate order, given their dependencies.

Index

Constants

This section is empty.

Variables

View Source
var ErrMissingCallback = errors.New("missing callback for BasicJob")

ErrMissingCallback indicates that a callback is missing on a BasicJob.

Functions

This section is empty.

Types

type BasicJob

type BasicJob struct {
	// JobName of the BasicJob.
	// Required.
	JobName string

	// RunCallback is called when the BasicJob is run.
	// Required.
	RunCallback func() error

	// ShouldRunCallback returns whether the BasicJob should be run.
	// Defaults to a function that always returns true.
	ShouldRunCallback func() (bool, error)

	// Deps is a list of dependencies for the BasicJob.
	// Defaults to []string{}.
	Deps []string
}

BasicJob is a simple type which implements Job.

func (BasicJob) Dependencies

func (bj BasicJob) Dependencies() ([]string, error)

Dependencies returns the dependencies list of the BasicJob. Never returns an error. If Deps is nil, returns an empty slice for the dependencies.

func (BasicJob) Name

func (bj BasicJob) Name() string

Name returns the name of the Job.

func (BasicJob) Run

func (bj BasicJob) Run(ctx context.Context) error

Run runs the BasicJob, calling RunCallback. Returns ErrMissingCallback if RunCallback is nil.

func (BasicJob) ShouldRun

func (bj BasicJob) ShouldRun() (bool, error)

ShouldRun checks ifd the BasicJob should be run, using ShouldRunCallback. Returns ErrMissingCallback if ShouldRunCallback is nil.

type BuildDependencyError

type BuildDependencyError []string

BuildDependencyError is an error indicating that dependencies failed

func (BuildDependencyError) Error

func (bde BuildDependencyError) Error() string

type CallbackTracker

type CallbackTracker func(err error)

CallbackTracker is a WorkTracker which calls a function on completion.

func (CallbackTracker) OnComplete

func (ct CallbackTracker) OnComplete(err error)

OnComplete calls the callback and implements WorkTracker.

type DependencyCycleError

type DependencyCycleError []string

DependencyCycleError is an error indicating that there is a dependency cycle

func (DependencyCycleError) Error

func (dce DependencyCycleError) Error() string

type EventHandler

type EventHandler interface {
	// OnQueued is called when a Job has been queued (waiting for dependencies)
	OnQueued(job string)

	// OnStart is called when a Job has been started
	OnStart(job string)

	// OnFinish is called when a Job has finished
	OnFinish(job string)

	// OnError is called when a Job fails
	OnError(job string, err error)
}

EventHandler is an interface used to process build events

var NoOpEventHandler EventHandler = nophandler{}

NoOpEventHandler is an EventHandler which does nothing

type FailHandler

type FailHandler func(error)

FailHandler is a type of function used as a callback for a Promise on failure

type FinishHandler

type FinishHandler func()

FinishHandler is a type of function used as a callback for a Promise on success

type Graph

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

Graph is an execution graph

func New

func New() *Graph

New creates a new Graph

func (*Graph) AddGenerator

func (g *Graph) AddGenerator(generator JobGenerator) *Graph

AddGenerator adds a JobGenerator to the Graph

func (*Graph) AddJob

func (g *Graph) AddJob(job Job) *Graph

AddJob adds a Job to the Graph

func (*Graph) GetJob

func (g *Graph) GetJob(name string) (j Job, err error)

GetJob searches the Graph for a Job with the specified name.

type Job

type Job interface {
	// Name returns the name of the Job.
	Name() string

	// The Run method runs the job.
	// Is called after the dependencies have been run successfully.
	// A context may be provided for cancellation purposes.
	Run(ctx context.Context) error

	// ShouldRun checks if the job should be run.
	// Job is marked as errored if this returns an error.
	// Dependents will be run even if this Job does not need to be run.
	ShouldRun() (bool, error)

	// Dependencies returns a list of dependencies for the Job.
	// If this returns an error, the Job is marked as errored.
	Dependencies() ([]string, error)
}

Job is an operation in the execution graph

type JobGenerator

type JobGenerator func(name string) (Job, error)

JobGenerator is a type which generates Jobs dynamically. If a job with the given name should not be generated by this generator, a nil Job should be returned. Any errors will be propogated.

type JobNotFoundError

type JobNotFoundError string

JobNotFoundError is an error type indicating that a job was not found. The underlying string is the name of the job.

func (JobNotFoundError) Error

func (err JobNotFoundError) Error() string

func (JobNotFoundError) String

func (err JobNotFoundError) String() string

type Promise

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

Promise is a future

func NewPromise

func NewPromise(fun func(FinishHandler, FailHandler)) *Promise

NewPromise returns a *Promise using the given function

func (*Promise) Then

func (p *Promise) Then(success FinishHandler, failure FailHandler)

Then registers callbacks and starts the Promise if it has not been already started. If the Promise has already completed, the success/failure handler is immediately called with the resu;t. This is like Promise.then in JavaScript.

type Runner

type Runner struct {
	Graph        *Graph
	WorkRunner   WorkRunner
	EventHandler EventHandler
}

Runner is a tool to run graphs

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, targets ...string)

Run executes the targets on the graph

type Task

type Task func() error

Task is a task function.

func (Task) Run

func (t Task) Run(tracker WorkTracker)

Run runs a task on the local goroutine, using the tracker to notify of completion.

type WorkRunner

type WorkRunner interface {
	// Do calls a func asynchronously.
	DoTask(task Task, tracker WorkTracker)

	// WorkRunner is closable - Close should clean up all existing state.
	io.Closer
}

WorkRunner is a type

func NewWorkPool

func NewWorkPool(parallel uint16) WorkRunner

NewWorkPool returns a WorkRunner that uses a fixed pool of goroutines. parallel is the number of goroutines to use in the pool. If parallel is 0, then one goroutine will be used per CPU.

type WorkTracker

type WorkTracker interface {
	// OnComplete is called when a task completes.
	// The error value from the job is passed as an argument.
	OnComplete(err error)
}

WorkTracker is an interface used to track completion of jobs.

Jump to

Keyboard shortcuts

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