https://github.com/xgfone/go-taskflow
A task flow in Go, you can use it to do and undo.
https://github.com/xgfone/go-taskflow
line-taskflow task-flow taskflow
Last synced: 6 months ago
JSON representation
A task flow in Go, you can use it to do and undo.
- Host: GitHub
- URL: https://github.com/xgfone/go-taskflow
- Owner: xgfone
- License: apache-2.0
- Created: 2016-04-14T14:50:34.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2022-09-13T09:06:41.000Z (over 2 years ago)
- Last Synced: 2024-10-30T20:49:13.439Z (7 months ago)
- Topics: line-taskflow, task-flow, taskflow
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 7
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-taskflow [](https://github.com/xgfone/go-taskflow/actions/workflows/go.yml) [](https://pkg.go.dev/github.com/xgfone/go-taskflow) [](https://raw.githubusercontent.com/xgfone/go-taskflow/master/LICENSE)
A task flow supporting `Go1.7+`, you can use it to do and undo the tasks.
## Installation
```shell
$ go get -u github.com/xgfone/go-taskflow
```## Example
```go
package mainimport (
"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)])])
}
```