You are here

function _brightcove_initiate_sync in Brightcove Video Connect 8

Same name and namespace in other branches
  1. 8.2 brightcove.module \_brightcove_initiate_sync()
  2. 3.x brightcove.module \_brightcove_initiate_sync()

Initiates a Brightcove-to-Drupal sync by adding API clients to the queue.

It does nothing if any of the five affected queues is non-empty.

Syncing from Brightcove to Drupal works as follows.

  • This function adds all the API Clients (registered in the Drupal site) to the brightcove_client_queue_worker.
  • BrightcoveClientQueueWorker::processItem() counts the videos and the playlists on that API Client, then adds pages of them to the brightcove_video_page_queue_worker and brightcove_playlist_page_queue_worker. All those pages may contain up to 100 items.
  • Both BrightcoveVideoPageQueueWorker::processItem() and BrightcovePlaylistPageQueueWorker::processItem() cycles through all the videos/playlists on that page, and add them to brightcove_video_queue_worker or brightcove_playlist_queue_worker, respectively.
  • Both BrightcoveVideoQueueWorker::processItem() and BrightcovePlaylistQueueWorker::processItem() process one video/playlist at a time. However, the latter may throw an exception if it encounters with a video which is not yet available on the Drupal side. In this case, the playlist is not removed from the queue with the hope that the affected video becomes available by the next time this playlist queue item is processed.

This process ensures that syncing from Brightcove to Drupal won't eat up all the server's resources, and those queues are run from cron, and it's even possible to run those queues from batches.

1 call to _brightcove_initiate_sync()
brightcove_cron in ./brightcove.module
Implements hook_cron().
1 string reference to '_brightcove_initiate_sync'
BrightcoveUtil::runStatusQueues in src/BrightcoveUtil.php
Runs specific status queues based on the given $type.

File

./brightcove.module, line 53
Brightcove module.

Code

function _brightcove_initiate_sync() {

  // Get queues.
  $client_queue = \Drupal::queue('brightcove_client_queue_worker');
  $player_queue = \Drupal::queue('brightcove_player_queue_worker');
  $player_delete_queue = \Drupal::queue('brightcove_player_delete_queue_worker');
  $custom_field_queue = \Drupal::queue('brightcove_custom_field_queue_worker');
  $custom_field_delete_queue = \Drupal::queue('brightcove_custom_field_delete_queue_worker');
  $video_page_queue = \Drupal::queue('brightcove_video_page_queue_worker');
  $video_queue = \Drupal::queue('brightcove_video_queue_worker');
  $video_delete_queue = \Drupal::queue('brightcove_video_delete_queue_worker');
  $text_track_queue = \Drupal::queue('brightcove_text_track_queue_worker');
  $text_track_queue_delete = \Drupal::queue('brightcove_text_track_delete_queue_worker');
  $playlist_page_queue = \Drupal::queue('brightcove_playlist_page_queue_worker');
  $playlist_queue = \Drupal::queue('brightcove_playlist_queue_worker');
  $playlist_delete_queue = \Drupal::queue('brightcove_playlist_delete_queue_worker');
  $subscriptions_queue = \Drupal::queue('brightcove_subscriptions_queue_worker');
  $subscription_delete_queue = \Drupal::queue('brightcove_subscription_delete_queue_worker');
  $subscription_queue = \Drupal::queue('brightcove_subscription_queue_worker');

  // Check whether the update/create queues are empty or not.
  $update_create_queues_empty = $client_queue
    ->numberOfItems() == 0 && $player_queue
    ->numberOfItems() == 0 && $custom_field_queue
    ->numberOfItems() == 0 && $player_delete_queue
    ->numberOfItems() == 0 && $custom_field_delete_queue
    ->numberOfItems() == 0 && $video_page_queue
    ->numberOfItems() == 0 && $video_queue
    ->numberOfItems() == 0 && $text_track_queue
    ->numberOfItems() == 0 && $text_track_queue_delete
    ->numberOfItems() == 0 && $playlist_page_queue
    ->numberOfItems() == 0 && $playlist_queue
    ->numberOfItems() == 0;

  // Run video deletion queue only if all of the other queues are empty too.
  if ($update_create_queues_empty && $video_delete_queue
    ->numberOfItems() == 0) {

    // Collect videos for the queue worker to check whether they are deleted
    // from Brightcove or not.
    $videos = Database::getConnection()
      ->select('brightcove_video', 'bv')
      ->fields('bv', [
      'bcvid',
      'api_client',
      'video_id',
    ])
      ->condition('video_id', NULL, 'IS NOT')
      ->execute();
    foreach ($videos as $video) {
      $video_delete_queue
        ->createItem($video);
    }
  }

  // Run playlist deletion queue only if all of the other queues are empty too.
  if ($update_create_queues_empty && $playlist_delete_queue
    ->numberOfItems() == 0) {

    // Collect playlists for the queue worker to check whether they are deleted
    // from Brightcove or not.
    $playlists = Database::getConnection()
      ->select('brightcove_playlist', 'bv')
      ->fields('bv', [
      'bcplid',
      'api_client',
      'playlist_id',
    ])
      ->condition('playlist_id', NULL, 'IS NOT')
      ->execute();
    foreach ($playlists as $playlist) {
      $playlist_delete_queue
        ->createItem($playlist);
    }
  }

  // Don't start new sync until all of the queues are not emptied.
  if ($update_create_queues_empty) {
    $brightcove_api_clients = BrightcoveAPIClient::loadMultiple();

    /** @var \Drupal\brightcove\Entity\BrightcoveAPIClient $api_client */
    foreach ($brightcove_api_clients as $api_client) {
      $client_queue
        ->createItem($api_client
        ->id());
    }
  }

  // Start Subscription queue workers.
  if ($update_create_queues_empty && $subscriptions_queue
    ->numberOfItems() == 0 && $subscription_queue
    ->numberOfItems() == 0 && $subscription_delete_queue
    ->numberOfItems() == 0) {

    // Check for new Subscriptions.

    /** @var \Drupal\brightcove\Entity\BrightcoveAPIClient[] $brightcove_api_clients */
    $brightcove_api_clients = BrightcoveAPIClient::loadMultiple();
    foreach ($brightcove_api_clients as $api_client) {
      $subscriptions_queue
        ->createItem($api_client
        ->id());
    }

    // Remove non-existing Subscriptions.
    $brightcove_subscriptions = BrightcoveSubscription::loadMultiple();
    foreach ($brightcove_subscriptions as $brightcove_subscription) {
      if (!empty($api_client = $brightcove_subscription
        ->getApiClient())) {
        $subscription_delete_queue
          ->createItem([
          'api_client_id' => $api_client
            ->id(),
          'subscription_id' => $brightcove_subscription
            ->getBcSid(),
        ]);
      }
    }
  }
}