gpt4 book ai didi

ruby-on-rails - Ruby on Rails - Puma on_worker_boot 与 redis 连接

转载 作者:IT王子 更新时间:2023-10-29 06:08:35 25 4
gpt4 key购买 nike

我正在尝试使用 Puma 连接 heroku 上的 redis,但我一直遇到错误

Read error: #<Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)>

我搜索了 stackoverflow,但没有一个答案似乎可以解决我的问题。

我的 puma.rb配置

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
ActiveRecord::Base.establish_connection

Redis.current.client.reconnect
$redis = Redis.current
end

我的 redis.rb初始化器

$redis = Redis.new(:url => ENV["REDISCLOUD_URL"])

我检查了ENV["REDISCLOUD_URL"]那是 redis://rediscloud:blank@blank.eu-west-1-1.1.ec2.garantiadata.com:port

我不知道我做错了什么,这对我来说似乎都是正确的

最佳答案

我必须设置配置变量 heroku config:set REDIS_PROVIDER=REDISCLOUD_URL

Puma.rb

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
ActiveRecord::Base.establish_connection

if ENV["REDISCLOUD_URL"]
uri = URI.parse(ENV["REDISCLOUD_URL"])
Redis.current = Redis.new(host: uri.host, port: uri.port, password: uri.password)
$redis = Redis.current
else
Redis.current.quit
end
end

redis.rb

if ENV["REDISCLOUD_URL"]
uri = URI.parse(ENV["REDISCLOUD_URL"])
$redis = Redis.new(host: uri.host, port: uri.port, password: uri.password)
end

关于ruby-on-rails - Ruby on Rails - Puma on_worker_boot 与 redis 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29942245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com