You are here

public function BrightcoveClientQueueWorker::processItem in Brightcove Video Connect 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/QueueWorker/BrightcoveClientQueueWorker.php \Drupal\brightcove\Plugin\QueueWorker\BrightcoveClientQueueWorker::processItem()
  2. 8 src/Plugin/QueueWorker/BrightcoveClientQueueWorker.php \Drupal\brightcove\Plugin\QueueWorker\BrightcoveClientQueueWorker::processItem()

Works on a single queue item.

Parameters

mixed $data: The data that was passed to \Drupal\Core\Queue\QueueInterface::createItem() when the item was queued.

Throws

\Drupal\Core\Queue\RequeueException Processing is not yet finished. This will allow another process to claim the item immediately.

\Exception A QueueWorker plugin may throw an exception to indicate there was a problem. The cron process will log the exception, and leave the item in the queue to be processed again later.

\Drupal\Core\Queue\SuspendQueueException More specifically, a SuspendQueueException should be thrown when a QueueWorker plugin is aware that the problem will affect all subsequent workers of its queue. For example, a callback that makes HTTP requests may find that the remote server is not responding. The cron process will behave as with a normal Exception, and in addition will not attempt to process further items from the current item's queue during the current cron run.

Overrides QueueWorkerInterface::processItem

See also

\Drupal\Core\Cron::processQueues()

File

src/Plugin/QueueWorker/BrightcoveClientQueueWorker.php, line 118

Class

BrightcoveClientQueueWorker
Processes Entity Update Tasks for Client.

Namespace

Drupal\brightcove\Plugin\QueueWorker

Code

public function processItem($data) {
  $cms = BrightcoveUtil::getCmsApi($data);
  $items_per_page = 100;

  // Create queue item for each player.
  $pm = BrightcoveUtil::getPmApi($data);
  $player_list = $pm
    ->listPlayers();
  $players = [];
  if (!empty($player_list)) {
    $players = $player_list
      ->getItems() ?: [];
  }
  $player_entities = BrightcovePlayer::getList($data);
  foreach ($players as $player) {

    // Remove existing players from the list.
    unset($player_entities[$player
      ->getId()]);

    // Create queue item.
    $this->playerQueue
      ->createItem([
      'api_client_id' => $data,
      'player' => $player,
    ]);
  }

  // Remove non-existing players.
  foreach (array_keys($player_entities) as $player_id) {

    // Create queue item for deletion.
    $this->playerDeleteQueue
      ->createItem([
      'player_id' => $player_id,
    ]);
  }

  /** @var \Brightcove\Item\CustomFields $video_fields */

  // Create queue item for each custom field.
  $video_fields = $cms
    ->getVideoFields();
  $custom_fields = [];
  foreach ($video_fields
    ->getCustomFields() as $custom_field) {
    $custom_fields[] = $custom_field
      ->getId();

    // Create queue item.
    $this->customFieldQueue
      ->createItem([
      'api_client_id' => $data,
      'custom_field' => $custom_field,
    ]);
  }

  // Collect non-existing custom fields and delete them.
  $custom_field_entities = BrightcoveCustomField::loadMultipleByApiClient($data);
  foreach ($custom_field_entities as $custom_field_entity) {
    if (!in_array($custom_field_entity
      ->getCustomFieldId(), $custom_fields)) {
      $this->customFieldDeleteQueue
        ->createItem($custom_field_entity);
    }
  }

  // Create queue items for each video page.
  $video_count = $cms
    ->countVideos();
  $page = 0;
  while ($page * $items_per_page < $video_count) {
    $this->videoPageQueue
      ->createItem([
      'api_client_id' => $data,
      'page' => $page,
      'items_per_page' => $items_per_page,
    ]);
    $page++;
  }

  // Create queue items for each playlist page.
  $playlist_count = $cms
    ->countPlaylists();
  $page = 0;
  while ($page * $items_per_page < $playlist_count) {
    $this->playlistPageQueue
      ->createItem([
      'api_client_id' => $data,
      'page' => $page,
      'items_per_page' => $items_per_page,
    ]);
    $page++;
  }
}