modulepackage
0.0.0-20160930120339-4a83cc6dc9d5
Repository: https://github.com/vdemeester/docker-events.git
Documentation: pkg.go.dev
# README
docker-events
With moving event stream processing to engine api merged in docker/docker, this project is obsolete and won't work with the latest docker/docker/client api.
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
}
# Functions
ByAction is a qualify function based on message action.
ByType is a qualify function based on message type.
Monitor subscribes to the docker events api using engine api and will execute the specified function on each message.
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.
NewHandler creates an event handler using the specified function to qualify the message and to route it to the correct handler.