github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/compute/sensors/logging_sensor.go (about) 1 package sensors 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/filecoin-project/bacalhau/pkg/model" 8 "github.com/rs/zerolog/log" 9 ) 10 11 type LoggingSensorParams struct { 12 InfoProvider model.DebugInfoProvider 13 Interval time.Duration 14 } 15 16 // LoggingSensor is a sensor that periodically logs the debug info 17 type LoggingSensor struct { 18 infoProvider model.DebugInfoProvider 19 interval time.Duration 20 } 21 22 // NewLoggingSensor create a new LoggingSensor from LoggingSensorParams 23 func NewLoggingSensor(params LoggingSensorParams) *LoggingSensor { 24 return &LoggingSensor{ 25 infoProvider: params.InfoProvider, 26 interval: params.Interval, 27 } 28 } 29 30 func (s LoggingSensor) Start(ctx context.Context) { 31 log.Ctx(ctx).Debug().Msgf("starting new logging sensor with interval %s", s.interval) 32 ticker := time.NewTicker(s.interval) 33 34 for { 35 select { 36 case <-ticker.C: 37 s.sense(ctx) 38 case <-ctx.Done(): 39 ticker.Stop() 40 return 41 } 42 } 43 } 44 45 func (s LoggingSensor) sense(ctx context.Context) { 46 debugInfo, err := s.infoProvider.GetDebugInfo() 47 if err != nil { 48 log.Ctx(ctx).Err(err).Msg("failed to marshal execution summaries") 49 } else { 50 log.Ctx(ctx).Info().Msgf("%s: %s", debugInfo.Component, debugInfo.Info) 51 } 52 }