gowq

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2025 License: MIT Imports: 4 Imported by: 0

README

goWQ

Build Go Report Card

A simple work queue manager to schedule jobs and then execute them on a defined pool of goroutines.

It can be used in two modes:

  • static: allows to create a job queue and then run it waiting for all jobs to complete;
  • dynamic: allows to run a job scheduler and then enqueue new jobs while the scheduler runs.

Examples

Static Queue Usage
wq := New[MyResult](2)

wq.Push(func(ctx context.Context) (MyResult, error) {
    // do something...
    return MyResult{}, nil
})
results, errors := wq.RunAll(context.TODO())
Dynamic Queue Manager
wq := New[MyResult](2)

go func(ctx context.Context) {
    wq.Start(ctx)
}(context.TODO())

wq.Schedule(func(ctx context.Context) (MyResult, error) {
    // do something...
    return nil
})

// Wait until all jobs have been completed.
_ := wq.Shutdown()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrQueueNotStarted error used whenever an action is required on a dynamic queue
	// that has not been started yet.
	ErrQueueNotStarted = fmt.Errorf("Start must be called before")
	// ErrJobFailed error is used to wrap errors provided by failing jobs.
	ErrJobFailed = fmt.Errorf("job failed")
)

Functions

This section is empty.

Types

type DynamicScheduler added in v0.4.0

type DynamicScheduler[Result JobResult] interface {
	Start(ctx context.Context)
	Schedule(job Job[Result])
	Shutdown() bool
}

type Job

type Job[T JobResult] func(context.Context) (T, error)

A Job is a simple function that should be executed in a limited set of routines.

type JobResult added in v0.5.0

type JobResult interface{}

type Queue added in v0.4.0

type Queue[Result JobResult] interface {
	GetResults() []Result
	GetErrors(flush bool) []error
	FlushErrors()
	DynamicScheduler[Result]
	StaticScheduler[Result]
}

func New added in v0.4.0

func New[Result JobResult](workers int) Queue[Result]

New creates a new WorkQueue instance to schedule jobs.

type StaticScheduler added in v0.4.0

type StaticScheduler[Result JobResult] interface {
	RunAll(context.Context) ([]Result, []error)
	Push(job Job[Result])
}

Jump to

Keyboard shortcuts

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