Documentation
¶
Overview ¶
Package pipelines builds configurable, retryable workflows out of Steps: external processes (Command), Go functions (FuncStep) and groups of steps that run concurrently (ConcurrentStep). A Pipeline runs its steps in order, and several pipelines can run side by side.
What happens when a step succeeds or fails is decided by a Policy: retries, callbacks and, after a failure, whether to abort, continue or rerun. A policy is set for a whole pipeline (Pipeline.Policy) or for one step (WithPolicy, WithRetry).
A Pipeline is a state machine: every finished or aborted step moves it to a State chosen by Pipeline.Transition, for instance from the step's exit code, and the jobs registered for that state (Pipeline.On) are run.
All output goes through a Logger: step output and logrus entries are queued on one channel and demultiplexed per pipeline into log files and into the tabs of a gocui terminal viewer.
ctx, log, err := pipelines.NewLogger(ctx, "logs", true)
...
err = pipelines.RunPipelinesConcurrently(ctx, []pipelines.Pipeline{build, test})
log.Close()
Index ¶
- Constants
- func AcquireConfigLock(ctx context.Context, symb string) error
- func Always(a Action) func(context.Context, Step, error) Action
- func CopyFile(src string, dst string) error
- func Log(ctx context.Context) *logrus.Entry
- func PipelinesDir(ctx context.Context) string
- func ReleaseConfigLock(ctx context.Context, symb string) error
- func RunPipelinesConcurrently(ctx context.Context, pipelines []Pipeline, opts ...RunOption) error
- func WithLockToken(ctx context.Context) context.Context
- type Action
- type Command
- type ConcurrentStep
- type FuncStep
- type Logger
- type Option
- type Pipeline
- func (p *Pipeline) AddCommand(cmd Command)
- func (p *Pipeline) AddFunc(name, description string, fn func(ctx context.Context, sw *StepWriter) error)
- func (p *Pipeline) AddFuncStep(fs FuncStep)
- func (p *Pipeline) AddStep(s Step)
- func (p *Pipeline) GetLogFilePath() string
- func (p *Pipeline) HelpText() string
- func (p *Pipeline) On(s State, jobs ...Step)
- func (p *Pipeline) Run(ctx context.Context) error
- type PipelineFormatter
- type Policy
- type Result
- type RunOption
- type State
- type Step
- type StepWriter
Constants ¶
const All = "all"
All is the stream every line is copied to: the first tab and all.log.
Variables ¶
This section is empty.
Functions ¶
func AcquireConfigLock ¶
AcquireConfigLock acquires a filesystem-based reentrant lock for symb. It blocks until the lock is acquired or the context is cancelled.
func Log ¶ added in v0.2.0
Log returns an entry for the Logger in ctx that is bound to the pipeline running in ctx, so that what it logs lands in that pipeline's tab and file. Without a Logger it falls back to logrus's standard logger.
func PipelinesDir ¶ added in v0.1.4
PipelinesDir returns a pipelines directory path from the context, falling back to the PIPELINES_DIR environment variable, or $HOME/.pipelines/.
func ReleaseConfigLock ¶
ReleaseConfigLock releases the filesystem-based reentrant lock for symb.
func RunPipelinesConcurrently ¶
RunPipelinesConcurrently runs the pipelines concurrently and returns their joined errors. It runs the slice's elements in place, so that is where their State is found afterwards. The caller provides a context wired to cancellation.
Types ¶
type Command ¶
type Command struct {
Name string
Description string
Environment map[string]string
Executable string
Args []string
Options []Option
}
Command is a Step that runs an external command.
func (*Command) Execute ¶
func (c *Command) Execute(ctx context.Context, sw *StepWriter) error
Execute runs the command with its output sent to sw. On cancellation the process is interrupted and, if it lingers, killed five seconds later.
func (*Command) StepDescription ¶
type ConcurrentStep ¶
type ConcurrentStep struct {
Name string
Description string
Steps []Step
MaxConcurrency int // sub-steps running at once; <= 0 means all of them
}
ConcurrentStep is a Step that runs its sub-steps concurrently and completes when all of them have. The first sub-step to fail cancels the others and its error is returned; wrap a sub-step with WithPolicy to retry it or to tolerate its failure.
func (*ConcurrentStep) Execute ¶
func (cs *ConcurrentStep) Execute(ctx context.Context, sw *StepWriter) error
func (*ConcurrentStep) HelpInfo ¶
func (cs *ConcurrentStep) HelpInfo() string
func (*ConcurrentStep) StepDescription ¶
func (cs *ConcurrentStep) StepDescription() string
func (*ConcurrentStep) StepName ¶
func (cs *ConcurrentStep) StepName() string
type FuncStep ¶
type FuncStep struct {
Name string
Description string
Fn func(ctx context.Context, sw *StepWriter) error
}
FuncStep is a Step that calls a Go function.
func (*FuncStep) StepDescription ¶
type Logger ¶ added in v0.2.0
type Logger struct {
*logrus.Logger
Dir string // holds all.log and, unless Pipeline.LogDir is set, <pipeline>.log
// contains filtered or unexported fields
}
Logger demultiplexes the log of concurrently running pipelines. Step output and logrus entries are queued on one channel and a single goroutine routes each line, by the pipeline it belongs to, to that pipeline's log file and terminal tab, as well as to All.
The embedded logrus.Logger is the front end for structured entries: level, formatter and hooks work as usual, and an entry is routed by its "pipeline" field (see Log).
func LoggerFromContext ¶
LoggerFromContext returns the Logger carried by ctx, or nil.
func NewLogger ¶ added in v0.2.0
NewLogger starts a Logger that stores its files in dir (default PipelinesDir) and returns it together with a context that carries it.
With tui set, the terminal becomes a gocui viewer with one tab per stream, and the returned context is cancelled when the user presses q or Ctrl-C. Otherwise the lines of All are printed to stdout.
type Pipeline ¶
type Pipeline struct {
Name string
Description string
Steps []Step
Policy Policy // applied to every step; see WithPolicy for a single step
LogDir string // where <Name>.log is written; defaults to the Logger's Dir
// State is maintained by Run. Read it from Transition, from jobs or after Run.
State State
// Transition picks the state that follows a step; nil means Next.
Transition func(s State, r Result) State
// Jobs are the steps to run, in order, whenever a step leaves the pipeline
// in the given state. They run to completion even if the pipeline is
// cancelled, and their failures are logged without changing the state.
Jobs map[State][]Step
}
Pipeline is a state machine driven by its Steps, which it runs one after another. Run starts in Running. Each time a step finishes or gets aborted, Transition picks the next state from the step's Result and the Jobs of that state are run. A final state ends the run; a pipeline that is still Running after its last step becomes Succeeded.
func (*Pipeline) AddCommand ¶
func (*Pipeline) AddFuncStep ¶
func (*Pipeline) GetLogFilePath ¶
type PipelineFormatter ¶
type PipelineFormatter struct{}
PipelineFormatter is a logrus.Formatter that renders entries as
LEVL[15:04:05](pipeline)(step)(progress) message key=value ...
where the three leading fields are shown only if the entry has them.
type Policy ¶ added in v0.2.0
type Policy struct {
// Retry is passed to retry.Do. Without retry.Attempts the step is tried once;
// retry.OnRetry replaces the built-in "retrying" log line.
Retry []retry.Option
// OnSuccess is called after the step succeeded.
OnSuccess func(ctx context.Context, s Step)
// OnFailure is called after the step failed for good and picks the Action.
// nil means Abort.
OnFailure func(ctx context.Context, s Step, err error) Action
}
Policy is the control over a step's outcome. A step runs until it succeeds or its Retry budget is spent; then exactly one of the callbacks is called.
Cancellation is not a failure: once ctx is done the step is neither retried nor reported to OnFailure, and its error is returned as is.
type Result ¶ added in v0.2.1
type Result struct {
Step Step
Err error // nil on success
ExitCode int // 0 on success, else Err's ExitCode() (as *exec.ExitError has), or -1 if it has none
Aborted bool // the step ended because the pipeline was cancelled
}
Result is how a step ended, as the pipeline sees it after the step's policies.
type RunOption ¶
type RunOption func(*runOptions)
RunOption configures RunPipelinesConcurrently.
func ContinueOnError ¶
func ContinueOnError() RunOption
ContinueOnError keeps the other pipelines running when one fails. By default the first failure cancels them. Either way all errors are returned.
type State ¶ added in v0.2.1
type State string
State is where a Pipeline stands. The four states below are built in; a Transition may return any other value, such as "degraded".
type Step ¶
type Step interface {
StepName() string
StepDescription() string
HelpInfo() string
Execute(ctx context.Context, sw *StepWriter) error
}
Step is one unit of work in a Pipeline. Execute reports failure by returning an error; what happens next is decided by the Policy the step runs under.
func WithPolicy ¶ added in v0.2.0
WithPolicy wraps s so that it runs under p wherever it is placed: in a Pipeline, in a ConcurrentStep or inside another WithPolicy. An enclosing policy (such as Pipeline.Policy) only sees the error that p let through.
type StepWriter ¶
type StepWriter struct {
// contains filtered or unexported fields
}
StepWriter is the output handle passed to every Execute call. It cuts what is written into lines, tags each with the names of its pipeline and step, and queues them on the Logger. It is safe for concurrent use.
func New ¶
func New(w io.Writer) *StepWriter
New returns a StepWriter that prints its lines to w instead of a Logger, which is handy for running a Step on its own.
func (*StepWriter) Flush ¶
func (sw *StepWriter) Flush() error
Flush emits a trailing line that was not terminated by a newline.