Categorygithub.com/daanv2/go-tasks
modulepackage
0.0.1
Repository: https://github.com/daanv2/go-tasks.git
Documentation: pkg.go.dev

# README

Go Tasks

GitHub go.mod Go version GitHub release (latest by date) 🐹 Golang

A package that allows you to easily parallelize tasks and chain them together. As well as allowing you to easily create a task that can be reused.

Examples

//State object
type User struct {
	Email  string
	Name   string
	Age    int
	Scopes []string
}

//Example 1

//Create a new task around the state object
task := tasks.New[User]()
task.
	//Add a tasks to the chain that will execute in order
	Do(RetrieveFromCache).
	Do(RetrieveFromDatabase).
	//What to do if the tasks is successful
	Then(UpdateCache).
	// What to do when the task is done
	Finally(func(user *User, ctx context.Context) error {
		zap.S().Infof("User: %+v", user)
		return nil
	}).
	//What to do if the tasks fails
	OnError(func(user *User, ctx context.Context, err error) {
		zap.S().Errorf("Error: %+v", err)
	})

err := task.Run(context.Background())

//Example 2
task := NewWith[User](&User{Email: "[email protected]"})
task.
	// If the first function returns an error, run the second function.
	Do(IfElse(RetrieveFromCache, RetrieveFromDatabase)).
	Then(UpdateCache)

//Example 3
task := tasks.New[User]()
task.
	Do(RetrieveFromCache).
	Do(RetrieveFromDatabase).
	Then(UpdateCache).
	Finally(func(user *User, ctx context.Context) error {
		zap.S().Infof("User: %+v", user)
		return nil
	}).
	OnError(func(user *User, ctx context.Context, err error) {
		zap.S().Errorf("Error: %+v", err)
	})

newTask := task.CopyFor(&User{Email: "[email protected]"})

//Example 4
next := task.Chain(New[User]())
next.
	Do(ValidateUser).
	Finally(func(user *User, ctx context.Context) error {
		err := ctx.Value("error").(error)
		if err != nil {
			zap.S().Errorf("Error: %+v", err)
		}

		return nil
	})

# Functions

IfElse runs the given function, and if it returns an error runs the next function.
No description provided by the author
No description provided by the author
Serial runs the given functions in order, passing the state to each function.

# Variables

No description provided by the author

# Structs

No description provided by the author