Integrate NATS with event subsystem

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickael Laventure 2016-12-12 14:26:51 -08:00
parent 934940a96c
commit aa5ff88bbc
10 changed files with 165 additions and 68 deletions

View file

@ -3,8 +3,8 @@ package events
import (
"context"
"github.com/Sirupsen/logrus"
"github.com/docker/containerd/log"
"github.com/sirupsen/logrus"
)
var (
@ -13,13 +13,17 @@ var (
// Poster posts the event.
type Poster interface {
Post(event Event)
Post(ctx context.Context, event Event)
}
type posterKey struct{}
func WithPoster(ctx context.Context, poster Poster) context.Context {
return context.WithValue(ctx, posterKey{}, poster)
}
func GetPoster(ctx context.Context) Poster {
poster := ctx.Value(ctx)
poster := ctx.Value(posterKey{})
if poster == nil {
logger := log.G(ctx)
tx, _ := getTx(ctx)
@ -27,7 +31,7 @@ func GetPoster(ctx context.Context) Poster {
// likely means we don't have a configured event system. Just return
// the default poster, which merely logs events.
return posterFunc(func(event Event) {
return posterFunc(func(ctx context.Context, event Event) {
fields := logrus.Fields{"event": event}
if topic != "" {
@ -48,8 +52,8 @@ func GetPoster(ctx context.Context) Poster {
return poster.(Poster)
}
type posterFunc func(event Event)
type posterFunc func(ctx context.Context, event Event)
func (fn posterFunc) Post(event Event) {
fn(event)
func (fn posterFunc) Post(ctx context.Context, event Event) {
fn(ctx, event)
}