You are here

public function UltimateCronDatabaseLogger::cleanupJob in Ultimate Cron 7.2

Cleanup logs for a single job.

1 call to UltimateCronDatabaseLogger::cleanupJob()
UltimateCronDatabaseLogger::cleanup in plugins/ultimate_cron/logger/database.class.php
Cleanup logs.

File

plugins/ultimate_cron/logger/database.class.php, line 79
Database logger for Ultimate Cron.

Class

UltimateCronDatabaseLogger
Class for using database as log storage.

Code

public function cleanupJob($job) {
  $settings = $job
    ->getSettings('logger');
  switch ($settings['method']) {
    case ULTIMATE_CRON_DATABASE_LOGGER_CLEANUP_METHOD_DISABLED:
      return 0;
    case ULTIMATE_CRON_DATABASE_LOGGER_CLEANUP_METHOD_EXPIRE:
      $expire = $settings['expire'];

      // Let's not delete more than ONE BILLION log entries :-o.
      $max = 10000000000;
      $chunk = 100;
      break;
    case ULTIMATE_CRON_DATABASE_LOGGER_CLEANUP_METHOD_RETAIN:
      $expire = 0;
      $max = db_query("SELECT COUNT(lid) FROM {ultimate_cron_log} WHERE name = :name", array(
        ':name' => $job->name,
      ))
        ->fetchField();
      $max -= $settings['retain'];
      if ($max <= 0) {
        return 0;
      }
      $chunk = min($max, 100);
      break;
    default:
      watchdog('ultimate_cron', 'Invalid cleanup method: @method', array(
        '@method' => $settings['method'],
      ));
      return 0;
  }

  // Chunked delete.
  $count = 0;
  do {
    $lids = db_select('ultimate_cron_log', 'l')
      ->fields('l', array(
      'lid',
    ))
      ->condition('l.name', $job->name)
      ->condition('l.start_time', microtime(TRUE) - $expire, '<')
      ->range(0, $chunk)
      ->orderBy('l.start_time', 'ASC')
      ->orderBy('l.end_time', 'ASC')
      ->execute()
      ->fetchAll(PDO::FETCH_COLUMN);
    if ($lids) {
      $count += count($lids);
      $max -= count($lids);
      $chunk = min($max, 100);
      db_delete('ultimate_cron_log')
        ->condition('lid', $lids, 'IN')
        ->execute();
    }
  } while ($lids && $max > 0);
  return $count;
}