Search code examples
ruby-on-railsruby-on-rails-3downloadresqueworker

Break Out File Download into Background Process Using Sidekiq


I'm trying to break out file download into a background process. My assets are stored on S3.

My original (blocking) code looks like this

# From DownloadsController#download

data = open(path)
send_data(data.read, type: @download.mime_type, filename: @download.file_title)

So I've set up Redis and Sidekiq, and Created a FielDownloadWorker:

class FileDownloadWorker

  include Sidekiq::Worker

  def perform(path, mime_type, file_title)
    data = open(path)
    # What happens next?
  end

end

Which is called using:

FileDownloadWorker.perform_async(path, @download.mime_type, @download.file_title)

How do I initiate download from the worker?


Solution

  • I ended up initiating the file download directly from S3 using Query String Authentication. This way the file is downloaded directly to the client from S3 and the Rails app's thread isn't blocked.

    Great writeup here.