Class: ImportSubscriptionWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/import_subscription_worker.rb

Overview

Background job to import a single feed subscription for a user.

This is a Sidekiq worker.

This worker is not enqueued directly, but rather is part of a batch set of workers enqueued by the sidekiq-superworker gem. It is always part of a global OPML import process.

Instance Method Summary collapse

Instance Method Details

#perform(opml_import_job_state_id, url, folder_id = nil) ⇒ Object

Import a single feed subscription for a user. Optionally the feed can be put into a folder.

Receives as arguments:

  • ID of the OpmlImportJobState instance. This object contains a reference to the user who is importing subscriptions,

so it's not necessary to pass the user ID as argument

  • URL of the feed

  • Optionally ID of the folder to put the feed into. Defaults to nil (feed won't be in a folder)

When the worker finishes, it increments by 1 the current number of processed feeds in the global OPML import job state. This enables the user to the import progress.

This method is intended to be invoked from Sidekiq-superworker, which means it is performed in the background.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/workers/import_subscription_worker.rb', line 28

def perform(opml_import_job_state_id, url, folder_id=nil)
  # Check if the opml import state actually exists
  if !OpmlImportJobState.exists? opml_import_job_state_id
    Rails.logger.error "Trying to perform ImportSubscriptionWorker as part of non-existing job state #{opml_import_job_state_id}. Aborting"
    return
  end
  opml_import_job_state = OpmlImportJobState.find opml_import_job_state_id
  user = opml_import_job_state.user

  # Check that opml_import_job_state has state RUNNING
  if opml_import_job_state.state != OpmlImportJobState::RUNNING
    Rails.logger.error "User #{user.id} - #{user.email} trying to perform ImportSubscriptionWorker as part of opml import with state #{opml_import_job_state.state} instead of RUNNING. Aborting"
    return
  end

  feed = import_feed user, url

  if folder_id.present? && feed.present?
    move_feed_to_folder user, feed, folder_id
  end
ensure
  # Only update total processed feeds count if job is in state RUNNING
  if OpmlImportJobState.exists? opml_import_job_state_id
    opml_import_job_state.reload
    if opml_import_job_state&.state == OpmlImportJobState::RUNNING
      Rails.logger.info "Incrementing processed feeds in OPML import for user #{user&.id} - #{user&.email} by 1"
      processed_feeds = opml_import_job_state.reload.processed_feeds
      # Increment the count of processed feeds up to the total number of feeds
      opml_import_job_state.update processed_feeds: processed_feeds+1 if processed_feeds < opml_import_job_state.total_feeds
    else
      Rails.logger.warn "OPML import job state #{opml_import_job_state_id} has state #{opml_import_job_state&.state} instead of RUNNING. Total number of processed feeds will not be incremented"
      return
    end
  else
    Rails.logger.warn "OPML import job state #{opml_import_job_state_id} was destroyed during import of feed #{url}. Aborting"
    return
  end
end