@@ -66,15 +66,18 @@ func newHandler() *eventHandler {
66
66
}
67
67
68
68
type eventHandler struct {
69
- eventLog []proto.Event
70
- logLock sync.Mutex
71
- cfg event.Config
69
+ eventLog []proto.Event
70
+ logLock sync.Mutex
71
+ applicationLogs []proto.Event
72
+ applicationLogsLock sync.Mutex
73
+ cfg event.Config
72
74
73
- iteration int
74
- state proto.State
75
- stateLock sync.Mutex
76
- eventChan chan * proto.Event
77
- listeners []* listener
75
+ iteration int
76
+ state proto.State
77
+ stateLock sync.Mutex
78
+ eventChan chan * proto.Event
79
+ eventListeners []* listener
80
+ applicationLogListeners []* listener
78
81
}
79
82
80
83
type listener struct {
@@ -96,6 +99,10 @@ func ForEachEvent(callback func(*proto.Event) error) error {
96
99
return handler .forEachEvent (callback )
97
100
}
98
101
102
+ func ForEachApplicationLog (callback func (* proto.Event ) error ) error {
103
+ return handler .forEachApplicationLog (callback )
104
+ }
105
+
99
106
func Handle (event * proto.Event ) error {
100
107
if event != nil {
101
108
handler .handle (event )
@@ -115,10 +122,10 @@ func (ev *eventHandler) getState() proto.State {
115
122
return state
116
123
}
117
124
118
- func (ev * eventHandler ) logEvent (event * proto.Event ) {
119
- ev . logLock .Lock ()
125
+ func (ev * eventHandler ) log (event * proto.Event , listeners * [] * listener , log * []proto. Event , lock sync. Locker ) {
126
+ lock .Lock ()
120
127
121
- for _ , listener := range ev . listeners {
128
+ for _ , listener := range * listeners {
122
129
if listener .closed {
123
130
continue
124
131
}
@@ -128,24 +135,32 @@ func (ev *eventHandler) logEvent(event *proto.Event) {
128
135
listener .closed = true
129
136
}
130
137
}
131
- ev . eventLog = append (ev . eventLog , * event )
138
+ * log = append (* log , * event )
132
139
133
- ev . logLock .Unlock ()
140
+ lock .Unlock ()
134
141
}
135
142
136
- func (ev * eventHandler ) forEachEvent (callback func (* proto.Event ) error ) error {
143
+ func (ev * eventHandler ) logEvent (event * proto.Event ) {
144
+ ev .log (event , & ev .eventListeners , & ev .eventLog , & ev .logLock )
145
+ }
146
+
147
+ func (ev * eventHandler ) logApplicationLog (event * proto.Event ) {
148
+ ev .log (event , & ev .applicationLogListeners , & ev .applicationLogs , & ev .applicationLogsLock )
149
+ }
150
+
151
+ func (ev * eventHandler ) forEach (listeners * []* listener , log * []proto.Event , lock sync.Locker , callback func (* proto.Event ) error ) error {
137
152
listener := & listener {
138
153
callback : callback ,
139
154
errors : make (chan error ),
140
155
}
141
156
142
- ev . logLock .Lock ()
157
+ lock .Lock ()
143
158
144
- oldEvents := make ([]proto.Event , len (ev . eventLog ))
145
- copy (oldEvents , ev . eventLog )
146
- ev . listeners = append (ev . listeners , listener )
159
+ oldEvents := make ([]proto.Event , len (* log ))
160
+ copy (oldEvents , * log )
161
+ * listeners = append (* listeners , listener )
147
162
148
- ev . logLock .Unlock ()
163
+ lock .Unlock ()
149
164
150
165
for i := range oldEvents {
151
166
if err := callback (& oldEvents [i ]); err != nil {
@@ -157,6 +172,14 @@ func (ev *eventHandler) forEachEvent(callback func(*proto.Event) error) error {
157
172
return <- listener .errors
158
173
}
159
174
175
+ func (ev * eventHandler ) forEachEvent (callback func (* proto.Event ) error ) error {
176
+ return ev .forEach (& ev .eventListeners , & ev .eventLog , & ev .logLock , callback )
177
+ }
178
+
179
+ func (ev * eventHandler ) forEachApplicationLog (callback func (* proto.Event ) error ) error {
180
+ return ev .forEach (& ev .applicationLogListeners , & ev .applicationLogs , & ev .applicationLogsLock , callback )
181
+ }
182
+
160
183
func emptyState (cfg event.Config ) proto.State {
161
184
builds := map [string ]string {}
162
185
for _ , p := range cfg .GetPipelines () {
@@ -365,6 +388,9 @@ func (ev *eventHandler) handleBuildSubtaskEvent(e *proto.BuildSubtaskEvent) {
365
388
366
389
func (ev * eventHandler ) handleExec (event * proto.Event ) {
367
390
switch e := event .GetEventType ().(type ) {
391
+ case * proto.Event_ApplicationLogEvent :
392
+ ev .logApplicationLog (event )
393
+ return
368
394
case * proto.Event_BuildSubtaskEvent :
369
395
be := e .BuildSubtaskEvent
370
396
ev .stateLock .Lock ()
0 commit comments