You are here

protected function SensorRunner::runSensor in Monitoring 8

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

Run a single given sensor.

Parameters

\Drupal\monitoring\Entity\SensorConfig $sensor_config: Sensor config

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 src/SensorRunner.php
Runs the defined sensors.

File

src/SensorRunner.php, line 162
Contains \Drupal\monitoring\SensorRunner.

Class

SensorRunner
Instantiate and run requested sensors.

Namespace

Drupal\monitoring

Code

protected function runSensor(SensorConfig $sensor_config) {
  $plugin = $sensor_config
    ->getPlugin();

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

  // In case result is not yet cached run sensor.
  if (!$result
    ->isCached()) {
    Timer::start($sensor_config
      ->id());
    try {
      $plugin
        ->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_config
      ->id());
    $result
      ->setExecutionTime($timer['time']);

    // Capture verbose output if requested and if we are able to do so.
    try {
      if ($this->verbose && $sensor_config
        ->isExtendedInfo()) {

        /** @var \Drupal\monitoring\SensorPlugin\ExtendedInfoSensorPluginInterface $plugin */
        $result
          ->setVerboseOutput($plugin
          ->resultVerbose($result));
      }
    } catch (\Exception $e) {
      $result
        ->setVerboseOutput(array(
        '#markup' => $e
          ->getMessage(),
      ));
    }
    try {
      $result
        ->compile();
    } catch (\Exception $e) {
      $result
        ->setStatus(SensorResultInterface::STATUS_CRITICAL);
      $result
        ->setMessage(get_class($e) . ': ' . $e
        ->getMessage());
    }
  }
  return $result;
}