Code Monkey home page Code Monkey logo

go-taskflow's Introduction

go-taskflow Build Status GoDoc License

A task flow supporting Go1.7+, you can use it to do and undo the tasks.

Installation

$ go get -u github.com/xgfone/go-taskflow

Example

package main

import (
	"context"
	"fmt"

	"github.com/xgfone/go-taskflow"
)

func logf(msg string, args ...interface{}) error {
	fmt.Printf(msg+"\n", args...)
	return nil
}

func do(n string) taskflow.TaskFunc {
	return func(context.Context) error { return logf("do the task '%s'", n) }
}

func undo(n string) taskflow.TaskFunc {
	return func(context.Context) error { return logf("undo the task '%s'", n) }
}

func failDo(n string) taskflow.TaskFunc {
	return func(context.Context) error {
		logf("do the task '%s'", n)
		return fmt.Errorf("failure")
	}
}

func newTask(n string) taskflow.Task {
	return taskflow.NewTask(n, do(n), undo(n))
}

func newFailTask(n string) taskflow.Task {
	return taskflow.NewTask(n, failDo(n), undo(n))
}

func main() {
	flow1 := taskflow.NewLineFlow("lineflow1")
	flow1.
		AddTasks(
			newTask("task1"),
			newTask("task2"),
			newTask("task3"),
		)

	flow2 := taskflow.NewLineFlow("lineflow2")
	flow2.
		AddTasks(
			newTask("task4"),
			newFailTask("task5"),
			newTask("task6"),
		)

	flow3 := taskflow.NewLineFlow("lineflow3")
	flow3.AddTask("task7", do("task7"), undo("task7")) // Use task functions
	flow3.
		AddTasks(
			flow1,
			newTask("task8"),
			flow2,
			newTask("task9"),
		)

	err := flow3.Do(context.Background())
	fmt.Println(err)

	// Output:
	// do the task 'task7'
	// do the task 'task1'
	// do the task 'task2'
	// do the task 'task3'
	// do the task 'task8'
	// do the task 'task4'
	// do the task 'task5'
	// undo the task 'task4'
	// undo the task 'task8'
	// undo the task 'task3'
	// undo the task 'task2'
	// undo the task 'task1'
	// undo the task 'task7'
	// FlowError(name=lineflow3, errs=[FlowError(name=lineflow2, errs=[TaskError(name=task5, doerr=failure)])])
}

go-taskflow's People

Contributors

xgfone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.