Im having trouble launching Sidekiq on my AWS ElasticBeanstalk environment.
I am running Sidekiq 4.1.0 with my Rails application.
I logged directly into the EC2 instance to start Sidekiq manually and when I run this:
bundle exec sidekiq -e staging -q default -q mailers
I get this error from the Insance:
2016-02-10T07:19:34.172Z 1655 TID-gn7dktok8 INFO: Booting Sidekiq 4.1.0 with redis options {:url=>"redis://xxxx.amazonaws.com:6379"}
'xxxx.xxxx.rds.amazonaws.com' database is not configured. Available: ["default", "development", "test", "production", "staging"]
I checked the RDS database URL its trying to connect to and its the right one for the environment I am trying to run this on.
My sidekiq config file looks like this:
redis_url = ENV['REDIS_URL']
Sidekiq.configure_server do |config|
config.redis = { url: redis_url, namespace: "swimlanes_#{Rails.env}" }
end
Sidekiq.configure_client do |config|
config.redis = { url: redis_url, namespace: "swimlanes_#{Rails.env}" }
end
I have tried everything. Do you know what I am missing?
Sounds like I helped you find the issue on GitHub: setting the Rails env incorrectly somewhere?
Related
I need help with my sidekiq and redis set up in my rails (7) app.
The problem is that all my Sidekiq jobs (emails) are being enqueued in production and not sending, but they work fine in development.
Here are my relevant files
sidekiq.rb
require 'sidekiq/web'
Sidekiq.configure_server do |config|
config.redis = { url: ENV.fetch('REDIS_URL', 'redis://localhost:6379/1') }
end
Sidekiq.configure_client do |config|
config.redis = { url: ENV.fetch('REDIS_URL', 'redis://localhost:6379/1') }
end
sidekiq.yml
development:
:concurrency: 5
production:
verbose: false
:concurrency: 5
:timeout: 8
:max_retries: 1
:queues:
- default
- mailers
Procfile.dev
web: bundle exec rails server -p $PORT
worker: bundle exec sidekiq -c 5 -v
I have basically read all of the questions related to my problem and the lucid guidelines by Mike Perham. None of the answers work; to be fair, I am feeling my way in the dark. It's unclear what the issue is because I do not understand the processes thoroughly. I am new to Sidekiq. If you need more information, please let me know.
I am having a really strange problem on Heroku that I have been spinning my wheels to figure out for a while now.
My app has a few external API calls and mailers which I have set up ActiveJob to run in the background. On Heroku I have two workers set up as and I am using a Resque/Redis combo for the jobs as per the below snippets. I am using the Redis Cloud add-on on Heroku.
Config / setup
Procfile
web: bundle exec puma -C config/puma.rb
resque: env TERM_CHILD=1 QUEUE=* bundle exec rake resque:work
lib/tasks/resque.rake
require "resque/tasks"
require "resque/scheduler/tasks"
task "resque:setup": :environment do
Resque.before_fork = proc { ActiveRecord::Base.connection.disconnect! }
Resque.after_fork = proc { ActiveRecord::Base.establish_connection }
end
config/initializers/active_job.rb
Rails.application.config.active_job.queue_adapter = :resque
config/initializers/redis.rb
if ENV["REDISCLOUD_URL"]
$redis = Redis.new(url: ENV["REDISCLOUD_URL"])
end
config/initializers/resque.rb
if Rails.env.production?
uri = URI.parse ENV["REDISCLOUD_URL"]
Resque.redis = Redis.new(host: uri.host, port: uri.port,
password: uri.password)
else
Resque.redis = "localhost:6379"
end
The problem
The problem I am having is when a user is using the app in browser (i.e., interfacing with the web worker) and performs an action which triggers one of the ActiveJob jobs the job is run "inline" using the web worker and not the resque worker. When I run the specific model method that queues the job in my Heroku app console (opened by running heroku run rails console) it adds the job to Redis and runs it using the resque worker as expected.
Why would one way work properly and the other way not work? I have looked at almost every tutorial / SO question on the topic and have tried everything so any help getting the jobs to be run but the right worker would be amazing!
Thanks in advance!
I managed to solve the problem by playing with my config a little. It seems that actions were being tunnelled through ActiveJob's "Inline" default rather than via Resque. To get things working I just had to direct Resque.redis to be equal to the $redis variable set in config/initializers/redis.rb so everything was pointing to the same Redis instance and then move the config set in config/initializers/active_job.rb to application.rb.
For reference, the new & improved config that all works is:
Config / setup
Procfile
web: bundle exec puma -C config/puma.rb
resque: env TERM_CHILD=1 RESQUE_TERM_TIMEOUT=7 QUEUE=* bundle exec rake resque:work
lib/tasks/resque.rake
require "resque/tasks"
task "resque:setup" => :environment
config/application.rb
module App
class Application < Rails::Application
...
# Set Resque as ActiveJob queue adapter.
config.active_job.queue_adapter = :resque
end
end
config/initializers/redis.rb
if ENV["REDISCLOUD_URL"]
$redis = Redis.new(url: ENV["REDISCLOUD_URL"])
end
config/initializers/resque.rb
Resque.redis = Rails.env.production? ? $redis : "localhost:6379"
thanks a lot for providing the answer. It saved me a lot of time.
You have one typo inside your Procfile.
It should be resque instead of rescue.
resque: env TERM_CHILD=1 RESQUE_TERM_TIMEOUT=7 QUEUE=* bundle exec rake resque:work
Also, I had to type in one more command to get this all to work in production. Hopefully this helps someone.
heroku ps:scale resque=1 --app appname
This command scales the resque process to 1 dyno(free). You can also do this from the dashboard on heroku.
You can read more about it on the heroku docs https://devcenter.heroku.com/articles/scaling
I've been struggling for two days with getting sidekiq work on heroku production environment. I've read all the available documentation about similar issues, and still haven't been able to produce a working solution, I'd really like some help!
After deploying on heroku, my app crashes and I get the following error stack trace:
2014-09-23T23:38:40.905093+00:00 app[worker.1]: No such file or directory # rb_sysopen - /app/tmp/pids/sidekiq.pid
2014-09-23T23:38:40.905122+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.1.0/bin/sidekiq:23:in `<main>'
2014-09-23T23:38:40.905119+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.1.0/gems/sidekiq-3.2.5/bin/sidekiq:7:in `<top (required)>'
2014-09-23T23:38:40.905117+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.1.0/gems/sidekiq-3.2.5/lib/sidekiq/cli.rb:347:in `write_pid'
2014-09-23T23:38:40.905115+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.1.0/gems/sidekiq-3.2.5/lib/sidekiq/cli.rb:347:in `open'
2014-09-23T23:38:40.905121+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.1.0/bin/sidekiq:23:in `load'
2014-09-23T23:38:40.905118+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.1.0/gems/sidekiq-3.2.5/lib/sidekiq/cli.rb:41:in `parse'
2014-09-23T23:38:40.905114+00:00 app[worker.1]: /app/vendor/bundle/ruby/2.1.0/gems/sidekiq-3.2.5/lib/sidekiq/cli.rb:347:in `initialize'
2014-09-23T23:38:39.588001+00:00 heroku[worker.1]: State changed from starting to up
First, sidekiq is working correctly on my local machine. I'm using heroku's REDISTOGO for redis, on local production, sidekiq have been pointed to the REDISTOGO correctly and running fine.
Second, according to the stack trace, especially this line No such file or directory # rb_sysopen - /app/tmp/pids/sidekiq.pid; it leads me to think that for some reason sidekiq.pid file is not generated correctly when running on heroku. On local environment, the sidekiq.pid file is generated everytime I start the app in the app/tmp/pids/ directory, and assigns a different pid number every time. I'm guessing that when running on heroku, sidekiq tried to read from this file yet could not find it.
Here is contents in my Procfile:
web: bundle exec rails server
worker: bundle exec sidekiq -C config/sidekiq.yml
Here is contents in my config/sidekiq.yml
---
:verbose: true
:pidfile: ./tmp/pids/sidekiq.pid
:concurrency: 25
# Set timeout to 8 on Heroku, longer if you manage your own systems.
:timeout: 8
:queues:
- carrierwave
Here is contents in my sidekiq.rb
Sidekiq.configure_server do |config|
config.redis = { :url => ENV['REDISTOGO_URL'], :namespace => "mynamespece"}
end
Sidekiq.configure_client do |config|
config.redis = { :url => ENV['REDISTOGO_URL'], :namespace => "mynamespece"}
end
Update 1:
I'm using carrierwave and carrierwave-backgrounder in sync with sidekiq.
This issue has been resolved by the following actions:
1) Thanks to #MikePerham for pointing me to the right direction, first I removed this line in my sidekiq.yml file:
:pidfile: ./tmp/pids/sidekiq.pid
2) Then, in my Procfile, I had to use the following line to replace the origin:
web: bundle exec rails server -p $PORT
worker: bundle exec sidekiq -C config/sidekiq.yml
Now sidekiq is working correctly with redistogo on heroku for me.
I found it useful to add the directory to the repository and re-deploy the application.
mkdir -p tmp/pids
touch tmp/pids/.gitkeep
git add -f tmp/pids/.gitkeep
git commit -m 'Keep tmp/pids directory in repo'
Hope this helps.
It was fixed by making pids directory in tmp directory in root of project
I'm trying to get Sidekiq playing in Heroku. Without Luck.. My configs look like that:
Procfile
web: bundle exec passenger start -p $PORT --max-pool-size 5
worker: bundle exec sidekiq
Initializers/redis.rb
uri = URI.parse(ENV["REDISTOGO_URL"] || "redis://localhost:6379/")
REDIS = Redis.new(:url => ENV['REDISTOGO_URL'])
but when i do heroku ps only the web instance is shown. Not Sidekiq.
However i can manually run heroku run sidekiq and run my workers. What am i missing so that Heroku doesn't start that on it's own ?
The problem is that you didn't configure Sidekiq in the initializer so that Sidekiq knows exactly how to connect to redis. Create Initializers/sidekiq.rb and add the following code
Sidekiq.configure_server do |config|
config.redis = { :url => ENV["REDISTOGO_URL"] }
end
Also you can remove
uri = URI.parse(ENV["REDISTOGO_URL"] || "redis://localhost:6379/")
You don't need it as you are using the redis url from the environment.
Don't forget to restart your server.
A Rails 3.2.6 application running as a 'web' Heroku process is connecting to Redis using the ENV["REDISTOGO_URL"] environment variable:
irb(main):002:0> Redis.current
=> #<Redis client v2.2.2 connected to redis://xxx.redistogo.com:1234/0 (Redis v2.4.11)>
----- /initializers/redis.rb
if Rails.env.development?
Redis.current = Redis.new
elsif Rails.env.test?
Redis.current = Redis.new
elsif Rails.env.production?
uri = URI.parse(ENV["REDISTOGO_URL"])
Redis.current = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
A ruby daemon 'streaming' process runs as a secondary worker:
----- Procfile
web: bundle exec rails server thin -p $PORT -e $RACK_ENV
worker: bundle exec rake jobs:work
streaming: RAILS_ENV=production ruby bin/streaming.rb start
However, the streaming process is crashing when it calls methods in the main Rails application that connect with Redis - even though the streaming process supposed to be loading the same redis.rb initializer as the 'web' Rails application process.
----- /bin/streaming_ctl.rb
# encoding: UTF-8
require "rubygems"
require "bundler/setup"
require "daemons"
Daemons.run(File.expand_path("../streaming.rb", __FILE__))
----- /bin/streaming.rb
# encoding: UTF-8
# TODO: set rails env in init script
ENV["RAILS_ENV"] ||= "production"
# load rails environment
require File.expand_path('../../config/environment', __FILE__)
logger = ActiveSupport::BufferedLogger.new(File.expand_path("./../../log/streaming.log", __FILE__))
Streaming.start
logger.info("\nStarting streaming in #{Rails.env.to_s} mode.")
Why is the streaming process/worker using the default Redis host and port?
[streaming.1]: /app/vendor/bundle/ruby/1.9.1/gems/redis-2.2.2/lib/redis/client.rb:236:in `rescue in establish_connection': Connection refused - Unable to connect to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
Switching from "Redis.current" in /initializer/redis.rb and using a "$redis" variable convention instead (as illustrated here http://jimneath.org/2011/03/24/using-redis-with-ruby-on-rails.html) fixed the connection problem.
The app was using redis gem version 2.2.2 at the time. It appears as though the Redis.current in this gem version won't be consistent across 'web' and 'worker' processes because Heroku runs these processes on separate threads. The documentation on the gem repo suggests that updating to gem to >= version 3 will enable Redis.current to run in multi-threaded environments:
https://github.com/redis/redis-rb/blob/master/CHANGELOG.md#300