You are here

protected function SensorRunner::runSensor in Monitoring 7

Same name and namespace in other branches
  1. 8 src/SensorRunner.php \Drupal\monitoring\SensorRunner::runSensor()

Run a single given sensor.

Parameters

SensorInfo $sensor_info: Sensor info

Return value

SensorResultInterface Sensor result.

Throws

\Drupal\monitoring\Sensor\DisabledSensorException Thrown if the passed sensor is not enabled.

See also

\Drupal\monitoring\Sensor\SensorInterface::runSensor()

1 call to SensorRunner::runSensor()
SensorRunner::runSensors in lib/Drupal/monitoring/SensorRunner.php
Runs the defined sensors.

File

lib/Drupal/monitoring/SensorRunner.php, line 176
Contains \Drupal\monitoring\SensorRunner.

Class

SensorRunner
Instantiate and run requested sensors.

Namespace

Drupal\monitoring

Code

protected function runSensor(SensorInfo $sensor_info) {
  $sensor = $this
    ->getSensorObject($sensor_info);

  // Check if sensor is enabled.
  if (!$sensor
    ->isEnabled()) {
    throw new DisabledSensorException(format_string('Sensor @sensor_name is not enabled and must not be run.', array(
      '@sensor_name' => $sensor_info
        ->getName(),
    )));
  }
  $result = $this
    ->getResultObject($sensor_info);

  // In case result is not yet cached run sensor.
  if (!$result
    ->isCached()) {
    timer_start($sensor_info
      ->getName());
    try {
      $sensor
        ->runSensor($result);
    } catch (\Exception $e) {

      // In case the sensor execution results in an exception, mark it as
      // critical and set the sensor status message.
      $result
        ->setStatus(SensorResultInterface::STATUS_CRITICAL);
      $result
        ->setMessage(get_class($e) . ': ' . $e
        ->getMessage());

      // Log the error to watchdog.
      watchdog_exception('monitoring_exception', $e);

      // @todo Improve logging by e.g. integrating with past or save the
      //   backtrace as part of the sensor verbose output.
    }
    $timer = timer_stop($sensor_info
      ->getName());
    $result
      ->setExecutionTime($timer['time']);

    // Capture verbose output if requested and if we are able to do so.
    if ($this->verbose && $sensor_info
      ->isExtendedInfo()) {
      $result
        ->setVerboseOutput($sensor
        ->resultVerbose($result));
    }
    try {
      $result
        ->compile();
    } catch (\Exception $e) {
      $result
        ->setStatus(SensorResultInterface::STATUS_CRITICAL);
      $result
        ->setMessage(get_class($e) . ': ' . $e
        ->getMessage());
    }
  }
  return $result;
}