How to get asset caching working - ruby-on-rails

I am trying to upgrade to cedar / asset pipeline and am stuggling with something.
Everything renders fine, but my app seems noticeably slower. Looking thorugh my logs I see tons of items like these:
2012-03-15T17:03:02+00:00 app[web.1]: cache: [GET
/assets/application.js] miss 2012-03-15T17:03:02+00:00 app[web.1]:
cache: [GET /assets/down_arrow.gif] miss 2012-03-15T17:03:02+00:00
app[web.1]: cache: [GET /assets/application.css] miss
I would expect these to be hits -- right?
My production.rb
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# For nginx:
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
# I also tried these
# config.action_dispatch.x_sendfile_header = "X-Sendfile"
# config.action_dispatch.x_sendfile_header = nil
config.cache_store = :dalli_store
My application.rb
...
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.assets.initialize_on_precompile = false
config.active_support.deprecation = :log
Note: that assets:precompile works fine on deploy:
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
-----> Rails plugin injection
Injecting rails_log_stdout
Injecting rails3_serve_static_assets
Thanks! Let me know if you need additional information

you probably missed one of the many asset-pipeline configurations. have a look at my production.rb file:
HamburgOnRuby::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
# config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
# Compress both stylesheets and JavaScripts
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :scss
# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
config.action_dispatch.x_sendfile_header = nil # http://devcenter.heroku.com/articles/rails31_heroku_cedar#the_asset_pipeline
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# See everything in the log (default is :info)
# config.log_level = :debug
# Use a different logger for distributed setups
# config.logger = SyslogLogger.new
# Use a different cache store in production
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
config.assets.precompile += %w( active_admin.js active_admin.css )
# Disable delivery errors, bad email addresses will be ignored
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => "25",
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => ENV['SENDGRID_DOMAIN']
}
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
# Set the default host for production
config.default_host = 'onruby.de'
end

I raised a support ticket with heroku and here was there response:
I'd give this a shot:
http://jackchu.com/blog/2011/09/20/rails-asset-pipeline-content-delivery-networks-and-rack-cache/
Recent investigations Rack::Cache and memcached/dalli seems to be
broken in Rails 3.1+.
That seemed to do the trick. I look forward to the interaction between Rack::Cache and dalli to be fixed, but in the mean time I will follow #jackchu approach

Related

Rails + AWS + sendmail: Emails not being sent even with (seemingly) correct config settings?

So in my development my emails are sending fine when I change the settings. However in my preview environment on my server, in the logs it looks like emails are sending properly with no errors, but I never receive anything. This applies to not just one mailer, but all of them.
This is my config/environments/preview.rb file:
EdmundWeb::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to nil and saved in location specified by config.assets.prefix
# config.assets.manifest = YOUR_PATH
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# See everything in the log (default is :info)
config.log_level = :debug
# Prepend all log lines with the following tags
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production
# config.cache_store = :mem_cache_store
config.action_view.cache_template_loading = true
config.cache_store = :dalli_store, 'localhost', { :namespace => 'preview' }
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
# config.active_record.auto_explain_threshold_in_seconds = 0.5
# For Devise
config.action_mailer.default_url_options = { :host => 'mysite.com' }
config.to_prepare { Devise::SessionsController.force_ssl }
config.to_prepare { Devise::RegistrationsController.force_ssl }
config.to_prepare { Devise::PasswordsController.force_ssl }
end
In my environment.rb I set my mailer to sendmail:
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
EdmundWeb::Application.initialize!
ActionMailer::Base.delivery_method = :sendmail
Could it be mailer settings on my AWS server that is preventing my mailer from sending mail?
My AWS machine was running on ubuntu. The fix was running apt-get install postfix as root and accepting all the default answers. How is this not documented anywhere else!

Javascript fails to load in production

