Search code examples
ruby-on-railssidekiqworker

How to create a background job for get request with Sidekiq and httparty?


I need help developing a worker with sidekiq for this situation:

I have a helper that looks like this:

module UploadsHelper

    def save_image
        response = HTTParty.get(ENV['IMAGE_URI'])
        image_data = JSON.parse(response.body)
        images = image_data["rows"].map do |line|
            u = Image.new
            u.description = line[5]
            u.image_url = line[6]
            u.save
            u
        end
        images.select(&:persisted?)
    end

end

In my app/views/uploads/index.html.erb I just do this

<% save_image %>

Now, when a user visits the uploads/index page the images are saved to the database.

The problem is that the get request to the API is really slow. I want to prevent request timeouts by moving this to a background job with sidekiq.

This is my workers/api_worker.rb

class ApiWorker
  include Sidekiq::Worker

  def perform

  end

end

I just don't know the best way to proceed from here.


Solution

  • Performing this task using a Sidekiq worker implies that the task will run in async, and thus, it will not be able to return the response immediately, which is being sent by images.select(&:persisted?).

    First of all, instead of calling save_image, you need to call the worker's perform_async method.

    <% ApiWorker.perform_async %>
    

    This will enqueue a job in Sidekiq's queue (your_queue in this example). Then in worker's perform method, call the save_image method of UploadsHelper.

    class ApiWorker
      include Sidekiq::Worker
      sidekiq_options queue: 'your_queue'
      include UploadsHelper
    
      def perform
        save_image
      end
    end
    

    You may want to save the response of save_image somewhere. To get Sidekiq start processing the jobs, you can run bundle exec sidekiq from your app directory.