-
Notifications
You must be signed in to change notification settings - Fork 0
/
application-event-bus.go
124 lines (105 loc) · 3.3 KB
/
application-event-bus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package models
import (
"github.com/asaskevich/EventBus"
"github.com/wufe/polo/pkg/logging"
"github.com/wufe/polo/pkg/utils"
)
const (
ApplicationEventTypeNone ApplicationEventType = "none"
ApplicationEventTypeInitializationStarted ApplicationEventType = "initialization_started"
ApplicationEventTypeInitializationCompleted ApplicationEventType = "initialization_completed"
ApplicationEventTypeFetchStarted ApplicationEventType = "fetch_started"
ApplicationEventTypeFetchCompleted ApplicationEventType = "fetch_completed"
ApplicationEventTypeHotSwap ApplicationEventType = "hot_swap"
ApplicationEventTypeAutoStart ApplicationEventType = "auto_start"
ApplicationEventTypeSessionBuild ApplicationEventType = "session_build"
ApplicationEventTypeSessionBuildFailed ApplicationEventType = "session_build_failed"
ApplicationEventTypeSessionBuildSucceeded ApplicationEventType = "session_build_succeeded"
ApplicationEventTypeSessionCleaned ApplicationEventType = "session_cleaned"
eventsBuffer = 100
)
type BusEventType string
func (t BusEventType) String() string {
return string(t)
}
type ApplicationEventType string
func (t ApplicationEventType) String() string {
return string(t)
}
type ApplicationEvent struct {
EventType ApplicationEventType
Application *Application
EventPayload interface{}
}
type ApplicationEventBus struct {
utils.RWLocker
bus EventBus.Bus
ch chan ApplicationEvent
history []ApplicationEvent
log logging.Logger
}
func NewApplicationEventBus(mutexBuilder utils.MutexBuilder, logger logging.Logger) *ApplicationEventBus {
eventBus := &ApplicationEventBus{
RWLocker: mutexBuilder(),
bus: EventBus.New(),
ch: make(chan ApplicationEvent, eventsBuffer),
log: logger,
}
eventBus.start()
return eventBus
}
func (b *ApplicationEventBus) start() {
history := []ApplicationEvent{}
b.history = history
eventsCount := 0
b.bus.Subscribe("application", func(ev interface{}) {
if appEv, ok := ev.(ApplicationEvent); ok {
b.Lock()
defer b.Unlock()
history = append(history, appEv)
b.ch <- appEv
eventsCount++
// Hack to prevent saturation of the receiving channel
// if there are no listeners.
if eventsCount >= eventsBuffer/2 {
L:
for {
select {
case <-b.ch:
default:
break L
}
}
eventsCount = 0
}
}
})
}
func (b *ApplicationEventBus) GetChan() <-chan ApplicationEvent {
return b.ch
}
func (b *ApplicationEventBus) PublishEvent(eventType ApplicationEventType, application *Application, payloadObjects ...interface{}) {
b.log.Tracef("Publishing event %q", eventType)
var payload interface{} = nil
if len(payloadObjects) == 1 {
payload = payloadObjects
} else {
payload = payloadObjects
}
b.bus.Publish("application", ApplicationEvent{
EventType: eventType,
Application: application,
EventPayload: payload,
})
}
func (b *ApplicationEventBus) Close() {}
func (b *ApplicationEventBus) convertHistoryEntries(entries []interface{}) []ApplicationEvent {
applicationEvents := []ApplicationEvent{}
for _, rawEvent := range entries {
applicationEvent, ok := rawEvent.(ApplicationEvent)
if ok {
applicationEvents = append(applicationEvents, applicationEvent)
}
}
return applicationEvents
}