You are here

function tweet_feed_cron in Tweet Feed 8.3

Same name and namespace in other branches
  1. 6 tweet_feed.module \tweet_feed_cron()
  2. 7.3 tweet_feed.module \tweet_feed_cron()
  3. 7 tweet_feed.module \tweet_feed_cron()
  4. 7.2 tweet_feed.module \tweet_feed_cron()

Implements hook_cron().

File

./tweet_feed.module, line 67
Contains tweet_feed.module.

Code

function tweet_feed_cron() {

  /** Get a list of all the available feeds. */
  $config = \Drupal::service('config.factory')
    ->getEditable('tweet_feed.twitter_feeds');
  $feeds = $config
    ->get('feeds');

  /** If we have configured feeds, then we iteratre through them. */
  if (0 && !empty($feeds)) {

    // Iterate through the feeds. We do all the feeds here instead of one at
    // a time like we did in the Drupal 7 module. Just makes more sense.
    foreach ($feeds as $feed) {

      // Get all of the tweets to be imported
      $tweets = $this
        ->tweet_feed_pull_data_from_feed($feed);

      // If we have no tweets, we can stop here.
      if (!is_array($tweets) || count($tweets) < 1) {
        continue;
      }

      /** Get the current tweet feed queue */

      /** @var QueueFactory $queue_factory */
      $queue_factory = \Drupal::service('queue');

      /** @var QueueInterface $queue */
      $queue = $queue_factory
        ->get('tweet_feed_queue');

      /** We are not doing updates, only inserts. So we only need to exclude existing */

      /** tweets. This is the only reason for that query. To keep queue size down. */
      foreach ($tweets as $key => $tweet) {
        if ($this
          ->tweet_feed_tweet_exists($tweet->id_str) === TRUE) {
          continue;
        }

        /** Insert the item into the queue */
        $item = new \stdClass();
        $item->tweet = $tweet;
        $item->feed = $feed;
        $queue
          ->createItem($item);
      }
      if ($queue
        ->numberOfItems() > 0) {

        // Run through items in the queue
        for ($i = 0; $i < $queue_size; $i++) {
          if ($item = $queue
            ->claimItem($i)) {
            $this
              ->tweet_feed_save_tweet($item->data['tweet'], $feed);
            $queue
              ->deleteItem($item);
          }
        }
      }
    }
  }

  /**
   * Determine if the cron is running. We do this by checking for a semaphore.
   *
   * @return bool $exists
   *   Returns TRUE if there is a cron semaphore item, FALSE if not.
   */
  function tweet_feed_is_cron_running() {
    $query = \Drupal::database()
      ->select('semaphore', 's');
    $query
      ->fields('s', [
      'name',
    ]);
    $query
      ->condition('s.name', 'cron', '=');
    $query
      ->orderBy('pr.owner', 'ASC');
    $rows = $query
      ->countQuery()
      ->fetchField();
    if (!empty($rows)) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }
}