https://github.com/plexsysio/taskmanager
Async Task manager
https://github.com/plexsysio/taskmanager
Last synced: 5 days ago
JSON representation
Async Task manager
- Host: GitHub
- URL: https://github.com/plexsysio/taskmanager
- Owner: plexsysio
- License: bsd-3-clause
- Created: 2021-07-19T18:32:07.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-12-20T12:37:47.000Z (about 4 years ago)
- Last Synced: 2023-07-27T22:28:42.432Z (over 2 years ago)
- Language: Go
- Size: 38.1 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# taskmanager [](https://github.com/plexsysio/taskmanager/actions) [](https://pkg.go.dev/github.com/plexsysio/taskmanager) [](https://coveralls.io/github/plexsysio/taskmanager?branch=main)
Async task manager. Tasks can easily be customized and executed asynchronously on
the next available worker.
The manager keeps workers ready to multiplex tasks. The maximum no. of workers can
be configured.
This package was mainly created to abstract all async functionality from the app. It
provides a consistent context interface to manage routine lifecycle from a single
place.
## Install
`taskmanager` works like a regular Go module:
```
> go get github.com/plexsysio/taskmanager
```
## Usage
```
import "github.com/plexsysio/taskmanager"
type exampleTask struct {}
func (e *exampleTask) Name() string {
return "exampleTask"
}
func (e *exampleTask) Execute(ctx context.Context) error {
for {
select {
case <-ctx.Done():
// taskmanager stopped
return nil
default:
// Do work. For long running tasks use ctx or move to next iteration
}
}
}
func main() {
tm := taskmanager.New(1, 100, time.Second*15)
t := &exampleTask{}
sched, err := tm.Go(t)
if err != nil {
fmt.Println(err)
return
}
// Task scheduled
<-sched
```
Closures can also be scheduled
```
fSched, err := tm.GoFunc(func(ctx context.Context) error {
for {
select {
case <-ctx.Done():
//taskmanager stopped
return nil
default:
// Do work
}
}
})
if err != nil {
fmt.Println(err)
return
}
// Stop will wait for all routines to stop. Context can be passed here to
// ensure timeout in Stop
ctx, _ := context.WithTimeout(time.Second)
err = tm.Stop(ctx)
if err != nil {
fmt.Printf("failed stopping %s\n", err.Error())
}
}
```