Server Event Optimizations (#116)

This commit is contained in:
Matthew Penner
2022-01-17 20:23:29 -07:00
committed by GitHub
parent 521cc2aef2
commit 649dc9663e
18 changed files with 551 additions and 282 deletions

View File

@@ -342,10 +342,10 @@ func (e *Environment) followOutput() error {
func (e *Environment) scanOutput(reader io.ReadCloser) {
defer reader.Close()
events := e.Events()
if err := system.ScanReader(reader, func(line string) {
events.Publish(environment.ConsoleOutputEvent, line)
if err := system.ScanReader(reader, func(v []byte) {
e.logCallbackMx.Lock()
defer e.logCallbackMx.Unlock()
e.logCallback(v)
}); err != nil && err != io.EOF {
log.WithField("error", err).WithField("container_id", e.Id).Warn("error processing scanner line in console output")
return

View File

@@ -49,7 +49,10 @@ type Environment struct {
// Holds the stats stream used by the polling commands so that we can easily close it out.
stats io.ReadCloser
emitter *events.EventBus
emitter *events.Bus
logCallbackMx sync.Mutex
logCallback func([]byte)
// Tracks the environment state.
st *system.AtomicString
@@ -100,9 +103,9 @@ func (e *Environment) IsAttached() bool {
return e.stream != nil
}
func (e *Environment) Events() *events.EventBus {
func (e *Environment) Events() *events.Bus {
e.eventMu.Do(func() {
e.emitter = events.New()
e.emitter = events.NewBus()
})
return e.emitter
@@ -214,3 +217,10 @@ func (e *Environment) SetState(state string) {
e.Events().Publish(environment.StateChangeEvent, state)
}
}
func (e *Environment) SetLogCallback(f func([]byte)) {
e.logCallbackMx.Lock()
defer e.logCallbackMx.Unlock()
e.logCallback = f
}

View File

@@ -90,11 +90,7 @@ func (e *Environment) pollResources(ctx context.Context) error {
st.Network.TxBytes += nw.TxBytes
}
if b, err := json.Marshal(st); err != nil {
e.log().WithField("error", err).Warn("error while marshaling stats object for environment")
} else {
e.Events().Publish(environment.ResourceEvent, string(b))
}
e.Events().Publish(environment.ResourceEvent, st)
}
}
}