You are here

function media_acquiadam_cron in Media: Acquia DAM 8

Same name and namespace in other branches
  1. 7 media_acquiadam.module \media_acquiadam_cron()

Implements hook_cron().

Refresh acquiadam metadata and sync to media entities.

File

./media_acquiadam.module, line 162
Integrates Drupal with Acquia DAM.

Code

function media_acquiadam_cron() {
  $cron_config = Drupal::configFactory()
    ->get('media_acquiadam.settings');
  $interval = $cron_config
    ->get('sync_interval');

  // 3600 seconds = 1 hour.
  $interval = !empty($interval) ? $interval : 3600;

  // Rate limit so we're not syncing every minute even if cron is configured to.
  $next_execution = Drupal::state()
    ->get('media_acquiadam.next_sync');
  $next_execution = !empty($next_execution) ? $next_execution : 0;
  $request_time = Drupal::time()
    ->getRequestTime();

  // -1 interval means run on every cron.
  $process_queues = $interval == -1 || $request_time >= $next_execution;
  if (!$process_queues) {
    return;
  }
  media_acquiadam_purge_expired_tokens();
  if ($cron_config
    ->get('notifications_sync')) {
    media_acquiadam_refresh_asset_sync_notifications_queue();
  }
  else {
    media_acquiadam_refresh_asset_sync_queue();
  }
  $total_queue_items = Drupal::queue('media_acquiadam_asset_refresh')
    ->numberOfItems();

  /** @var \Drupal\Core\Logger\LoggerChannelInterface $logger */
  $logger = \Drupal::service('logger.factory')
    ->get('media_acquiadam');
  $logger
    ->info('Total items in the queue: @items.', [
    '@items' => $total_queue_items,
  ]);
  Drupal::state()
    ->set('media_acquiadam.next_sync', $request_time + $interval);
}