wfe

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

wfe - Go Workflow Engine

License GoDoc Project status Go Report Card

wfe is a lightweight and flexible engine that allows you to define and manage workflows in Go. It enables you to easily model and execute a variety of workflows, from simple tasks to complex processes.

Features

  • Node-based structure: Structure your workflows as a series of steps (nodes).
  • Actions: Define actions to be executed at each step.
  • Type safety: Ensure type safety in your workflows using generics.
  • Easy integration: Easily integrate into your existing Go projects.
  • Flexibility: Customizable to support different workflow scenarios.

Installation

go get github.com/9ssi7/wfe

Simple Example

package main

import (
    "context"

    "github.com/9ssi7/wfe"
)

type Todo struct {
    Title string
}

func main() {
    flow := wfe.New[Todo]("todo")
    flow.AddAction("print", func(ctx context.Context, p Todo) error {
        println(p.Title)
        return nil
    })
    flow.AddNode(wfe.NewNode("print"))
    flow.Run(context.Background(), Todo{Title: "Hello, World!"})
}

Kanban Application Example

For a more complex example, see the Kanban application example.

  • Business Process Automation: Model and automate complex business processes.
  • Event-driven Systems: Handle events and trigger corresponding workflows.
  • User Interface Interactions: Orchestrate UI interactions and backend logic.
  • Algorithm Generation: Allow users to create algorithms through a visual interface, defining the steps and actions within the workflow.

Contributing

Contributions are welcome! Feel free to submit bug reports, feature requests, or pull requests.

License

This project is licensed under the Apache License 2.0. See the LICENSE file for the full license text.

Documentation

Overview

Package wfe provides a workflow engine.

This package allows you to define and execute workflows consisting of nodes and actions. You can create different types of flows, such as task flows and cron flows, and add nodes and actions to them. Actions can be defined using `ActionRunner` functions, and flows can be executed with a context and payload.

Example usage:

package main

import (
    "context"
    "fmt"
    "wfe"
)

func main() {
    flow := wfe.New[string]("myFlow")

    flow.AddAction("hello", func(ctx context.Context, name string) error {
        fmt.Println("Hello,", name)
        return nil
    })

    flow.AddNode(wfe.NewNode("helloNode", "hello"))

    if err := flow.Run(context.Background(), "World"); err != nil {
        fmt.Println("Error:", err)
    }
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action[P any] interface {
	// Run executes the action with the given context and payload.
	Run(ctx context.Context, p P) error
	Reference() string
	// ErrorRef returns the error reference of the action, if any.
	ErrorRef() *string
}

Action is an interface representing an action in a workflow.

func NewAction

func NewAction[P any](ref string, fn ActionRunner[P], errRef ...string) Action[P]

type ActionRunner

type ActionRunner[P any] func(ctx context.Context, p P) error

ActionRunner is a function that runs an action with the given context and payload.

type Flow

type Flow[P any] interface {
	Name() string
	Kind() Kind

	// AddNode adds one or more nodes to the flow.
	AddNode(n ...Node)

	// AddAction adds an action to the flow with the given reference, runner function, and optional error reference.
	AddAction(ref string, fn ActionRunner[P], errRef ...string)

	// GetAction retrieves an action from the flow by its reference.
	GetAction(ref string) (Action[P], bool)

	// Run executes the flow with the given context and payload.
	Run(ctx context.Context, p P) error

	// Cancel cancels the flow execution with the given context.
	Cancel(ctx context.Context) error
}

Flow is an interface representing a workflow.

func New

func New[P any](name string) Flow[P]

New creates a new task flow with the given name.

func NewWithCron

func NewWithCron[P any](name, trigger string) Flow[P]

NewWithCron creates a new cron flow with the given name and trigger.

type Kind

type Kind int

Kind represents the type of a flow.

const (
	KindTask Kind = iota
	KindEvent
	KindCron
)

type Node

type Node struct {
	// Name of the node.
	Name string
	// ActionRef is the reference to the action associated with the node.
	ActionRef string
	// Env is a map of environment variables for the node.
	Env map[string]any
}

Node represents a node in a workflow.

func NewNode

func NewNode(name string, actionRef ...string) Node

NewNode creates a new Node with the given name and optional action reference. If actionRef is provided, it will be used as the ActionRef; otherwise, the name will be used.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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