7

パスワードで保護されたRedisサーバーに接続するために、Unicornで実行されているRailsアプリを取得しようとすると、予期しない重大な問題が発生します。

コマンドラインで使用bundle exec rails c productionすると、Resque.redisを介してコマンドを発行できます。ただし、Unicornでフォークすると、構成が失われているようです。

パスワードで保護されていないRedisサーバーを使用するだけで機能します。ただし、Redisサーバーが存在する場所以外のサーバーでワーカーを実行するつもりなので、パスワードで保護する必要があります。

また、パスワードで保護された(同じ手法を使用して)が、UnicornではなくPassengerを使用することに成功しました。

私は次の設定をしています:

# config/resque.yml

development: localhost:6379
test: localhost:6379
production: redis://user:PASSWORD@oak.isc.org:6379

# config/initializers/redis.rb

rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
rails_env = ENV['RAILS_ENV'] || 'development'

$resque_config = YAML.load_file(rails_root + '/config/resque.yml')
uri = URI.parse($resque_config[rails_env])
Resque.redis = Redis.new(host: uri.host, port: uri.port, password: uri.password)

# unicorn.rb bootup file

preload_app true

before_fork do |server, worker|
  Redis.current.quit
end

after_fork do |server, worker|
  Redis.current.quit
end

4

5 に答える 5

6

わかりました、この問題をグーグルしているかもしれない他の人々のために、私は少なくとも自分自身のためにこれを解決しました

基本的な問題は、コード内の他の場所、たとえばジオコーダーのセットアップやユニコーンの構成ファイルでRedis.newを呼び出すことです。

initialize Redisを呼び出すたびに、適切な値を渡すようにしてください。

REDIS = Redis.connect(:url =>  ENV['REDISTOGO_URL'])

どこにでもあり、あなたは決して持ってはいけません

Redis.new 

デフォルトはlocalhostとデフォルトのポートになるため

于 2012-09-02T00:33:26.757 に答える
5

更新されたresqueの問題に対する@lmarlowのコメントに基づくまったく異なるアイデア。

Redis〜> 3(サーバーバージョンではなく、rubyクライアントバージョンを意味します)がある場合は、どこでも壊れると思います。

この記事の執筆時点では、ResqueにはRedis〜> 2が必要ですが、gemspecでそれを指定していません。したがって、これをGemfileに追加して支援する必要があります。

gem 'redis', '~>2' # until a new version of resque comes out
gem 'resque'

また、バンドラーがどこでも使用されていることを確認してください。そうしないと、システムに新しいバージョンのRedis gemがある場合、それが使用され、Resqueは以前のように失敗します。

最後に、見た目のメモ...設定を次のように簡略化できます。

# config/initializers/redis.rb
$resque_redis_url = uris_per_environment[rails_env] # note no URI.parse
Resque.redis = $resque_redis_url

その後

# unicorn.rb bootup file
after_fork do |server, worker|
  Resque.redis = $resque_redis_url
end
于 2012-06-06T22:19:25.623 に答える
2

これは私にとって役に立ちました:

ソース:https ://github.com/redis/redis-rb/blob/master/examples/unicorn/unicorn.rb

require "redis"

worker_processes 3

# If you set the connection to Redis *before* forking,
# you will cause forks to share a file descriptor.
#
# This causes a concurrency problem by which one fork
# can read or write to the socket while others are
# performing other operations.
#
# Most likely you'll be getting ProtocolError exceptions
# mentioning a wrong initial byte in the reply.
#
# Thus we need to connect to Redis after forking the
# worker processes.

after_fork do |server, worker|
  Redis.current.quit
end
于 2012-12-05T21:26:18.390 に答える
2

私のために働いたのはここのユニコーン設定でした:https ://stackoverflow.com/a/14636024/18706

before_fork do |server, worker|
  if defined?(Resque)
    Resque.redis.quit
    Rails.logger.info("Disconnected from Redis")
  end
end

after_fork do |server, worker|
  if defined?(Resque)
    Resque.redis = REDIS_WORKER
    Rails.logger.info("Connected to Redis")
  end
end
于 2013-07-19T17:39:42.017 に答える
0

問題はResque-webにあったと思います。その設定ファイル、彼らは今それを修正しました。バージョン0.0.11では、コメントでも言及されています:https ://github.com/resque/resque-web/blob/master/config/initializers/resque_config.rb#L3

以前、彼らのファイルは次のようになりました:https ://github.com/resque/resque-web/blob/v0.0.9/config/initializers/resque_config.rb

また、何らかの理由でアップグレードできない場合は、RAILS_RESQUE_REDIS=<host>:<port>代わりにenv変数を設定してみてください。これは、Initializersがredisへの接続を試みた後にロードしているためです(失敗します)。

于 2017-09-11T06:34:55.237 に答える