views:

398

answers:

1

While using Capistrano for deployment every time that I deploy I find that Rails regenerates the entire JS & CSS asset caches. This is regardless of whether or not the file is updated.

Is there any way to get Rails to do this only if one, or more, of the files have been updated?

I have turned off :normalize_asset_timestamps in Capistrano so that should not be an issue.


My main issue is that right off the bat it uses a substantial amount of memory to perform this action and it seemingly never releases that memory.

Thanks in advance.

+1  A: 

If the files are not present, Rails will create them. You could scp the old cached assets into the new directory before starting your Rails code, however I don't believe capistrano is smart about which files have changed so you won't know when to do this.

If you want exactly what you describe, I would maybe md5sum the old and new js/css directories and look for a difference. If there is one, you need to build from scratch, if there is not one, you can scp the old files over.

Alternatively, if all you care about is the memory you can build the assets before starting the servers. Something like:

class AssetCache
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::AssetTagHelper

  APPLICATION_JAVASCRIPTS = %w{ mootools mycustom admin }

  def pre_cache
    javascript_include_tag JAVASCRIPT_ASSETS,
                           :cache => 'cache/app.js'
  end
end

And in cap before you start Rails daemons run script/runner -e production AssetCache.new.pre_cache to populate the files. In your layouts, just use AssetCache::APPLICATION_JAVASCRIPTS instead of your inline list of files. Rinse, repeat and wash for stylesheets. The Rails daemons will see the existing caches and not hog your memory.

If you're on multiple servers you will probably need to replicate the assets across without changing their timestamps.

Good luck!

mixonic