Sean Rucker
Sean Rucker

Reputation: 1096

How to configure Resque on Heroku with Rails 3.1 and the asset pipeline

After installing the Resque gem and running a worker my app now produces an asset pipeline error:

Sass:SyntaxError: File to import not found or unreadable

I created a fresh branch and narrowed the problem down to running the Resque worker. I've only changed three files. Here are the steps to reproduce:

1) Add the Resque gem to Gemfile:

gem 'resque'

2) Create a Resque rake task:

# lib/resque.rake

require 'resque/tasks'

task "resque:setup" => :environment do
    ENV['QUEUE'] = '*'
    Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection }
end

3) Add a worker to the Procfile:

web: bundle exec rails server -p $PORT thin
worker: bundle exec rake resque:work

I have a feeling that the problem is that the worker is trying to load all my assets. Which I don't want because its just a background process. I'm not even running the Resque front end so the problem is unrelated to that.

Upvotes: 4

Views: 1344

Answers (2)

hybernaut
hybernaut

Reputation: 636

In Rails 4 the initialize_on_precompile option is deprecated.

I was having the same problem with a Rails 4 app and decided that there's no need to create a Redis connection if the REDISTOGO_URL config var is not set (for example, during assets:precompile). So I changed my redis.rb:

if ENV.include?('REDISTOGO_URL')
  uri = URI.parse(ENV["REDISTOGO_URL"])
  REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end

Upvotes: 0

Sean Rucker
Sean Rucker

Reputation: 1096

The culprit was actually in my Resque initializer:

if Rails.env.staging? || Rails.env.production?
  uri = URI.parse ENV['REDISTOGO_URL']
  Resque.redis = Redis.new :host => uri.host, :port => uri.port, :password => uri.password
end

Heroku runs assets:precompile during slug compilation. During the precompile the environment is loaded, but Heroku does not pass in the ENV vars. The URI parse line was failing due to

ENV['REDISTOGO_URL'] 

being nil. This caused the assets:precompile rake task to fail.

The solution is to add:

config.assets.initialize_on_precompile = false 

to your application.rb file.

This is available as of Rails 3.1.1 and when set to false the environment will no longer be loaded when compiling the assets. This is a safe choice most of the times.

Thanks Neil for pointing me in the right direction.

Upvotes: 5

Related Questions