How to debug Rails app in production

Hi,

I just deployed a Rails app on a Digital Ocean VPS (Ubuntu 14.04 Trusty Tahr)

Everything works, except for uploading images to Amazon AWS

I’m using Carrierwave to uploade images, my initializer configuration looks like this:

CarrierWave.configure do |config|
  if Rails.env.staging? || Rails.env.production?
    config.storage = :fog
    config.fog_credentials = {
      :provider               => 'AWS',                        # required
      :aws_access_key_id      => Rails.application.secrets.S3_KEY,                        # required
      :aws_secret_access_key  => Rails.application.secrets.S3_SECRET,                        # required
    }
    config.fog_directory  = 'beautysalon2'                     # required
  elsif Rails.env.test?
    config.storage = :file
    config.enable_processing = false

    # make sure our uploader is auto-loaded
    PromotionImageUploader

    # use different dirs when testing
    CarrierWave::Uploader::Base.descendants.each do |klass|
      next if klass.anonymous?
      klass.class_eval do
        def cache_dir
          "#{Rails.root}/spec/support/uploads/tmp"
        end

        def store_dir
          "#{Rails.root}/spec/support/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
        end
      end
    end
  else
    config.storage = :file
    config.enable_processing = Rails.env.production?
  end
end

I suspect there must be an issue with the AWS credentials but I’m not completely sure. I checked the /var/log/nginx/error.log on the production server, but I can’t find any errors there pointing to an issue with the AWS credentials.

Is there any other place I can look for errors on the productions server?

Thanks for your help,

Anthony

I think there are three places you should try looking/trying that aren’t the nginx logs:

  1. Can you upload a file from the filesystem (such as your app repo’s README) using the console in production?
  2. Can you temporarily download your production credentials to development and debug locally?
  3. (for use in conjunction with the above two): I would look in the development.log and production.log files rather than the nginx logs for exception reports.

Hi Geoff,

Not sure how to upload files from the console in production.

When I check the production log in /home/deploy/myapp/shared/log/production.log

I see this error message:

I, [2014-11-07T05:27:29.714155 #21359]  INFO -- : Started GET "/images/environ-peeling-kuur.jpg" for     157.193.240.206 at
2014-11-07 05:27:29 -0500
F, [2014-11-07T05:27:29.717148 #21359] FATAL -- :
ActionController::RoutingError (No route matches [GET] "/images/environ-peeling-kuur.jpg"):
  actionpack (4.1.7) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.1.7) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.7) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.7) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.7) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.7) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.7) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.7) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.7) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.7) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'

“~/beautysalon/shared/log/production.log” 1161L, 95551C

But I’m not sure if this error message:

ActionController::RoutingError (No route matches [GET] "/images/environ-peeling-kuur.jpg"):

has anything to do with the issue of not being able to upload an image to Amazon S3 with Carrierwave. I think it’s just caused by the fact that there are some broken image paths on the homepage (the site is still in testing phase).

Thanks for your help,

Anthony

I followed your suggestion and tried out my AWS credentials in development mode. That lead me to the discovery of an issue in the carrierwave uploader class (app/uploaders/promotion_image_uploader.rb), where I called the scale() method, while the scale method was still commented out.

However, I did not solve the issue I have when uploading an image in production. Also when testing the Carrierwave upload functionality in development mode, I noticed that images are not stored in my Amazon S3 bucket.

So the problem must have to do something with my AWS credentials I guess. The tough part is that in production I don’t seem to find any debugging information.

greetings,

Anthony

On your development machine first make sure taht carrierwave is ifact using S3. In rails console access Rails.application.secrets.S3_ variables and make sure they are available and match your credentials. If everything is in place then I believe you should see some sort of error message in your development machine…

Looking at your config - You might be missing region and bucket name.

Hi Lenart,

Thanks for your suggestion. I checked my S3 variables in the Rails console with:

Rails.application.secrets.S3_KEY

and

Rails.application.secrets.S3_SECRET

and that’s ok

I finally solved the issue. I was right about calling the scale method in my Carrierwave uploader file, as the production.log gave this error:

I, [2014-11-08T05:31:52.761109 #4424]  INFO -- : Completed 500 Internal Server Error in 5ms
F, [2014-11-08T05:31:52.762952 #4424] FATAL -- :
NoMethodError (undefined method `scale' for #<PromotionImageUploader:0x007fcf33fa54d8>):
  app/controllers/admin/promotions_controller.rb:15:in `create'

However fixing this issue and redeploying the application didn’t solve the issue and to my puzzlement I kept seeing the same error message in the production.log

After lots of head scratching and server restarts and redeployments, I appeared that the problem was caused by Capistrano not deploying from the remote branch.

I use the technique for deploying from a remote branch as explained in this blog post:

https://firmhouse.com/blog/deploying-branches-with-capistrano

but deploying with:

BRANCH=my-remote-branch bundle exec cap production deploy

I took me a while to figure this out. After I merged my feature branch to master and deployed from the master branch, al problems where solved, and now I can upload images on my production server :smiley:

greetings,

Anthony

Oh I see, I thought these settings where not required.

my app is working now with this configuration:

CarrierWave.configure do |config|
  if Rails.env.staging? || Rails.env.production?
    config.storage = :fog
    config.fog_credentials = {
      :provider               => 'AWS',                        # required
      :aws_access_key_id      => Rails.application.secrets.S3_KEY,                        # required
      :aws_secret_access_key  => Rails.application.secrets.S3_SECRET,                        # required
   }
    config.fog_directory  = 'beautysalon2'                     # required
  else
    config.storage = :file
     config.enable_processing = Rails.env.production?
  end
end

However when I check my bucket in Amazon S3, the bucket is empty???

AWS has a somewhat complicated permissioning system, it may be that your access key ID isn’t allowed to save files in that bucket. In the AWS management console, I’d look at the user’s privileges and the bucket’s privileges.