我正在尝试将我的rails应用程序连接到使用Resque的redis-to-go服务器,但是我收到错误:
Error connecting to Redis on localhost:6379 (ECONNREFUSED)
我尝试了很多可能的解决方案,但其中任何一个都有效。我按照这里的所有说明进行操作,但什么都没有。
我有 2 个初始值设定项:
Redis.rb
ENV["REDISTOGO_URL"] ||= "redis://redistogo:7d3dd2a11d0018445472de9d676360c3@albacore.redistogo.com:9408/"
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
resque.rb
Resque.redis = REDIS
Dir["#{Rails.root}/app/workers/*.rb"].each { |file| require file }
Resque.before_fork do
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end
Resque.after_fork do
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
和耙子文件:
require 'rake/dsl_definition'
require 'resque/tasks'
task "resque:setup" => :environment do
ENV['QUEUE'] ||= '*'
Resque.before_fork do
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end
Resque.after_fork do
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
end
namespace :resque do
desc "let resque workers always load the rails environment"
task :setup => :environment do
ENV['QUEUE'] ||= '*'
Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection }
end
desc "kill all workers (using -QUIT), god will take care of them"
task :stop_workers => :environment do
pids = Array.new
Resque.workers.each do |worker|
pids << worker.to_s.split(/:/).second
end
if pids.size > 0
system("kill -QUIT #{pids.join(' ')}")
end
# god should handle the restart
end
end
desc "Alias for resque:work (To run workers on Heroku)"
task "jobs:work" => "resque:work"
我添加了很多部分的一些代码,所以现在有点丑陋,但在我尝试使用 redis-to-go 之前,我在本地将其与其他配置一起使用并且它可以工作,但现在我想在 heroku 中部署,问题开始了:c。
附言
如果我尝试手动插入/获取 redis,它可以工作!(导轨控制台)
irb(main):001:0> REDIS.set("foo", "bar")
=> "OK"
irb(main):002:0> REDIS.get("foo")
=> "bar"
尝试使用 Redis.new(url: REDIS_URL) 语法而不是 URI.parse
if Rails.env == 'production'
$redis = Redis.new(url: ENV["REDISCLOUD_URL"])
else
$redis = Redis.new
end