Resque worker failing with PostgreSQL server
-
26-10-2019 - |
Pergunta
I've managed to set up my workers and they used to execute without issue (in development) but now they don't in either production or development (I'm guessing after changing from SQlite3 to PostgreSQL).
When I run a rake command to run the workers with rake resque:work QUEUE=*
I get the following error and stack trace:
getaddrinfo: nodename nor servname provided, or not known
I get the following errors when running heroku rake resque:work QUEUE=*
in the console to test pending workers in the queue.
Class SentimentJob
Arguments [4, 5, 6]
Exception ActiveRecord::StatementInvalid
Error PGError: server closed the connection unexpectedly This probably means
the server terminated abnormally before or while processing the request.
: SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc,
a.attnotnull FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid =
d.adrelid AND a.attnum = d.adnum WHERE a.attrelid = '"taggings"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum
And for my Facebook worker:
Class FBConnectionsJob
Arguments 1
Exception OpenSSL::SSL::SSLError
Error SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B:
certificate verify failed
Class FBConnectionsJob
Arguments 1
Exception ActiveRecord::StatementInvalid
Error PGError: server closed the connection unexpectedly This probably means
the server terminated abnormally before or while processing the request.
: SELECT tablename FROM pg_tables WHERE schemaname = ANY
(current_schemas(false))
Why do I get different errors in different environments? My initializer files look like:
Should I add ENV specifications here?
Resque.rb
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Dir["#{Rails.root}/app/jobs/*.rb"].each { |file| require file }
Redis.rb
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Resque.redis = REDIS
My environments/production.rb file has no reference to Redis and my environments/development.rb file has this for Redis setup:
ENV["REDISTOGO_URL"] = 'redis://username:password@my.host:6789'
Solução
Adding the following to my Rakefile fixed a similar problem for me:
task "resque:setup" => :environment do
Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection }
end
This reconnects PostgreSQL before Resque forks its process to create a worker.
Outras dicas
It looks like your app can not connect to Redis server. Did you provide valid connection details for production environment? Is you Redis server available? Isn't it behind firewall are some private network?
I think it might be same issue with you production PostgreSQL server.
I've changed the initializers to the following and it now works on localhost, so @mirtinciu was right about the server connection. Still need to figure out what is wrong on the production server.
if Rails.env.development?
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => 'localhost', :port => '6379')
else
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
I think it's a duplicate and it is solved by restoring the ActiveRecord connection: Rails Resque workers fail with PGError: server closed the connection unexpectedly