Asynchronous in-memory workers for Go based projects.
It is based on workerpool, a lightweight workers solution, and tomb that handles clean goroutine tracking and termination.
This project aimed to provide the following points:
- Dynamic concurrency (expand and shrink the number of workers)
- Trackable background jobs
- Cancelable jobs
- Jobs' status
go get github.com/mdouchement/workerpool
package main
import (
"fmt"
"github.com/mdouchement/workerpool"
)
func main() {
// It is not mandatory to implement all *Func.
job := &workerpool.Job{
// Triggered when the job's status change
OnStatusChangeFunc: func(j *workerpool.Job) error {
fmt.Println("Status changed to:", j.Status())
return nil
},
// Before starting job
BeforeFunc: func(j *workerpool.Job) error {
fmt.Println("Before starting job")
return nil
},
// Task to perform
ActionFunc: func(j *workerpool.Job) error {
fmt.Println("Starting job")
return nil
},
// When job is completed
AfterFunc: func(j *workerpool.Job) error {
fmt.Println("After job")
return nil
},
// Triggered when workerpool.Cancel("job_id")
// You can use j.Context() to forward cancellation signal.
CancelFunc: func(j *workerpool.Job) error {
fmt.Println("Execute cleanup function")
return nil
},
// Triggered when an error or panic occurred.
ErrHandler: func(j *workerpool.Job, err error, panic bool) {
// Do something with the error (and j.Context()).
},
}
id := workerpool.Send(job)
fmt.Println("Job id:", id)
}
MIT
All PRs are welcome.
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request
As possible, run the following commands to format and lint the code:
# Format
find . -name '*.go' -not -path './vendor*' -exec gofmt -s -w {} \;
# Lint
gometalinter --config=gometalinter.json ./...