jobber

package module
v0.0.0-...-c5079ef Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2018 License: Apache-2.0 Imports: 7 Imported by: 0

README

jobber

Little job queue thing for golang

A very basic example is provided in main

Overview

At a high level, you create a single Boss, add named Workers to the Boss, and add Jobs to the Workers.

Boss

The Boss is the central controller for any / all workers. You can have as many workers as you want under a single boss.

Worker

Worker is responsible for completing a specific type of work. The general idea is that workers have a unique name within a given Boss and they only do one type of job. You can, of course, do whatever you want.

The default Worker is called PitDroid and can be used out of the box or as a prototype for your own worker implementation.

Job

A Job can be any unit of work you wish to executed by a Worker. It's interface is designed to be minimal. The output of Process() is directly passed to the channel returned by RespondTo(), unless the Worker has been terminated in which case it is recommended an standard error of some sort be passed in.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLogger

func SetLogger(l Logger)

Types

type Boss

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

Boss controls the life of the workers

func NewBoss

func NewBoss() *Boss

NewBoss will create a new Boss with a background context

func NewBossWithContext

func NewBossWithContext(ctx context.Context) *Boss

NewBossWithContext will create a new Boss with a context of your creation

func (*Boss) AddJob

func (b *Boss) AddJob(workerName string, j Job) (err error)

AddWork will push a new job to a worker's queue

func (*Boss) HasWorker

func (b *Boss) HasWorker(name string) bool

func (*Boss) HireWorker

func (b *Boss) HireWorker(name string, queueLength int) error

HireWorker will attempt to hire a new worker using the specified HiringAgency and add them to the job pool.

func (*Boss) PlaceWorker

func (b *Boss) PlaceWorker(worker Worker) (err error)

PlaceWorker will attempt to add a hired worker to the job pool, if one doesn't already exist with that name

func (*Boss) ScaleDownWorker

func (b *Boss) ScaleDownWorker(workerName string) (err error)

ScaleDownWorker will tell a worker to finish up their queue then remove them

func (*Boss) Shutdown

func (b *Boss) Shutdown()

Shutdown will attempt to gracefully shutdown, completing all currently queued jobs but no longer accepting new ones

func (*Boss) Shutdowned

func (b *Boss) Shutdowned() bool

Shutdowned will return true if the boss has been told to shut down or terminate

func (*Boss) Terminate

func (b *Boss) Terminate()

Terminate will immediately fire all workers and shut down the boss

func (*Boss) TerminateWorker

func (b *Boss) TerminateWorker(workerName string) (err error)

TerminateWorker will remove the worker immediately, effectively cancelling all queued work.

func (*Boss) Worker

func (b *Boss) Worker(name string) (worker Worker)

Worker will attempt to return to you a worker by name

type HR

type HR chan<- Worker

HR is where workers are sent when they are done and should be removed from the Boss

type HiringAgencyFunc

type HiringAgencyFunc func(name string, queueLength int) Worker
var HiringAgency HiringAgencyFunc = NewPitDroid

HiringAgency allows you to create your own worker hiring function in case you don't like PitDroids.

type Job

type Job interface {
	// Process must contain whatever logic is needed to perform the job, returning whatever error is generated while
	// processing (if any)
	Process() error
	// RespondTo must be passed whatever output came from Process()
	RespondTo() chan<- error
}

Job represents any unit of work that you'd like to task a Worker with. Any context handling should be done in your Process() implementation.

type Logger

type Logger interface {
	Print(...interface{})
	Printf(string, ...interface{})
}

type PitDroid

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

PitDroids are simple workers that will do as instructed.

func (*PitDroid) AddJob

func (w *PitDroid) AddJob(j Job) (err error)

AddJob will append a job to this worker's queue

func (*PitDroid) Length

func (w *PitDroid) Length() int

Length returns the current number of items this worker has in its queue

func (*PitDroid) Name

func (w *PitDroid) Name() string

Name returns the name of this worker

func (*PitDroid) ScaleDown

func (w *PitDroid) ScaleDown(hr HR)

ScaleDown will tell this worker to stop accepting new jobs, complete all jobs left in its queue, then send itself to HR

func (*PitDroid) Terminate

func (w *PitDroid) Terminate(hr HR)

Terminate will tell this worker to stop accepting new jobs, flush all current jobs from its queue, then send itself to HR

type Worker

type Worker interface {
	// Name must return the name of worker.  This must be unique across all workers managed by the boss
	Name() string
	// Length must return the size of the current queue of work for this worker.
	Length() int
	// AddJob must attempt to add a new job to the worker's queue, failing if the worker has been told to stop
	AddJob(Job) error
	// ScaleDown must mark the worker as stopped, process any and all jobs remaining in it's queue, and then finally
	// send itself to HR
	ScaleDown(HR)
	// Terminate must send an error message to all remaining jobs in this worker's queue, then send itself to HR.
	Terminate(HR)
}

func NewPitDroid

func NewPitDroid(name string, queueLength int) Worker

NewPitDroid will return to you a new PitDroid, the default worker prototype for jobber

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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