Documentation
¶
Overview ¶
Package taskflow provides some tools to execute many tasks in a certain turn.
Index ¶
- func LogTaskFunc(logf func(fmt string, args ...interface{})) func(Task) Task
- func UnwrapFailedTaskDoError(err error) error
- type Flow
- type FlowError
- type LineFlow
- func (f *LineFlow) AddTask(name string, do TaskFunc, undo ...TaskFunc)
- func (f *LineFlow) AddTasks(tasks ...Task)
- func (f *LineFlow) Do(c context.Context) (err error)
- func (f *LineFlow) DoneTasks() []Task
- func (f *LineFlow) GetCtx(key string) interface{}
- func (f *LineFlow) Name() string
- func (f *LineFlow) SetCtx(key string, value interface{})
- func (f *LineFlow) SetErrorHandler(handle func(err error))
- func (f *LineFlow) SetUndoAllTasks(b bool)
- func (f *LineFlow) SetUndoFailedTask(undo bool)
- func (f *LineFlow) String() string
- func (f *LineFlow) Tasks() []Task
- func (f *LineFlow) Undo(c context.Context) error
- type Task
- type TaskError
- type TaskErrors
- type TaskFunc
- type UndoAll
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LogTaskFunc ¶
LogTaskFunc returns a wrap function to create the LogTask.
func UnwrapFailedTaskDoError ¶ added in v0.5.0
UnwrapFailedTaskDoError unwraps the original error returned by the failed task.
Types ¶
type FlowError ¶
type FlowError struct {
Name string
TaskErrors
}
FlowError represents the flow error.
func NewFlowError ¶
NewFlowError returns a new FlowError.
type LineFlow ¶
type LineFlow struct {
// contains filtered or unexported fields
}
LineFlow is used to execute the tasks in turn.
Example ¶
logf := func(msg string, args ...interface{}) { fmt.Printf(msg+"\n", args...) }
do := func(n string) TaskFunc {
return func(c context.Context) error { logf("do the task '%s'", n); return nil }
}
undo := func(n string) TaskFunc {
return func(c context.Context) error { logf("undo the task '%s'", n); return nil }
}
failDo := func(n string) TaskFunc {
return func(c context.Context) error {
logf("do the task '%s'", n)
return fmt.Errorf("failure")
}
}
newTask := func(n string) Task { return NewTask(n, do(n), undo(n)) }
newFailTask := func(n string) Task { return NewTask(n, failDo(n), undo(n)) }
flow1 := NewLineFlow("lineflow1")
flow1.
AddTasks(
newTask("task1"),
newTask("task2"),
newTask("task3"),
)
flow2 := NewLineFlow("lineflow2")
flow2.
AddTasks(
newTask("task4"),
newFailTask("task5"),
newTask("task6"),
)
flow3 := 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)])])
func NewLineFlow ¶
NewLineFlow returns a new line flow, which executes the task in turn.
func (*LineFlow) AddTask ¶ added in v0.4.0
AddTask adds the task with the task name and do/undo function.
func (*LineFlow) GetCtx ¶
GetCtx returns the value of the context named key.
Return nil if the key does not exist.
func (*LineFlow) SetCtx ¶
SetCtx adds the key-value context to allow that the subsequent tasks access it, which will override it if the key has existed.
func (*LineFlow) SetErrorHandler ¶ added in v0.5.0
SetErrorHandler sets the handler to handle it if there is an error.
func (*LineFlow) SetUndoAllTasks ¶
SetUndoAllTasks sets whether to undo all the tasks or not if the task has implemented the interface UndoAll.
func (*LineFlow) SetUndoFailedTask ¶ added in v0.4.0
SetUndoFailedTask sets whether to undo the failed task or not.
type Task ¶
type Task interface {
fmt.Stringer
Name() string
Undo(context.Context) error
Do(context.Context) error
}
Task represents a task.
func NewLogTask ¶
NewLogTask wraps the task t and prints the log before executing the task.
func NewRetryTask ¶
NewRetryTask returns a new Task, which will wrap the task t and retry it if the task fails.
type TaskError ¶
TaskError is used to represent the task error.
func NewTaskError ¶
NewTaskError returns a new TaskError.
type TaskErrors ¶
type TaskErrors []TaskError
TaskErrors is a set of TaskError.
func (*TaskErrors) Append ¶
func (es *TaskErrors) Append(name string, doErr, undoErr error)
Append is equal to es.AppendTaskError(NewTaskError(name, doErr, undoErr)).
func (*TaskErrors) AppendTaskError ¶
func (es *TaskErrors) AppendTaskError(e TaskError)
AppendTaskError appends the task error.
func (TaskErrors) Error ¶
func (es TaskErrors) Error() string