Search code examples
ruby-on-railspaperclipdelayed-job

Paperclip with multiple servers


We have recently expanded an application to use multiple load balanced servers, and I have a question regarding handling of Paperclip attachments and, more specifically, the delayed upload to S3

Here's the scenario:

  • iPhone app adds new data to the web app, including a photo upload.
  • For performance reasons, the photo is first stored locally
  • We use delayed_job to process and upload the file to S3

Here is the relevant code:

has_attached_file :local

has_attached_file :remote, :styles =>{:thumb =>'100x100'},
                           :path => ":class/photos/:id_partition/:style.:extension",
                           :s3_credentials => "#{Rails.root}/config/amazon_s3.yml",
                           :storage => :s3

after_save :update_remote

def photo
  remote_file_name ? remote : local
end

def photo=(attachment)
  local = attachment
end

def update_remote
  unless self.remote_file_name
    if self.local.exists?
      self.remote = self.local
      self.local = nil
      self.save
    end
  end
end

handle_asynchronously :update_remote

My question is, how to expand this to multiple app servers? The local file will only exist on one of them, and I'm loathe to upload directly to S3 due to the delay.


Solution

  • It doesn't precisely answer the question, but I switched to using the database for the "local" attachment (before transferring to S3) in place of the file system, which got around the problem