2018-10-02 19:40:27 +00:00
|
|
|
package eventer
|
2018-09-19 19:56:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-09-30 03:03:49 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2018-09-26 14:42:02 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-09-19 19:56:50 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-09-30 03:03:49 +00:00
|
|
|
// DefaultSendEventTimeout is the timeout used when publishing events to consumers
|
2018-09-19 19:56:50 +00:00
|
|
|
DefaultSendEventTimeout = 2 * time.Second
|
2018-10-02 19:36:55 +00:00
|
|
|
|
|
|
|
// ConsumerGCInterval is the interval at which garbage collection of consumers
|
|
|
|
// occures
|
|
|
|
ConsumerGCInterval = time.Minute
|
2018-09-19 19:56:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Eventer is a utility to control broadcast of TaskEvents to multiple consumers.
|
2018-09-30 03:03:49 +00:00
|
|
|
// It also implements the TaskEvents func in the DriverPlugin interface so that
|
2018-09-19 19:56:50 +00:00
|
|
|
// it can be embedded in a implementing driver struct.
|
|
|
|
type Eventer struct {
|
|
|
|
|
|
|
|
// events is a channel were events to be broadcasted are sent
|
2018-10-02 19:36:55 +00:00
|
|
|
// This channel is never closed, because it's lifetime is tied to the
|
|
|
|
// life of the driver and closing creates some subtile race conditions
|
|
|
|
// between closing it and emitting events.
|
2018-09-26 14:42:02 +00:00
|
|
|
events chan *drivers.TaskEvent
|
2018-09-19 19:56:50 +00:00
|
|
|
|
2018-09-30 03:03:49 +00:00
|
|
|
// consumers is a slice of eventConsumers to broadcast events to.
|
|
|
|
// access is gaurded by consumersLock RWMutex
|
2018-10-02 19:36:55 +00:00
|
|
|
consumers []*eventConsumer
|
|
|
|
consumersLock sync.RWMutex
|
2018-09-19 19:56:50 +00:00
|
|
|
|
2018-09-30 03:03:49 +00:00
|
|
|
// ctx to allow control of event loop shutdown
|
|
|
|
ctx context.Context
|
|
|
|
|
|
|
|
logger hclog.Logger
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 19:36:55 +00:00
|
|
|
type eventConsumer struct {
|
|
|
|
timeout time.Duration
|
|
|
|
ctx context.Context
|
|
|
|
ch chan *drivers.TaskEvent
|
|
|
|
logger hclog.Logger
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:56:50 +00:00
|
|
|
// NewEventer returns an Eventer with a running event loop that can be stopped
|
|
|
|
// by closing the given stop channel
|
2018-09-30 03:03:49 +00:00
|
|
|
func NewEventer(ctx context.Context, logger hclog.Logger) *Eventer {
|
2018-09-19 19:56:50 +00:00
|
|
|
e := &Eventer{
|
2018-09-26 14:42:02 +00:00
|
|
|
events: make(chan *drivers.TaskEvent),
|
2018-09-30 03:03:49 +00:00
|
|
|
ctx: ctx,
|
|
|
|
logger: logger,
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
go e.eventLoop()
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// eventLoop is the main logic which pulls events from the channel and broadcasts
|
|
|
|
// them to all consumers
|
|
|
|
func (e *Eventer) eventLoop() {
|
|
|
|
for {
|
|
|
|
select {
|
2018-09-30 03:03:49 +00:00
|
|
|
case <-e.ctx.Done():
|
2018-10-03 16:08:26 +00:00
|
|
|
e.logger.Trace("task event loop shutdown")
|
2018-09-19 19:56:50 +00:00
|
|
|
return
|
|
|
|
case event := <-e.events:
|
2018-10-02 19:36:55 +00:00
|
|
|
e.iterateConsumers(event)
|
|
|
|
case <-time.After(ConsumerGCInterval):
|
|
|
|
e.gcConsumers()
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:08:26 +00:00
|
|
|
// iterateConsumers will iterate through all consumers and broadcast the event,
|
|
|
|
// cleaning up any consumers that have closed their context
|
2018-10-02 19:36:55 +00:00
|
|
|
func (e *Eventer) iterateConsumers(event *drivers.TaskEvent) {
|
|
|
|
e.consumersLock.Lock()
|
|
|
|
filtered := e.consumers[:0]
|
|
|
|
for _, consumer := range e.consumers {
|
|
|
|
select {
|
|
|
|
case <-time.After(consumer.timeout):
|
|
|
|
filtered = append(filtered, consumer)
|
|
|
|
e.logger.Warn("timeout sending event", "task_id", event.TaskID, "message", event.Message)
|
|
|
|
case <-consumer.ctx.Done():
|
|
|
|
// consumer context finished, filtering it out of loop
|
|
|
|
close(consumer.ch)
|
|
|
|
case consumer.ch <- event:
|
|
|
|
filtered = append(filtered, consumer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.consumers = filtered
|
|
|
|
e.consumersLock.Unlock()
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 19:36:55 +00:00
|
|
|
func (e *Eventer) gcConsumers() {
|
|
|
|
e.consumersLock.Lock()
|
|
|
|
filtered := e.consumers[:0]
|
|
|
|
for _, consumer := range e.consumers {
|
|
|
|
select {
|
|
|
|
case <-consumer.ctx.Done():
|
|
|
|
// consumer context finished, filtering it out of loop
|
|
|
|
default:
|
|
|
|
filtered = append(filtered, consumer)
|
|
|
|
}
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
2018-10-02 19:36:55 +00:00
|
|
|
e.consumers = filtered
|
|
|
|
e.consumersLock.Unlock()
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 03:03:49 +00:00
|
|
|
func (e *Eventer) newConsumer(ctx context.Context) *eventConsumer {
|
|
|
|
e.consumersLock.Lock()
|
|
|
|
defer e.consumersLock.Unlock()
|
2018-09-19 19:56:50 +00:00
|
|
|
|
2018-09-30 03:03:49 +00:00
|
|
|
consumer := &eventConsumer{
|
2018-09-26 14:42:02 +00:00
|
|
|
ch: make(chan *drivers.TaskEvent),
|
2018-09-19 19:56:50 +00:00
|
|
|
ctx: ctx,
|
|
|
|
timeout: DefaultSendEventTimeout,
|
2018-09-30 03:03:49 +00:00
|
|
|
logger: e.logger,
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
2018-09-30 03:03:49 +00:00
|
|
|
e.consumers = append(e.consumers, consumer)
|
2018-09-19 19:56:50 +00:00
|
|
|
|
2018-09-30 03:03:49 +00:00
|
|
|
return consumer
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TaskEvents is an implementation of the DriverPlugin.TaskEvents function
|
2018-09-26 14:42:02 +00:00
|
|
|
func (e *Eventer) TaskEvents(ctx context.Context) (<-chan *drivers.TaskEvent, error) {
|
2018-09-30 03:03:49 +00:00
|
|
|
consumer := e.newConsumer(ctx)
|
|
|
|
return consumer.ch, nil
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:56:50 +00:00
|
|
|
// EmitEvent can be used to broadcast a new event
|
2018-09-30 03:03:49 +00:00
|
|
|
func (e *Eventer) EmitEvent(event *drivers.TaskEvent) error {
|
2018-09-30 03:12:22 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-e.ctx.Done():
|
2018-10-02 19:36:55 +00:00
|
|
|
return e.ctx.Err()
|
2018-09-30 03:12:22 +00:00
|
|
|
case e.events <- event:
|
|
|
|
if e.logger.IsTrace() {
|
2018-10-02 19:36:55 +00:00
|
|
|
e.logger.Trace("emitting event", "event", event)
|
2018-09-30 03:12:22 +00:00
|
|
|
}
|
2018-09-30 03:03:49 +00:00
|
|
|
}
|
|
|
|
return nil
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|