简体   繁体   中英

Heroku scheduler with Sidekiq and redis

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?

一种选择是将URL保留为空,并将REDIS_PROVIDER环境变量设置为OPENREDIS_URL。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM