You are here

public function CleanerMysqlOptimizeEventSubscriber::optimizeMysql in Cleaner 8.2

Cleaner MySQL optimization.

File

src/EventSubscriber/CleanerMysqlOptimizeEventSubscriber.php, line 82

Class

CleanerMysqlOptimizeEventSubscriber
Class CleanerMysqlOptimizeEventSubscriber.

Namespace

Drupal\cleaner\EventSubscriber

Code

public function optimizeMysql() {
  $opt = $this->config
    ->get('cleaner_optimize_db');
  if ($opt) {

    // Get's the database driver name.
    $db_type = $this->database
      ->driver();

    // Make sure the db type hasn't changed.
    if ($db_type == 'mysql') {

      // Gathering tables list.
      $list = $this
        ->buildTablesList();
      if (!empty($list)) {

        // Run optimization timer.
        Timer::start('cleaner_db_optimization');

        // Perform optimization.
        $this
          ->optimizeIt(static::getOptimizationQuery($opt, $list));

        // Write a log about successful optimization into the watchdog.
        // Convert tables list into a comma-separated list.
        $list = implode(', ', $list);

        // Get the timers's results.
        $time = static::getTimerResult();
        $this->loggerChannel
          ->info("Optimized tables: @list. This required @time seconds.", [
          '@list' => $list,
          '@time' => $time,
        ]);
      }
      else {

        // Write a log about thing that optimization process is
        // no tables which can to be optimized.
        $this->loggerChannel
          ->info('There is no tables which can to be optimized.');
      }
    }
    else {

      // Write a log(error) about thing that optimization process
      // isn't allowed for non-MySQL databases into the watchdog.
      // Change log level to an error.
      $this->loggerChannel
        ->error("Database type (@db_type) not allowed to be optimized.", [
        '@db_type' => $db_type,
      ]);
    }
  }
}