package module
Version: v0.0.0-...-6425d5a Latest Latest

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

Go to latest
Published: Sep 4, 2019 License: MIT Imports: 10 Imported by: 0



Build Status

A simple beanstalk client library to consume jobs written in go. Heavily inspired from the standard net/http package.


$ go get


package main

import (

func main() {
	mux := workers.NewWorkMux()

	mux.Handle("tube1", workers.HandlerFunc(func(job *workers.Job) {
		fmt.Println("deleting job:", job.ID, job.Tube)

	mux.Handle("tube2", workers.HandlerFunc(func(job *workers.Job) {

	workers.ConnectAndWork("tcp", "", mux)

Or if you would like to consume jobs only on the default tube:

package main

import (

func main() {
	workers.ConnectAndWork("tcp", "", workers.HandlerFunc(func(job *workers.Job) {
		fmt.Println("deleting job:", job.ID, job.Tube)

Job Handlers

Jobs are serviced each in their own goroutines. Jobs are handled in parallel as fast as they are reserved from the server.

You can handle jobs by providing an object implementing the Handler interface:

type Handler interface {

Or use the HandlerFunc adapter as seen in the examples above.

Stopping workers

The client will disconnect itself from the beanstalk server and return upon receiving a SIGINT or a SIGTERM signal, waiting for current jobs to be handled.



Package workers provides a client for the beanstalk protocol. See for the server.

mux := NewWorkMux()

mux.Handle("tube1", HandlerFunc(func(job *Job) {
	fmt.Printf("processing job %d with content %v\n", job.ID, job.Body)

mux.Handle("tube2", HandlerFunc(func(job *Job) {
	job.Release(0, 0)

ConnectAndWork("tcp", "localhost:11300", mux)




This section is empty.


View Source
var ErrClientHasQuit = errors.New("client has quit")

ErrClientHasQuit is returned by Client when it is quitting


func ConnectAndWork

func ConnectAndWork(network string, addr string, handler Handler) error

ConnectAndWork creates a client, connects to the beanstalk instance and reserves jobs to be processed by Handler.


type Client

type Client struct {
	Network string
	Addr    string
	Handler Handler
	// contains filtered or unexported fields

Client defines parameters for running an beanstalk client.

func (*Client) ConnectAndWork

func (c *Client) ConnectAndWork() error

ConnectAndWork connects on the c.Network and c.Addr and then calls Reserve to handle jobs on the beanstalk instance.

func (*Client) Reserve

func (c *Client) Reserve(conn io.ReadWriteCloser) error

Reserve accepts incoming jobs on the beanstalk.Conn conn, creating a new service goroutine for each. The service goroutines read the job and then call c.Handler to process them.

func (*Client) Stop

func (c *Client) Stop()

Stop stops reserving jobs and wait for current workers to finish their job.

type Handler

type Handler interface {

Handler defines a way for workers to handle jobs for a tube. Objects implementing the Handler interface can be registered to handle jobs for a particular tube.

type HandlerFunc

type HandlerFunc func(*Job)

HandlerFunc type is an adapter to allow the use of ordinary functions as Work handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.

func (HandlerFunc) Work

func (f HandlerFunc) Work(j *Job)

Work makes HandlerFunc implement the Handler interface.

type Job

type Job struct {
	ID   uint64
	Tube string
	Body []byte
	// contains filtered or unexported fields

Job represents a job received by a worker.

func NewJob

func NewJob(conn *beanstalk.Conn, tube string, id uint64, body []byte) *Job

NewJob creates a Job.

func (*Job) Bury

func (j *Job) Bury(pri uint32) error

Bury buries the current job. Bury puts the job into the "buried" state. Buried jobs are put into a FIFO linked list and will not be touched by the server again until a client kicks them manually.

func (*Job) Delete

func (j *Job) Delete() error

Delete deletes the current job. It removes the job from the server entirely.

func (*Job) Release

func (j *Job) Release(pri uint32, delay time.Duration) error

Release releases the current job. Release puts the reserved job back into the ready queue (and marks its state as ready) to be run by any client.

func (*Job) Stats

func (j *Job) Stats() (*JobStats, error)

Stats gives statistical information about the current job.

func (*Job) Touch

func (j *Job) Touch() error

Touch touches the current job. It allows the worker to request more time to work on the job.

type JobStats

type JobStats struct {
	Priority uint32
	Age      time.Duration
	TimeLeft time.Duration

JobStats represents statistical information about a job.

type WorkMux

type WorkMux struct {
	// contains filtered or unexported fields

WorkMux is a Beanstalkd Job multiplexer. It matches the tube of each incoming job against a list of registered tubes and calls the handler of that tube.

func NewWorkMux

func NewWorkMux() *WorkMux

NewWorkMux allocates and returns a new WorkMux.

func (*WorkMux) Handle

func (mux *WorkMux) Handle(tube string, handler Handler)

Handle registers the job handler for the given tube. If a handler already exists for tube, Handle panics.

func (*WorkMux) Handler

func (mux *WorkMux) Handler(tube string) Handler

Handler returns the handler to use for the given job. If there is no registered handler that applies to the job, Handler returns nil.

func (*WorkMux) Tubes

func (mux *WorkMux) Tubes() []string

Tubes returns a list of tubes handled by the WorkMux.

func (WorkMux) Work

func (mux WorkMux) Work(j *Job)

Work dispatches the job to the proper handler. Makes WorkMux Implements the Handler interface. Work panics if no handler is defined to handle the job.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL