/jobq

Generic golang worker pool library

Primary LanguageGo

jobq

Drone Status Maintainability Test Coverage


jobq is a generic worker pool library with

  1. dynamic adjust worker number
  2. self-expiring job
  3. easy exported metrics

Install

go get github.com/honestbee/jobq

Run The Test

# under the jobq repo
export GO111MODULE=on
go mod vendor
go test -v -race ./...

Examples

basic usage

package main

import (
	"context"
        "time"

        "github.com/honestbee/jobq"
)

func main() {
        dispatcher := jobq.NewWorkerDispatcher()
        defer dispatcher.Stop()

        job := func(ctx context.Context) (interface{}, error) {
                time.Sleep(200 *time.Millisecond)
                return "success", nil
        }

        tracker := dispatcher.QueueFunc(context.Background(), job)

        payload, err := tracker.Result()
        status := tracker.Status()

        fmt.Printf("complete=%t\n", status.Complete)
        fmt.Printf("success=%t\n", status.Success)
        if err != nil {
                fmt.Printf("err=%s\n", err.Error())
        } else {
                fmt.Printf("payload=%s\n", payload)
        }

        // Output:
        // complete=true
        // success=true
        // payload=success
}

more examples

Contribution

please feel free to do the pull request !!!