event

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2015 License: MIT Imports: 4 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultEvent is the default event and is used by AddHandler, Trigger, AddQueue, Start and Stop.
	DefaultEvent = New()

	// ErrDone represents that a queue is finished.
	ErrDone = errors.New("queue is done")

	// ErrNotExist is passed to ErrorHandler if handler not exists.
	ErrNotExist = errors.New("handler not exist")
)

Functions

func AddHandler

func AddHandler(name string, queueName string, handler func(args ...interface{}) error) error

AddHandler is shorthand of the DefaultEvent.AddHandler.

func RegisterQueue

func RegisterQueue(name string, queue Queue) error

RegisterQueue is shorthand of the DefaultEvent.RegisterQueue.

func Start

func Start()

Start is shorthand of the DefaultEvent.Start.

func Stop

func Stop()

Stop is shorthand of the DefaultEvent.Stop.

func Trigger

func Trigger(name string, args ...interface{}) error

Trigger is shorthand of the DefaultEvent.Trigger.

Types

type Event added in v0.7.0

type Event struct {
	// ErrorHandler is the error handler.
	// If you want to use your own error handler, set ErrorHandler.
	ErrorHandler func(err interface{})
	// contains filtered or unexported fields
}

Event represents an Event.

func New added in v0.7.0

func New() *Event

New returns a new Event.

func (*Event) AddHandler added in v0.7.0

func (e *Event) AddHandler(name string, queueName string, handler func(args ...interface{}) error) error

AddHandler adds handlers that related to name and queue. The name is an event name such as "log.error" that will be used for Trigger. The queueName is a name of queue registered by RegisterQueue in advance. If you add handler by name that has already been added, handler will associated to that name additionally. If queue of queueName still hasn't been registered, it returns error.

func (*Event) RegisterQueue added in v0.7.0

func (e *Event) RegisterQueue(name string, queue Queue) error

RegisterQueue makes a background queue available by the provided name. If queue is already registerd or if queue nil, it panics.

func (*Event) SetWorkersPerQueue added in v0.7.0

func (e *Event) SetWorkersPerQueue(n int)

SetWorkersPerQueue sets the number of workers per queue. It must be called before Start calls.

func (*Event) Start added in v0.7.0

func (e *Event) Start()

Start starts background event workers. By default, workers per queue is 1. To set the workers per queue, use SetWorkersPerQueue before Start calls.

func (*Event) Stop added in v0.7.0

func (e *Event) Stop()

Stop wait for all workers to complete.

func (*Event) Trigger added in v0.7.0

func (e *Event) Trigger(name string, args ...interface{}) error

Trigger emits the event. The name is an event name. It must be added in advance using AddHandler. If Trigger called by not added name, it returns error. If args are given, they will be passed to handlers added by AddHandler.

type Queue

type Queue interface {
	// New returns a new Queue to launch the workers.
	// You can use an argument n as a hint when you create a new queue.
	// n is the number of workers per queue.
	New(n int) Queue

	// Enqueue add data to the queue.
	Enqueue(data string) error

	// Dequeue returns the data that fetch from the queue.
	// It will return ErrDone as err when Stop is called.
	Dequeue() (data string, err error)

	// Stop wait for Enqueue and/or Dequeue to complete then will stop a queue.
	Stop()
}

Queue is the interface that must be implemeted by background event queue.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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