<commit_before>##
# Background job to subscribe a user to a feed and, optionally, put the feed in a folder.
#
# Its perform method will be invoked from a Resque worker.

class SubscribeUserJob
  @queue = :update_feeds

  ##
  # Subscribe a user to a feed and optionally put the feed in a folder
  #
  # Receives as arguments:
  # - id of the user
  # - url of the feed
  # - id of the folder. It must be owned by the user. If a nil is passed, ignore it
  # - boolean indicating whether the subscription is part of an OPML import process
  #
  # If requested, the data_import of the user is updated so that the user can see the import progress.
  #
  # This method is intended to be invoked from Resque, which means it is performed in the background.

  def self.perform(user_id, feed_url, folder_id, running_data_import)
    # Check if the user actually exists
    if !User.exists? user_id
      Rails.logger.error "Trying to add subscription to non-existing user @#{user_id}, aborting job"
      return
    end
    user = User.find user_id

    # Check if the folder actually exists and is owned by the user
    if folder_id.present?
      if !Folder.exists? folder_id
        Rails.logger.error "Trying to add subscription in non-existing folder @#{folder_id}, aborting job"
        return
      end
      folder = Folder.find folder_id
      if !user.folders.include? folder
        Rails.logger.error "Trying to add subscription in folder #{folder.id} - #{folder.title} which is not owned by user #{user.id} - #{user.email}, aborting job"
        return
      end
    end

    # Check that user has a data_import with status RUNNING if requested to update it
    if running_data_import
      if user.data_import.try(:status) != DataImport::RUNNING
        Rails.logger.error "User #{user.id} - #{user.email} does not have a data import with status RUNNING, aborting job"
        return
      end
    end

    self.subscribe_feed feed_url, user, folder
  rescue => e
    Rails.logger.error e.message
    Rails.logger.error e.backtrace
    # Re-raise the exception so that Resque takes care of it<commit_msg>When adding a subscription in SubscribeUserJob, if an error caused by the feed server is raised (404 response, timeout, unable to resolve FQDN...) do not mark the job as failed.<commit_after>##
# Background job to subscribe a user to a feed and, optionally, put the feed in a folder.
#
# Its perform method will be invoked from a Resque worker.

class SubscribeUserJob
  @queue = :update_feeds

  ##
  # Subscribe a user to a feed and optionally put the feed in a folder
  #
  # Receives as arguments:
  # - id of the user
  # - url of the feed
  # - id of the folder. It must be owned by the user. If a nil is passed, ignore it
  # - boolean indicating whether the subscription is part of an OPML import process
  #
  # If requested, the data_import of the user is updated so that the user can see the import progress.
  #
  # This method is intended to be invoked from Resque, which means it is performed in the background.

  def self.perform(user_id, feed_url, folder_id, running_data_import)
    # Check if the user actually exists
    if !User.exists? user_id
      Rails.logger.error "Trying to add subscription to non-existing user @#{user_id}, aborting job"
      return
    end
    user = User.find user_id

    # Check if the folder actually exists and is owned by the user
    if folder_id.present?
      if !Folder.exists? folder_id
        Rails.logger.error "Trying to add subscription in non-existing folder @#{folder_id}, aborting job"
        return
      end
      folder = Folder.find folder_id
      if !user.folders.include? folder
        Rails.logger.error "Trying to add subscription in folder #{folder.id} - #{folder.title} which is not owned by user #{user.id} - #{user.email}, aborting job"
        return
      end
    end

    # Check that user has a data_import with status RUNNING if requested to update it
    if running_data_import
      if user.data_import.try(:status) != DataImport::RUNNING
        Rails.logger.error "User #{user.id} - #{user.email} does not have a data import with status RUNNING, aborting job"
        return
      end
    end

    self.subscribe_feed feed_url, user, folder
  rescue RestClient::Exception, SocketError, Errno::ETIMEDOUT, AlreadySubscribedError, EmptyResponseError, FeedAutodiscoveryError, FeedFetchError, FeedParseError, ImportDataError=> e
    # all these errors mean the feed cannot be subscribed, but the job itself has not failed. Do not re-raise the error
    Rails.logger.error e.message
    Rails.logger.error e.backtrace
  rescue => e
    Rails.logger.error e.message
    Rails.logger.error e.backtrace
    # The job has failed. Re-raise the exception so that Resque takes care of it
{'fraction_non_alphanumeric': 0.06743455497382199, 'fraction_numerical': 0.0006282722513089005, 'mean_word_length': 3.0681431005110733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6620754', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1356, 'n_words': 603}