Sidekiqのみでinitializersを読み込む(SSL接続)
問題
initializers/sidekiq.rbを作成してRedisへSSL接続しようとしてもエラーになる。
起動時のログから謎にURLだけは反映されていたので余計に混乱していた。
require 'sidekiq'
require 'dotenv/load'
require 'openssl'
OpenSSL.debug = true
redis_url = ENV['REDIS_URL'] || 'rediss://localhost:6379/0'
redis_crt_path = ENV['REDIS_CRT_PATH']
Sidekiq.configure_server do |config|
config.redis = {
url: redis_url,
ssl: true,
ssl_params: {
verify_mode: OpenSSL::SSL::VERIFY_NONE,
ca_file: redis_crt_path
}
}
end
Sidekiq.configure_client do |config|
config.redis = {
url: redis_url,
ssl: true,
ssl_params: {
verify_mode: OpenSSL::SSL::VERIFY_NONE,
ca_file: redis_crt_path
}
}
end
Sidekiqを起動してもエラーでRedisに接続できない。
2024-01-05T03:24:30.064Z pid=21495 tid=iu7 INFO: Sidekiq 7.2.0 connecting to Redis with options {:size=>10, :pool_name=>"internal", :url=>"rediss://demo.com:6379/5"}
SSL_connect returned=1 errno=0 peeraddr=3.81.124.35:6379 state=error: certificate verify failed (self signed certificate)
demo-project/vendor/bundle/ruby/3.2.0/gems/redis-client-0.18.0/lib/redis_client/ruby_connection.rb:134:in `connect_nonblock'
demo-project/vendor/bundle/ruby/3.2.0/gems/redis-client-0.18.0/lib/redis_client/ruby_connection.rb:134:in `block in connect'
demo-project/vendor/bundle/ruby/3.2.0/gems/redis-client-0.18.0/lib/redis_client/ruby_connection.rb:133:in `loop'
demo-project/vendor/bundle/ruby/3.2.0/gems/redis-client-0.18.0/lib/redis_client/ruby_connection.rb:133:in `connect'
demo-project/vendor/bundle/ruby/3.2.0/gems/redis-client-0.18.0/lib/redis_client/ruby_connection.rb:49:in `initialize'
demo-project/vendor/bundle/ruby/3.2.0/gems/redis-client-0.18.0/lib/redis_client.rb:720:in `new'
demo-project/vendor/bundle/ruby/3.2.0/gems/redis-client-0.18.0/lib/redis_client.rb:720:in `block in connect'
demo-project/vendor/bundle/ruby/3.2.0/gems/redis-client-0.18.0/lib/redis_client/middlewares.rb:12:in `connect'
demo-project/vendor/bundle/ruby/3.2.0/gems/redis-client-0.18.0/lib/redis_client.rb:719:in `connect'
demo-project/vendor/bundle/ruby/3.2.0/gems/redis-client-0.18.0/lib/redis_client.rb:706:in `raw_connection'
demo-project/vendor/bundle/ruby/3.2.0/gems/redis-client-0.18.0/lib/redis_client.rb:673:in `ensure_connected'
demo-project/vendor/bundle/ruby/3.2.0/gems/redis-client-0.18.0/lib/redis_client.rb:254:in `call'
demo-project/vendor/bundle/ruby/3.2.0/gems/redis-client-0.18.0/lib/redis_client/decorator.rb:26:in `call'
demo-project/vendor/bundle/ruby/3.2.0/gems/sidekiq-7.2.0/lib/sidekiq/config.rb:144:in `block in redis_info'
demo-project/vendor/bundle/ruby/3.2.0/gems/sidekiq-7.2.0/lib/sidekiq/config.rb:163:in `block in redis'
demo-project/vendor/bundle/ruby/3.2.0/gems/connection_pool-2.4.1/lib/connection_pool.rb:110:in `block (2 levels) in with'
demo-project/vendor/bundle/ruby/3.2.0/gems/connection_pool-2.4.1/lib/connection_pool.rb:109:in `handle_interrupt'
demo-project/vendor/bundle/ruby/3.2.0/gems/connection_pool-2.4.1/lib/connection_pool.rb:109:in `block in with'
demo-project/vendor/bundle/ruby/3.2.0/gems/connection_pool-2.4.1/lib/connection_pool.rb:106:in `handle_interrupt'
demo-project/vendor/bundle/ruby/3.2.0/gems/connection_pool-2.4.1/lib/connection_pool.rb:106:in `with'
demo-project/vendor/bundle/ruby/3.2.0/gems/sidekiq-7.2.0/lib/sidekiq/config.rb:160:in `redis'
demo-project/vendor/bundle/ruby/3.2.0/gems/sidekiq-7.2.0/lib/sidekiq/config.rb:143:in `redis_info'
demo-project/vendor/bundle/ruby/3.2.0/gems/sidekiq-7.2.0/lib/sidekiq/cli.rb:75:in `run'
demo-project/vendor/bundle/ruby/3.2.0/gems/sidekiq-7.2.0/bin/sidekiq:31:in `<top (required)>'
demo-project/vendor/bundle/ruby/3.2.0/bin/sidekiq:25:in `load'
demo-project/vendor/bundle/ruby/3.2.0/bin/sidekiq:25:in `<top (required)>'
demo-project/vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/cli/exec.rb:58:in `load'
demo-project/vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/cli/exec.rb:58:in `kernel_load'
demo-project/vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/cli/exec.rb:23:in `run'
demo-project/vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/cli.rb:492:in `exec'
demo-project/vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
demo-project/vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
demo-project/vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
demo-project/vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/cli.rb:34:in `dispatch'
demo-project/vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
demo-project/vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/cli.rb:28:in `start'
demo-project/vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/exe/bundle:37:in `block in <top (required)>'
demo-project/vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
demo-project/vendor/bundle/ruby/3.2.0/gems/bundler-2.4.21/exe/bundle:29:in `<top (required)>'
home/.anyenv/envs/rbenv/versions/3.2.2/bin/bundle:25:in `load'
home/.anyenv/envs/rbenv/versions/3.2.2/bin/bundle:25:in `<main>'
socket = OpenSSL::SSL::SSLSocket.new(socket, @config.ssl_context)に渡している@configの内容を見ても設定が反映されていない。
@ssl_params=nil
@verify_mode=1
"#<RedisClient::Config:0x0000000105a36438 @username=nil, @password=nil, @db=5, @id=nil, @ssl=true, @ssl_params=nil, @connect_timeout=1.0, @read_timeout=1.0, @write_timeout=1.0, @driver=RedisClient::RubyConnection, @custom={}, @client_implementation=RedisClient, @protocol=3, @command_builder=RedisClient::CommandBuilder, @inherit_socket=false, @reconnect_attempts=[0], @connection_prelude=[[\"HELLO\", \"3\"], [\"SELECT\", \"5\"]], @circuit_breaker=nil, @middlewares_stack=RedisClient::Middlewares, @host=\"demo.com\", @port=6379, @path=nil, @ssl_context=#<OpenSSL::SSL::SSLContext:0x0000000105a31be0 @verify_mode=1, @verify_hostname=true, @max_proto_version=nil, @min_proto_version=769, @cert_store=#<OpenSSL::X509::Store:0x0000000100919cd0 @verify_callback=nil, @error=nil, @error_string=nil, @chain=nil, @time=nil>>>"
対処方法
GitHubのISSUEを参考に default_configuration を使うが反映されない。
require 'dotenv/load'
require 'sidekiq'
require 'openssl'
redis_url = ENV['REDIS_URL'] || 'rediss://localhost:6379/0'
redis_crt_path = ENV['REDIS_CRT_PATH']
redis_config = {
url: redis_url,
ssl_params: {
verify_mode: OpenSSL::SSL::VERIFY_NONE,
ca_file: redis_crt_path
}
}
Sidekiq.default_configuration.redis = redis_config
ただ、そもそもRails環境ではなかったためinitializersが読み込まれていなかったことが原因だった様子・・・
ワーカーで以下のように読み込むことで反映され、SSLでRedisに接続出来た。
require_relative '../config/initializers/sidekiq.rb'