Skip to content

mdouchement/workerpool

Repository files navigation

Workerpool

GoDoc Go Report Card License

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

Usage

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

License

MIT

Contributing

All PRs are welcome.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. 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 ./...