rund

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2019 License: BSD-3-Clause Imports: 6 Imported by: 0

README

rund

GoDoc

rund is a task dependency scheduler to run all tasks in parallel topological order, using the semantic of Directed Acyclic Graph.

Install

go get github.com/andy2046/rund

Example

r := rund.New()

op1 := rund.NewFuncOperator(func() error {
	fmt.Println("1 will run before 2 and 3")
	return nil
})
op2 := rund.NewFuncOperator(func() error {
	fmt.Println("2 and 3 will run in parallel before 4")
	return nil
})
op3 := rund.NewFuncOperator(func() error {
	fmt.Println("2 and 3 will run in parallel before 4")
	return nil
})
op4 := rund.NewFuncOperator(func() error {
	fmt.Println("4 will run after 2 and 3")
	return errors.New("4 is broken")
})
op5 := rund.NewFuncOperator(func() error {
	fmt.Println("5 will never run")
	return nil
})

r.AddNode("1", op1)
r.AddNode("2", op2)
r.AddNode("3", op3)
r.AddNode("4", op4)
r.AddNode("5", op5)

r.AddEdge("1", "2")
r.AddEdge("1", "3")
r.AddEdge("2", "4")
r.AddEdge("3", "4")
r.AddEdge("4", "5")

//     2
//   /   \
//  1     4 - 5
//   \   /
//     3

fmt.Printf("the result: %v\n", r.Run())
// Output:
// 1 will run before 2 and 3
// 2 and 3 will run in parallel before 4
// 2 and 3 will run in parallel before 4
// 4 will run after 2 and 3
// the result: 4 is broken

Documentation

Overview

Package rund implements a DAG task dependency scheduler.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CmdOperator

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

CmdOperator runs external command.

func NewCmdOperator

func NewCmdOperator(cmd []string, envar map[string]string) (CmdOperator, error)

NewCmdOperator returns a new CmdOperator.

func (CmdOperator) Run

func (op CmdOperator) Run() error

Run runs external command.

type FuncOperator

type FuncOperator func() error

FuncOperator executes function.

func NewFuncOperator

func NewFuncOperator(f func() error) FuncOperator

NewFuncOperator returns a new FuncOperator.

func (FuncOperator) Run

func (op FuncOperator) Run() error

Run executes function.

type NoopOperator

type NoopOperator struct{}

NoopOperator does nothing.

func NewNoopOperator

func NewNoopOperator() NoopOperator

NewNoopOperator returns a new NoopOperator.

func (NoopOperator) Run

func (op NoopOperator) Run() error

Run runs nooperation.

type Operator

type Operator interface {
	Run() error
}

Operator defines the Run interface to run operation.

type Rund

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

Rund adds `Operator` into the graph as node and edge, `Operator` runs in parallel topological order.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/andy2046/rund"
)

func main() {
	r := rund.New()

	op1 := rund.NewFuncOperator(func() error {
		fmt.Println("1 will run before 2 and 3")
		return nil
	})
	op2 := rund.NewFuncOperator(func() error {
		fmt.Println("2 and 3 will run in parallel before 4")
		return nil
	})
	op3 := rund.NewFuncOperator(func() error {
		fmt.Println("2 and 3 will run in parallel before 4")
		return nil
	})
	op4 := rund.NewFuncOperator(func() error {
		fmt.Println("4 will run after 2 and 3")
		return errors.New("4 is broken")
	})
	op5 := rund.NewFuncOperator(func() error {
		fmt.Println("5 will never run")
		return nil
	})

	r.AddNode("1", op1)
	r.AddNode("2", op2)
	r.AddNode("3", op3)
	r.AddNode("4", op4)
	r.AddNode("5", op5)

	r.AddEdge("1", "2")
	r.AddEdge("1", "3")
	r.AddEdge("2", "4")
	r.AddEdge("3", "4")
	r.AddEdge("4", "5")

	//      2
	// 	  /   \
	//  1       4 - 5
	// 	  \   /
	// 	    3

	fmt.Printf("the result: %v\n", r.Run())
}
Output:

1 will run before 2 and 3
2 and 3 will run in parallel before 4
2 and 3 will run in parallel before 4
4 will run after 2 and 3
the result: 4 is broken

func New

func New() *Rund

New returns a new `Rund`.

func (*Rund) AddEdge

func (d *Rund) AddEdge(from, to string)

AddEdge establishes a dependency between two nodes in the graph, `from` node will be executed before `to` node.

func (*Rund) AddNode

func (d *Rund) AddNode(name string, op Operator)

AddNode adds an `Operator` as a node in the graph, `Operator` name should be unique.

func (*Rund) Run

func (d *Rund) Run() error

Run validates the graph then runs each node in parallel topological order, if any node Operator returns error, no more node will be scheduled.

type Status

type Status int

Status for `Operator`.

const (
	PENDING Status = iota
	RUNNING
	SKIPPED
	SUCCESS
	FAILED
)

`Operator` Status List.

Jump to

Keyboard shortcuts

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