You are here

function ip2country_cron in IP-based Determination of a Visitor's Country 8

Same name and namespace in other branches
  1. 6 ip2country.module \ip2country_cron()
  2. 7 ip2country.module \ip2country_cron()

Implements hook_cron().

Updates the IP to Country database automatically on a periodic basis. Default period is 1 week.

File

./ip2country.module, line 55
Determination of user's Country based on IP.

Code

function ip2country_cron() {
  $ip2country_config = \Drupal::config('ip2country.settings');
  $rir = $ip2country_config
    ->get('rir');
  $md5_checksum = $ip2country_config
    ->get('md5_checksum');
  $batch_size = $ip2country_config
    ->get('batch_size');

  // Automatic database updates are disabled when $update_interval == 0.
  $update_interval = $ip2country_config
    ->get('update_interval');
  if ($update_interval && \Drupal::state()
    ->get('ip2country_last_update') <= \Drupal::time()
    ->getRequestTime() - $update_interval) {
    $status = \Drupal::service('ip2country.manager')
      ->updateDatabase($rir, $md5_checksum, $batch_size);

    // Log to watchdog if requested.
    if ($ip2country_config
      ->get('watchdog')) {
      if ($status != FALSE) {

        // Success.
        \Drupal::logger('ip2country')
          ->notice('Database updated from @registry server. Table contains @rows rows.', [
          '@registry' => mb_strtoupper($ip2country_config
            ->get('rir')),
          '@rows' => $status,
        ]);
      }
      else {

        // Failure.
        \Drupal::logger('ip2country')
          ->warning('Database update from @registry server FAILED.', [
          '@registry' => mb_strtoupper($ip2country_config
            ->get('rir')),
        ]);
      }
    }
  }
}