LoginSignup
26
27

More than 5 years have passed since last update.

Redis接続をチェックしてからRailsサーバー起動

Last updated at Posted at 2012-01-13

はじめに

Railsでredisを使うとき,設定に

config/initializers/redis.rb
$redis = Redis.new(:host => 'localhost', :port => 6379)

のように書いて$redis.get 'key'みたいに使うけれど,これだとRedisサーバーに繋がらないとき(redisサーバーが落ちてるとか)でもアプリケーション内でredisへアクセスするまでわからない.
起動時にチェックしてほしい.

手段、コード

初期化したあとすぐにpingを飛ばす

config/initializers/redis.rb
$redis = Redis.new(:host => 'localhost', :port => 6379)
$redis.ping

繋がらない場合は例外を投げて落ちるのでより安心.

参考リンク

Redisのpingコマンド

26
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
27