I have an app on Rails 3.2 which is deployed on Heroku
with unicorn
. It requires a job to be executed every hour. So I have added Heroku Scheduler
Addon which heroku provides.
I have also integrated Sidekiq
and Redis
with my app.
Following are the code snippet:
config/initializer/sidekiq.rb
Sidekiq.configure_server do |config|
config.redis = { url: ENV["OPENREDIS_URL"] }
end unless ENV['OPENREDIS_URL'].blank?
config/unicorn.rb
worker_processes 2
timeout 30
preload_app true
before_fork do |server, worker|
# Replace with MongoDB or whatever
if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection.disconnect!
Rails.logger.info('Disconnected from ActiveRecord')
end
Rails.logger.info('Disconnected from Redis')
sleep 1
end
after_fork do |server, worker|
# Replace with MongoDB or whatever
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
Rails.logger.info('Connected to ActiveRecord')
end
Sidekiq.configure_client do |config|
config.redis = { url: ENV["OPENREDIS_URL"] }
end unless ENV['OPENREDIS_URL'].blank?
Rails.logger.info('Connected to Redis')
end
The issue is: Whenever the rake task that I mentioned in Heroku Scheduler is executed, it gives following error:
Redis::CannotConnectError: Error connecting to Redis on localhost:6379 (ECONNREFUSED)
Trying to figure out what is wrong here?
UPDATE: On a side note, it is happening only for Heroku scheduler tasks. Other background jobs are working fine.
Finally, got it working by adding Sidekiq client configuration to sidekiq.rb
.
Sidekiq.configure_server do |config|
config.redis = { url: ENV["OPENREDIS_URL"] }
end unless ENV['OPENREDIS_URL'].blank?
Sidekiq.configure_client do |config|
config.redis = { url: ENV["OPENREDIS_URL"] }
end unless ENV['OPENREDIS_URL'].blank?