Class: ImportOpmlWorker

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

Overview

Background job to import an OPML data file with subscriptions data for a user. After processing the OPML file it enqueues a superworker (using the sidekiq-superworker gem) to import each feed in the data file in batches of individual jobs.

This is a Sidekiq worker

Instance Method Summary collapse

Instance Method Details

#perform(filename, user_id) ⇒ Object

Imports an OPML file with subscriptions for a user, and then deletes it.

Receives as arguments:

  • the name of the file, including path from Rails.root (e.g. 'uploads/1371321122.opml')

  • the id of the user who is importing the file

The opml_import_job_state of the user is updated with the total number of feeds so that the user can see the import progress.

After finishing the job the file will be deleted no matter what.

Folders present in the file will be created if they don't already exist.

After this job finishes an ImportSubscriptionsWorker superworker will be enqueued, passing the feeds and folders present in the OPML as arguments. Thanks to sidekiq-superworker the actual work of importing each individual feed and moving it to the corresponding folder will be delegated to lightweigth ImportSubscriptionWorker instances.

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


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/workers/import_opml_worker.rb', line 36

def perform(filename, user_id)
  # Check if the user actually exists
  if !User.exists? user_id
    Rails.logger.error "Trying to import OPML file #{filename} for non-existing user #{user_id}"
    return
  end
  user = User.find user_id

  # Check that user has a opml_import_job_state with state RUNNING
  if user.opml_import_job_state&.state != OpmlImportJobState::RUNNING
    Rails.logger.error "User #{user.id} - #{user.email} does not have a data import with state RUNNING, aborting OPML import"
    return
  end

  OPMLImporter.process_opml filename, user
rescue => e
  OPMLImportNotifier.notify_error user, e
ensure
  Feedbunch::Application.config.uploads_manager.delete user_id, OPMLImporter::FOLDER, filename
end