Categorygithub.com/forhsd/go-workflow
modulepackage
0.1.3
Repository: https://github.com/forhsd/go-workflow.git
Documentation: pkg.go.dev

# README

Workflow - a library organizes steps with dependencies into DAG (Directed-Acyclic-Graph) for Go

Go Report Card Go Test Status Go Test Coverage

Overview

Strongly encourage everyone to read examples in the examples directory to have a quick understanding of how to use this library.

go-workflow helps Go developers organize steps with dependencies into a Directed-Acyclic-Graph (DAG).

  • It provides a simple and flexible way to define and execute a workflow.
  • It is easy to implement steps and compose them into a composite step.
  • It uses goroutine to execute steps concurrently.
  • It supports retry, timeout, and other configurations for each step.
  • It supports callbacks to hook before / after each step.

See it in action:

package yours

import (
    "context"

    flow "github.com/forhsd/go-workflow"
)

type Step struct{ Value string }

// All required for a step is `Do(context.Context) error`
func (s *Step) Do(ctx context.Context) error {
    fmt.Println(s.Value)
    return nil
}

func main() {
    // declare steps
    var (
        a = new(Step)
        b = &Step{Value: "B"}
        c = flow.Func("declare from anonymous function", func(ctx context.Context) error {
            fmt.Println("C")
            return nil
        })
    )
    // compose steps into a workflow!
    w := new(flow.Workflow)
    w.Add(
        flow.Step(b).DependsOn(a),     // use DependsOn to define dependencies
        flow.Steps(a, b).DependsOn(c), // final execution order: c -> a -> b

        // other configurations, like retry, timeout, condition, etc.
        flow.Step(c).
            Retry(func(ro *flow.RetryOption) {
                ro.Attempts = 3 // retry 3 times
            }).
            Timeout(10*time.Minute), // timeout after 10 minutes

        // use Input to change step at runtime
        flow.Step(a).Input(func(ctx context.Context, a *Step) error {
            a.Value = "A"
            return nil
        }),
    )
    // execute the workflow and block until all steps are terminated
    err := w.Do(context.Background())
}

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

# Packages

No description provided by the author

# Functions

AllSucceeded: all Upstreams are Succeeded.
AllSucceededOrSkipped: all Upstreams are Succeeded or Skipped.
Always: as long as all Upstreams are terminated.
AnyFailed: any Upstream is Failed.
AnySucceeded: any Upstream is Succeeded.
As finds all steps in the tree of step that matches target type, and returns them.
BatchPipe creates a batched pipeline in Workflow.
BeCanceled: only run when the workflow is canceled.
Cancel marks the current step as `Canceled`, and reports the error.
ConditionOrDefault will use DefaultCondition if cond is nil.
Func constructs a Step from an arbitrary function.
No description provided by the author
No description provided by the author
No description provided by the author
Has reports whether there is any step inside matches target type.
HasStep reports whether there is any step matches target step.
If adds a conditional branch to the workflow.
Keys returns the keys of the map m.
LogValue is used with log/slog, you can use it like: logger.With("step", LogValue(step)) To prevent expensive String() calls, logger.With("step", String(step)).
Mock helps to mock a step in Workflow.
Name can rename a Step.
NameFunc can rename a Step with a runtime function.
Names can rename multiple Steps.
NameStringer can rename a Step with a fmt.Stringer, which allows String() method to be called at runtime.
NoOp constructs a step doing nothing.
Pipe creates a pipeline in Workflow.
Skip marks the current step as `Skipped`, and reports the error.
StatusFromError gets the StepStatus from error.
Step declares Step ready to be added into Workflow.
Steps declares a series of Steps ready to be added into Workflow.
String unwraps step and returns a proper string representation.
Succeed marks the current step as `Succeeded`, while still reports the error.
Switch adds a switch branch to the workflow.
ToSteps converts []<StepDoer implemention> to []StepDoer.
Traverse performs a pre-order traversal of the tree of step.
Values returns the values of the map m.
WithStackTraces saves stack frames into error.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
TraverseContinue continue the traversal.
TraverseEndBranch end the current branch, but continue sibling branches.
TraverseStop stop and exit the traversal immediately.

# Variables

DefaultCondition used in workflow, defaults to AllSucceeded.
DefaultIsCanceled is used to determine whether an error is being regarded as canceled.
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
BranchCheck represents a branch to be checked.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ErrWithStackTraces saves stack frames into error, and prints error into error message Stack Traces: file:line.
Function wraps an arbitrary function as a Step.
IfBranch adds target step, then and else step to workflow, and check the target step and determine which branch to go.
MockStep helps to mock a step.
NamedStep is a wrapper of Steper, it gives your step a name by overriding String() method.
No description provided by the author
RetryEvent is the event fired when a retry happens.
RetryOption customizes retry behavior of a Step in Workflow.
State is the internal state of a Step in a Workflow.
StepBuilder allows to build the internal Steps when adding into Workflow.
No description provided by the author
No description provided by the author
StepResult contains the status and error of a Step.
No description provided by the author
SwitchBranch adds target step, cases and default step to workflow, and check the target step and determine which branch to go.
Workflow represents a collection of connected Steps that form a directed acyclic graph (DAG).

# Interfaces

Steper describes the requirement for a Step, which is basic unit of a Workflow.
WorkflowAdder is addable into Workflow.

# Type aliases

No description provided by the author
AfterStep defines callback being called AFTER step being executed.
BeforeStep defines callback being called BEFORE step being executed.
BranchCheckFunc checks the target and returns true if the branch should be selected.
Condition is a function to determine what's the next status of Step.
ErrCycleDependency means there is a cycle-dependency in your Workflow!!!.
ErrWorkflow contains all errors reported from terminated Steps in Workflow.
No description provided by the author
StepStatus describes the status of a Step.
No description provided by the author