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

31
events/nats.go Normal file
View File

@@ -0,0 +1,31 @@
package events
import (
"context"
"strings"
"github.com/docker/containerd/log"
nats "github.com/nats-io/go-nats"
)
type natsPoster struct {
nec *nats.EncodedConn
}
func GetNATSPoster(nec *nats.EncodedConn) Poster {
return &natsPoster{nec}
}
func (p *natsPoster) Post(ctx context.Context, e Event) {
subject := strings.Replace(log.GetModulePath(ctx), "/", ".", -1)
topic := getTopic(ctx)
if topic != "" {
subject = strings.Join([]string{subject, topic}, ".")
}
if subject == "" {
log.GetLogger(ctx).WithField("event", e).Warn("unable to post event, subject is empty")
}
p.nec.Publish(subject, e)
}