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)
}
}
}

View File

@@ -8,7 +8,6 @@ import (
)
const (
ConsoleOutputEvent = "console output"
StateChangeEvent = "state change"
ResourceEvent = "resources"
DockerImagePullStarted = "docker image pull started"
@@ -35,7 +34,7 @@ type ProcessEnvironment interface {
// Returns an event emitter instance that can be hooked into to listen for different
// events that are fired by the environment. This should not allow someone to publish
// events, only subscribe to them.
Events() *events.EventBus
Events() *events.Bus
// Determines if the server instance exists. For example, in a docker environment
// this should confirm that the container is created and in a bootable state. In
@@ -108,4 +107,7 @@ type ProcessEnvironment interface {
// Uptime returns the current environment uptime in milliseconds. This is
// the time that has passed since it was last started.
Uptime(ctx context.Context) (int64, error)
// SetLogCallback sets the callback that the container's log output will be passed to.
SetLogCallback(func([]byte))
}