worker

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2022 License: MIT Imports: 5 Imported by: 0

README

Worker

Godoc Release Build

A Golang library that provides worker pools.

Usage

See *_test.go files.

Test

# Test everything:
make test

# For BDD development:
# It will open a new browser window. Make sure:
#   1. There is no errors on the terminal window.
#   2. There is no other open GoConvey page.
make test-ui

# Benchmarks
make test-benchmarks

Release

# Update and commit CHANGELOG.md first (i.e. git add CHANGELOG.md && git commit -m "v1.0.0").
# Set GIT_TAG using semver (i.e. GIT_TAG=v1.0.0)
make release GIT_TAG=

Contributing

See CONTRIBUTING.md

License

Licensed under The MIT License (MIT)
For the full copyright and license information, please view the LICENSE.txt file.

Documentation

Overview

Package worker provides functions for running tasks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Descriptor

type Descriptor struct {
	ID       string
	Metadata map[string]interface{}
}

Descriptor represents a descriptor.

type Job

type Job struct {
	Descriptor Descriptor
	Fn         func(ctx context.Context, args ...interface{}) (interface{}, error)
	Args       []interface{}
}

Job represents a worker job which executes the given function.

func (Job) Exec

func (j Job) Exec(ctx context.Context) Result

Exec executes the job function and returns it's result.

type Pool

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

Pool represents a worker pool which runs concurrent workers and jobs.

func NewPool

func NewPool(ctx context.Context, numberOfWorkers int, jobsBuffer int) (*Pool, error)

NewPool creates a new worker Pool instance by the given options.

func (*Pool) AddJob

func (pool *Pool) AddJob(job Job)

AddJob adds a new job to the worker pool.

func (*Pool) Results

func (pool *Pool) Results() <-chan Result

Results returns the results channel which is populated by job results.

func (*Pool) Run

func (pool *Pool) Run() error

Run runs the worker pool and waits for it to complete.

func (*Pool) Start

func (pool *Pool) Start() error

Start starts the worker pool but does not wait for it to complete.

func (*Pool) Stop

func (pool *Pool) Stop() error

Stop stops the worker pool.

type Result

type Result struct {
	Descriptor Descriptor
	Value      interface{}
	Error      error
}

Result represents a result.

type Worker

type Worker struct {
}

Worker represents a worker which runs jobs.

func (*Worker) Run

func (worker *Worker) Run(ctx context.Context, jobs <-chan Job, results chan<- Result)

Run runs the worker by the given context, job and result channels.

Jump to

Keyboard shortcuts

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