(mon monitor.CompositeMonitor)
| 177 | } |
| 178 | |
| 179 | func (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 | |
| 189 | loop: |
| 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 | |
| 231 | func (s *Sensor) processMonitoringResults(mon monitor.CompositeMonitor) error { |
| 232 | // A bit of code duplication to avoid starting a goroutine |
no test coverage detected