workhorse

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2020 License: Apache-2.0 Imports: 3 Imported by: 0

README

Workhorse

Build Status GoDoc License

A simple worker abstraction on top of errgroup with custom middlewares.

Examples

import (
	"context"
	"fmt"
	"sync/atomic"

	"github.com/bsm/workhorse"
)

func main() {
	// define root context
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	var count uint32

	// init a worker
	w := workhorse.New(ctx)

	// schedule task "one"
	w.Go("one", func(_ context.Context) error {
		for i := 0; i < 1000; i++ {
			atomic.AddUint32(&count, 1)
		}
		return nil
	})

	// schedule task "two"
	w.Go("two", func(_ context.Context) error {
		for i := 0; i < 1000; i++ {
			atomic.AddUint32(&count, 2)
		}
		return nil
	})

	// wait for both tasks to complete
	if err := w.Wait(); err != nil {
		panic(err)
	}

	fmt.Println(count)
	// Output:
	// 3000}

Documentation

Full documentation is available on GoDoc

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"
	"sync/atomic"

	"github.com/bsm/workhorse"
)

func main() {
	// define root context
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	var count uint32

	// init a worker
	w := workhorse.New(ctx)

	// schedule task "one"
	w.Go("one", func(_ context.Context) error {
		for i := 0; i < 1000; i++ {
			atomic.AddUint32(&count, 1)
		}
		return nil
	})

	// schedule task "two"
	w.Go("two", func(_ context.Context) error {
		for i := 0; i < 1000; i++ {
			atomic.AddUint32(&count, 2)
		}
		return nil
	})

	// wait for both tasks to complete
	if err := w.Wait(); err != nil {
		panic(err)
	}

	fmt.Println(count)
}
Output:

3000

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func TaskName

func TaskName(ctx context.Context) string

TaskName extracts the task name from the context.

Types

type Task

type Task func(context.Context) error

Task is a task function.

func Every

func Every(task Task, interval time.Duration) Task

Every applies a task periodically every interval until the first failure. Example:

w.Go("task", workhorse.Every(func(ctx context.Context) error {
	fmt.Println("still alive!")
	return nil
}, time.Minute))

func Instrument

func Instrument(task Task, tfn func(name string, runTime time.Duration, err error)) Task

Instrument allows to instrument tasks.

Example
package main

import (
	"context"
	"log"
	"time"

	"github.com/bsm/workhorse"
)

func main() {
	// implement instrumentation
	inst := func(name string, dur time.Duration, err error) {
		if err != nil {
			log.Printf("task %s failed with %v", name, err)
		} else {
			log.Printf("task %s finished in %v", name, dur)
		}
	}

	// a noop task, just waits for 1s
	task := func(ctx context.Context) error {
		select {
		case <-ctx.Done():
		case <-time.After(time.Second):
		}
		return nil
	}

	// init a worker
	w := workhorse.New(context.Background())

	// run instrumented task every 5s
	w.Go("task", workhorse.Every(
		workhorse.Instrument(task, inst),
		5*time.Second,
	))
	if err := w.Wait(); err != nil {
		panic(err)
	}
}
Output:

func Retry

func Retry(task Task, numRetries int, backoff time.Duration) Task

Retry retries a task on failures with a linear backoff. Set the numRetries to -1 to retry forever. Example:

w.Go("task", workhorse.Retry(func(ctx context.Context) error {
	return fmt.Errorf("instant failure!")
}, 4, time.Second))

type Worker

type Worker struct {
	// contains filtered or unexported fields
}

Worker runs jobs.

func New

func New(ctx context.Context) *Worker

New returns a Worker under a global ctx.

func (*Worker) Go

func (w *Worker) Go(name string, task func(ctx context.Context) error)

Go starts a named background task.

func (*Worker) Wait

func (w *Worker) Wait() error

Wait blocks and waits for jobs to complete and returns the first error (if any).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL