yappbox / render_anywhere

Render Rails templates to a string from any class.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Assets issue when rendering from background job

anero opened this issue · comments

Hey guys,

We're having some issues not specifically related to render_anywhere but you may be able to shed some light on them.

In our rails 3.2 app, we're using a Sidekiq job for refreshing cache on the background, below is the snippet for the base class and one of the workers:

require 'render_anywhere'

module CacheRenderer
  class Base
    include Sidekiq::Worker
    include RenderAnywhere
  end
end

# ------------------------------------

module CacheRenderer
  class RecentEventRsvps < Base
    #do not enqueue a rendering job more than once for the same petition over a 2 minute period
    sidekiq_options unique: true, unique_job_expiration: 2 * 60

    def perform(event_id)
      event = Event.find(event_id)
      content_to_cache = render(partial: 'shared/attendees_carousel', locals: { ... })
      Rails.cache.write(['recent_event_rsvps', event.cache_key], content_to_cache)
    end
  end
end

Now this is rendered just fine in development, where assets are not precompiled, but in other environments a AssetNotPrecompiledError exception is risen for an image referenced from an image_tag. Doing some debugging we found that Rails.application.config.assets.digests is nil when the job is ran. Assets are synced to S3 using asset_sync.
Have you ever seen this kind of issues? Do you know if we need to perform any manual initialization for sprockets on the sidekiq job?

Thanks in advance,
Diego.

Is your Sidekiq process loading all of Rails, or just some portions of your application? If it's loading all of Rails it should load everything it needs. If it's a portion, there may be some initializers you need to include.

I've also seen issues before if there's an asset host, say if a CDN is being used. Are you using one?

We're loading the full app; but the issue might be on the CDN (Cloudfront), is there an extra step required to reference the assets on a different host?

Another thing that I realized it may be important is that the sidekiq workers are running on a separated server which doesn't run assets precompilation (the assets are precompiled on the web app servers and uploaded to S3 through the asset_sync gem).

I think you'll want config.assets.compile = false in your config, and also set the CDN config.action_controller.asset_host = "cdn.domain.com". Then it shouldn't even try to compile it.