taskflow

package module
Version: v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

README

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)])])
}

Documentation

Overview

Package taskflow provides some tools to execute many tasks in a certain turn.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func LogTaskFunc

func LogTaskFunc(logf func(fmt string, args ...interface{})) func(Task) Task

LogTaskFunc returns a wrap function to create the LogTask.

func UnwrapFailedTaskDoError added in v0.5.0

func UnwrapFailedTaskDoError(err error) error

UnwrapFailedTaskDoError unwraps the original error returned by the failed task.

Types

type Flow

type Flow interface {
	// AddTasks adds a set of Tasks or Flows.
	AddTasks(tasks ...Task)

	Task
}

Flow is used to execute the task by the certain relationship.

type FlowError

type FlowError struct {
	Name string
	TaskErrors
}

FlowError represents the flow error.

func NewFlowError

func NewFlowError(name string, errs ...TaskError) FlowError

NewFlowError returns a new FlowError.

func (FlowError) Error

func (e FlowError) Error() string

func (FlowError) Unwrap

func (e FlowError) Unwrap() error

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

func NewLineFlow(name string) *LineFlow

NewLineFlow returns a new line flow, which executes the task in turn.

func (*LineFlow) AddTask added in v0.4.0

func (f *LineFlow) AddTask(name string, do TaskFunc, undo ...TaskFunc)

AddTask adds the task with the task name and do/undo function.

func (*LineFlow) AddTasks

func (f *LineFlow) AddTasks(tasks ...Task)

AddTasks adds the task or flow into the line flow.

func (*LineFlow) Do

func (f *LineFlow) Do(c context.Context) (err error)

Do does the tasks, which undoes them if a certain task fails.

func (*LineFlow) DoneTasks

func (f *LineFlow) DoneTasks() []Task

DoneTasks returns all the done tasks.

func (*LineFlow) GetCtx

func (f *LineFlow) GetCtx(key string) interface{}

GetCtx returns the value of the context named key.

Return nil if the key does not exist.

func (*LineFlow) Name

func (f *LineFlow) Name() string

Name returns the name of line flow.

func (*LineFlow) SetCtx

func (f *LineFlow) SetCtx(key string, value interface{})

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

func (f *LineFlow) SetErrorHandler(handle func(err error))

SetErrorHandler sets the handler to handle it if there is an error.

func (*LineFlow) SetUndoAllTasks

func (f *LineFlow) SetUndoAllTasks(b bool)

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

func (f *LineFlow) SetUndoFailedTask(undo bool)

SetUndoFailedTask sets whether to undo the failed task or not.

func (*LineFlow) String

func (f *LineFlow) String() string

func (*LineFlow) Tasks

func (f *LineFlow) Tasks() []Task

Tasks returns all the tasks.

func (*LineFlow) Undo

func (f *LineFlow) Undo(c context.Context) error

Undo undoes the done tasks.

type Task

type Task interface {
	fmt.Stringer
	Name() string
	Undo(context.Context) error
	Do(context.Context) error
}

Task represents a task.

func NewLogTask

func NewLogTask(task Task, logf func(fmt string, args ...interface{})) Task

NewLogTask wraps the task t and prints the log before executing the task.

func NewRetryTask

func NewRetryTask(t Task, retryNum int, retryInterval time.Duration) Task

NewRetryTask returns a new Task, which will wrap the task t and retry it if the task fails.

func NewTask

func NewTask(name string, do TaskFunc, undo ...TaskFunc) Task

NewTask returns a new Task.

type TaskError

type TaskError interface {
	error

	Name() string
	DoErr() error
	UndoErr() error
}

TaskError is used to represent the task error.

func NewTaskError

func NewTaskError(name string, doErr, undoErr error) TaskError

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

type TaskFunc

type TaskFunc func(context.Context) error

TaskFunc is the function to execute a task.

type UndoAll

type UndoAll interface {
	UndoAll(context.Context) error
}

UndoAll is used to undo all the tasks.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
Morty Proxy This is a proxified and sanitized view of the page, visit original site.