MCPcopy
hub / github.com/slimtoolkit/slim / runWithMonitor

Method runWithMonitor

pkg/app/sensor/controlled/controlled.go:179–229  ·  view source on GitHub ↗
(mon monitor.CompositeMonitor)

Source from the content-addressed store, hash-verified

177}
178
179func (s *Sensor) runWithMonitor(mon monitor.CompositeMonitor) error {
180 log.Debug("sensor: monitor.worker - waiting to stop monitoring...")
181 log.Debug("sensor: error collector - waiting for errors...")
182
183 ticker := time.NewTicker(time.Second * 5)
184 defer ticker.Stop()
185
186 // Only two ways out of this: either StopMonitor or ShutdownSensor.
187 stopCommandReceived := false
188
189loop:
190 for {
191 select {
192 case <-mon.Done():
193 break loop
194
195 case cmd := <-s.exe.Commands():
196 switch cmd.(type) {
197 case *command.StopMonitor:
198 stopCommandReceived = true
199 mon.Cancel()
200 break loop
201
202 case *command.ShutdownSensor:
203 mon.Cancel() // Dirty exit - abandoning the results.
204 return ErrPrematureShutdown
205
206 default:
207 log.Info("sensor: ignoring unknown or unexpected command => ", cmd)
208 } // eof: type switch
209
210 case err := <-mon.Errors():
211 log.WithError(err).Warn("sensor: non-critical monitor error condition")
212 s.exe.PubEvent(event.Error, monitor.NonCriticalError(err).Error())
213
214 case <-ticker.C:
215 s.exe.HookTargetAppRunning()
216 log.Debug(".")
217 } // eof: select
218 }
219
220 if !stopCommandReceived {
221 // Monitor can finish before the stop command is received.
222 // In such case, we have to await the explicit stop.
223 if cmd, ok := (<-s.exe.Commands()).(*command.StopMonitor); !ok {
224 return fmt.Errorf("sensor received unepxected command: %#+v", cmd)
225 }
226 }
227
228 return s.processMonitoringResults(mon)
229}
230
231func (s *Sensor) processMonitoringResults(mon monitor.CompositeMonitor) error {
232 // A bit of code duplication to avoid starting a goroutine

Callers 1

runMethod · 0.95

Calls 11

NonCriticalErrorFunction · 0.92
StopMethod · 0.65
DoneMethod · 0.65
CommandsMethod · 0.65
CancelMethod · 0.65
ErrorsMethod · 0.65
PubEventMethod · 0.65
HookTargetAppRunningMethod · 0.65
InfoMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected