wq

package module
v0.0.0-...-83b05b1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2020 License: Apache-2.0 Imports: 5 Imported by: 0

README

wq, a basic Worker Queue in Go

GoDoc Build Status

Example

fn := func(key string) error { ... }
nb, err := wq.Queue(
    8, // number of workers
    fn, // payload function
    []string{"key1", ...},
)

Detailed usage

conf := wq.NewConfig(workers)
job, err := conf.NewJob(fn, tasks)
if err != nil {
	...
}
job.Print()
ok := job.Run() // ok is the number of successful tasks
...

About

This project was originally written in 2015 while playing with Go. It was a very simple worker/queue where the payload for each job will update state in a SQL database, later rewritten without the database/sql dependency.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoJob = errors.New("nothing to do")

ErrNoJob is the error used when there is nothing to do.

Functions

func MinInt

func MinInt(x, y int) int

MinInt computes the minimum of two values.

func Plural

func Plural(nb int) string

Plural displays 's' if value is more than 1.

func Queue

func Queue(workers int, fn Payload, tasks []string) (int, error)

Queue creates and runs a job queue.

This all-in-one function does not require defining elements separately.

Types

type Config

type Config struct {
	Workers         int  // number of workers
	Verbose         bool // verbose flag
	WaitBeforeStart bool // wait 3 seconds before starting
	ProgressBar     bool // display progress bar
}

Config describes the configuration of a job queue.

func NewConfig

func NewConfig(workers int) *Config

NewConfig creates a new configuration by default.

func (*Config) NewJob

func (conf *Config) NewJob(fn Payload, tasks []string) (*Job, error)

NewJob creates a new job.

type Job

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

Job defines a list of tasks to execute.

func (*Job) NewWorker

func (j *Job) NewWorker(ID int) *Worker

NewWorker creates a new Worker.

func (*Job) Print

func (j *Job) Print()

Print displays a job with potential confirmation.

func (*Job) Run

func (j *Job) Run() int

Run executes a job.

type Payload

type Payload func(string) error

Payload defines the payload function.

Payloads do not return value and rely on side effects (updating a database, printing values, etc.).

type Worker

type Worker struct {
	ID int // worker ID
	// contains filtered or unexported fields
}

Worker is a task executor.

func (*Worker) Launch

func (w *Worker) Launch(tasks <-chan string, done chan<- bool)

Launch launches a Worker.

Jump to

Keyboard shortcuts

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