My app works dandy in development but yesterday I tried to move it to my production server and after some work with permissions and paths I though I got it to work.
But it seems like I'm unable to load the JS assets while images and css works fine.
At first I thought it was the bootstrap js file that made my life misserable but removing it just gives me an error at the next JS asset.
https://dl.dropboxusercontent.com/u/32242733/jsfail.png
Production.rb
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to nil and saved in location specified by config.assets.prefix
# config.assets.manifest = YOUR_PATH
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# See everything in the log (default is :info)
# config.log_level = :debug
# Prepend all log lines with the following tags
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
config.action_mailer.default_url_options = { :host => 'example.com' }
# ActionMailer Config
# Setup for production - deliveries, no errors raised
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "example.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
# config.active_record.auto_explain_threshold_in_seconds = 0.5
Assets
root#flypatterns:/var/www/flypatterns/current# ls public/assets/
application-35684ef44e5902293fd317c512600f66.css glyphicons-halflings.png
application-35684ef44e5902293fd317c512600f66.css.gz glyphicons-halflings-white-6cccd17a7aed91dbc0157d343c68c0d9.png
application-66ba1e6c258ba515e18111b4b93c8c57.js glyphicons-halflings-white.png
application-66ba1e6c258ba515e18111b4b93c8c57.js.gz images
application.css jquery.min-6c267bfd2b3f36e6edccb2e584934c1c.map
application.css.gz jquery.min.map
application.js manifest.yml
application.js.gz patterns
gallery_images users
glyphicons-halflings-2851b489e8c39f8fad44fc10efb99c3e.png
i follow this step for Heroku
hello do follwing changes before deploy following file
------enviorment.rb-----
::ActiveSupport::Deprecation.silenced = true
------Production.rb-------
config.assets.compile = ['*.js', '*.css']
config.active_support.deprecation = :silence
-------application.rb-------
config.assets.enabled = true
config.assets.initialize_on_precompile = false
try changing
config.serve_static_assets = false
to
config.serve_static_assets = true
I encountered a problem with my app while pushing it to heroku. I had used twitter-bootstrap, there was some errors while precompiling assets. So i switched to manual compiling rather than automatic precompiling. For that i did the following:
I had set initialize_on_precompile to false in config/application.rb as follows:
config.assets.initialize_on_precompile = false
and then i did manually by running:
bundle exec rake assets:precompile RAILS_ENV=production
Finally it worked...
This maynot be the solution you looking for unless you use any predefined front-end frameworks.
check your application.js file i had added some code to do a java script code that added to some funk weird file. the problem was i was missing an "/" for some code that must have been commented out :-( stupid me it took forever
bundle exec rake assets:precompile RAILS_ENV=production
showed me what file and line to look at and i opened that file in sublime. I hope this helps took me5 days to find this damn error smh

Caching caches_action fragmens in Memcache and assets in the file system

Using Rails 3.1.1 on Heroku, Dalli gem and Memcachier.
I am trying to troubleshoot an issue where keys disappear from Memcachier, in order to do this I would like to make sure that only the content from my caches_action will be cached in the memcache. I would like my assets (jpg's etc) to be cached elsewhere.
I believe it is the same issue as this question but when I do the same, i.e. adding
config.action_dispatch.rack_cache = {
:metastore => Dalli::Client.new,
:entitystore => 'file:tmp/cache/rack/body',
:allow_reload => false
}
to my production.rb, it doesn't really seem to do anything.
My production.rb in full:
MyDemoApp::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# See everything in the log (default is :info)
# config.log_level = :debug
# Prepend all log lines with the following tags
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production
config.cache_store = :dalli_store
config.action_dispatch.rack_cache = {
:metastore => Dalli::Client.new,
:entitystore => 'file:tmp/cache/rack/body',
:allow_reload => false
}
# Set expire header of 30 days for static files
config.static_cache_control = "public, max-age=2592000"
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end
Caching assets work fine (get cache fresh-responses) and caching actions like this:
caches_action :show, :layout => false, :expires_in => 3.days
Also creates a cached entry in Rails cache (although they disappear after a while).
My question now is: How can I, if possible, do so that only my caches_action fragments end up in my Memcache (and not my assets)?
A bonus question would be, do you see anything out of the ordinary in my production.rb?
I use the gem 'asset_sync' to move all my assets out to Amamzon S3
See Instructions here
You get three advantages:
Your assets are served from Amazon so the page loads faster for the
user
Your slug size on Heroku is smaller and is supposed to be more
performant
Your assets will no longer be in memecahce
All my assets cost me about 20cents every three month to host on Amamzon

Datatables not showing in production mode after running rake assets:precompile. rails 3.2

When i run my application in development my datatables table is showing
but when i run my app in production datatables is not showing. only the records ar shown.
screenshot in developemnt:
screenshot in production:
My production.rb:
Contractbeheerpj::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to nil and saved in location specified by config.assets.prefix
# config.assets.manifest = YOUR_PATH
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# See everything in the log (default is :info)
# config.log_level = :debug
# Prepend all log lines with the following tags
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
# config.active_record.auto_explain_threshold_in_seconds = 0.5
end
Hope someone can help! :)
Your issue is probably because of Rails Asset pipelining which Rails in production precompiles your js files to public/assets hence your js file is either not loaded or is an older version. Look at this answer to a similar issue to what you are having.

How can I use assets within Mailer?

I have trouble using any form of the asset pipeline within mailer, wether the Mailer itself or the view.
The following produces and empty src image tag.
<%= image_tag "emails/header-general.png" %>
The empty image tag looks like this:
img alt="Header-general"
The following form of attaching a file through the model and using it in the view attaches an empty image.
attachments.inline['header.jpg'] = 'emails/header-general.png'
...
<%= image_tag attachments['header.png'] %>
I did check the path and even tried with multiple paths and so on but with no luck.
Please help. Any form of including an image within the email would be helpful.
Here is the production env.
Xenium::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Choose the compressors to use
config.assets.js_compressor = :yui
config.assets.css_compressor = :yui
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# See everything in the log (default is :info)
config.log_level = :fatal
# Use a different logger for distributed setups
# config.logger = SyslogLogger.new
# Use a different cache store in production
config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server
#config.action_controller.asset_host = "http://asset.xenium.bg"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
config.action_mailer.raise_delivery_errors = true
#config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "localhost",
:port => 25,
:domain => 'xenium.bg',
#:user_name => '<username>',
#:password => '<password>',
#:authentication => 'plain',
:enable_starttls_auto => false
}
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end
Thanks!
set config.action_controller.asset_host and config.action_mailer.asset_host and this works well.
config.action_mailer.asset_host = URL from where pick image
<%= image_tag image_path('logo.png') %>
According to the 2.3.3 Making Inline Attachments section, to create an inline attachment you will do as follows
attachments.inline['image.jpg'] = File.read('/path/to/image.jpg')
So in your case, it should be
attachments.inline['header.jpg'] = File.read("#{Rails.root}/app/assets/images/emails/header-general.png"

Resources