An open API service indexing awesome lists of open source software.

https://github.com/daanv2/go-tasks

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.
https://github.com/daanv2/go-tasks

concurrency go parallel-computing parallel-programming tasks

Last synced: 4 months ago
JSON representation

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.

Awesome Lists containing this project

README

        

# Go Tasks

![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/DaanV2/go-tasks)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/DaanV2/go-tasks)
[![🐹 Golang](https://github.com/DaanV2/go-tasks/actions/workflows/go-checks.yml/badge.svg)](https://github.com/DaanV2/go-tasks/actions/workflows/go-checks.yml)

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
```golang
//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
})

```