events

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

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

Go to latest
Published: Jun 21, 2016 License: Apache-2.0 Imports: 7 Imported by: 0

README

docker-events

GoDoc Build Status Go Report Card License

A really small library with the intent to ease the use of Events method of engine-api.

Usage

It should be pretty straighforward to use :

import "events"

// […]

cli, err := client.NewEnvClient()
if err != nil {
    // Do something..
}

cxt, cancel := context.WithCancel(context.Background())
// Call cancel() to get out of the monitor

errChan := events.Monitor(ctx, cli, types.EventsOptions{}, func(event eventtypes.Message) {
    fmt.Printf("%v\n", event)
})

if err := <-errChan; err != nil {
    // Do something
}

It's also possible to do a little more advanced stuff using EventHandler :

import "events"

// […]

cli, err := client.NewEnvClient()
if err != nil {
    // Do something..
}

// Setup the event handler
eventHandler := events.NewHandler(events.ByAction)
eventHandler.Handle("create", func(m eventtypes.Message) {
    // Do something in case of create message
})

stoppedOrDead := func(m eventtypes.Message) {
    // Do something in case of stop or die message as it might be the
    // same way to react.
}

eventHandler.Handle("die", stoppedOrDead)
eventHandler.Handle("stop", stoppedOrDead)

// The other type of message will be discard.

// Filter the events we wams so receive
filters := filters.NewArgs()
filters.Add("type", "container")
options := types.EventsOptions{
    Filters: filters,
}

cxt, cancel := context.WithCancel(context.Background())
// Call cancel() to get out of the monitor

errChan := events.MonitorWithHandler(ctx, cli, options, eventHandler)

if err := <-errChan; err != nil {
    // Do something
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByAction

func ByAction(e eventtypes.Message) string

ByAction is a qualify function based on message action.

func ByType

func ByType(e eventtypes.Message) string

ByType is a qualify function based on message type.

func Monitor

func Monitor(ctx context.Context, cli client.SystemAPIClient, options types.EventsOptions, fun func(m eventtypes.Message)) chan error

Monitor subscribes to the docker events api using engine api and will execute the specified function on each message. It will pass the specified options to the underline method (i.e Events).

Example
cli, err := client.NewEnvClient()
if err != nil {
	// Do something..
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

errChan := Monitor(ctx, cli, types.EventsOptions{}, func(event eventtypes.Message) {
	fmt.Printf("%v\n", event)
})

if err := <-errChan; err != nil {
	// Do something
}
Output:

func MonitorWithHandler

func MonitorWithHandler(ctx context.Context, cli client.SystemAPIClient, options types.EventsOptions, handler *Handler) chan error

MonitorWithHandler subscribes to the docker events api using engine api and will pass the message to the specified Handler, that will take care of it. It will pass the specified options to the underline method (i.e Events).

Types

type Handler

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

Handler is a struct holding the handlers by keys, and the function to get the key from the message.

func NewHandler

func NewHandler(fun func(eventtypes.Message) string) *Handler

NewHandler creates an event handler using the specified function to qualify the message and to route it to the correct handler.

func (*Handler) Handle

func (w *Handler) Handle(key string, h func(eventtypes.Message))

Handle registers a function has handler for the specified key.

func (*Handler) Watch

func (w *Handler) Watch(c <-chan eventtypes.Message)

Watch ranges over the passed in event chan and processes the events based on the handlers created for a given action. To stop watching, close the event chan.

Jump to

Keyboard shortcuts

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