Rails + Puma + sidekiq + rabbitMQ (Bunny Gem), Получение проблемы с соединением, когда мы запускаем его на Heroku - PullRequest
0 голосов
/ 26 сентября 2018

Я делаю прототип с Rails + Puma + sidekiq + rabbitMQ (самоцвет кролика)

Ruby version: 2.5.0
Sidekiq: 5.2.1

инициализаторы / sidekiq.rb

require 'sidekiq'

Sidekiq.configure_client do |config|
  config.redis = { :size => 1 }
end

Sidekiq.configure_server do |config|

end

puma.rb

threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads threads_count, threads_count

port ENV.fetch("PORT") { 3000 }

environment ENV.fetch("RAILS_ENV") { "development" }

workers ENV.fetch("WEB_CONCURRENCY") { 2 }

plugin :tmp_restart

Procfile.rb

web: bundle exec puma -C config/puma.rb
worker: bundle exec sidekiq -c 2 -v

Это потребительская часть для получения сообщения из очереди

initializers / subscriber.rb

connection = Bunny.new(ENV['CLOUDAMQP_URL'])

connection.start # start a communication session with the amqp server

channel = connection.channel()

queue = channel.queue('order-queue', durable: true)

puts ' [*] Waiting for messages. To exit press CTRL+C'

queue.subscribe(manual_ack: true, block: true) do |delivery_info, 
properties, payload|
  puts " [x] Received #{payload}"
  puts " [x] Done"

  channel.ack(delivery_info.delivery_tag, false)
  # Call sidekiq worker to do task
  callSidekiqWorker.perform_async(payload)
end

Здесь, вcallSidekiqWorker У меня есть одна функция для получения соединения из другой базы данных

sidekiqWorker.rb

def perform(params)
        # Get Database Connection
        connection = get_db_connection
        params.each do |param|
            query1 = "..."
            connection.execute(query1)
            query2 = "..."
            connection.execute(query2)
            ............
        end
        ActiveRecord::Base.remove_connection(connection)
end

def get_db_connection
    ActiveRecord::Base.establish_connection(
        :adapter => 'postgresql',
        :database => ENV["DB_NAME"],
        :username => ENV["USERNAME"],
        :password => ENV["PASSWORD"],
        :host => ENV["HOST"]
    ).connection
end

, поэтому, когда я запускаю это в heroku, я получаю эту ошибку

ID-jk9fg DEBUG: enqueued retry: {"class":"OrderWorker","args":["{"order_id":748,"state_id":215,"make_id":136}"],"retry":true,"queue":"default","jid":"2a66e52a0280994c7d2d4df1","created_at":1537881287.7355168,"enqueued_at":1537881287.7355669,"error_message":"**ActiveRecord::ConnectionNotEstablished**","error_class":"ActiveRecord::ConnectionNotEstablished","failed_at":1537881289.2520792,"retry_count":0}

2018-09-26T04:53:27.409Z 61465 TID-oupmwm6gd WARN: ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
2018-09-26T04:53:27.411Z 61465 TID-oupmwm6gd WARN: /Users/david/.rvm/gems/ruby-2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_handling.rb:112:in connection_pool' /Users/david/.rvm/gems/ruby-2.5.0/gems/activerecord-5.1.6/lib/active_record/query_cache.rb:27:inrun'
/Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/execution_wrapper.rb:25:in before' /Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:413:inblock in make_lambda'
/Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:197:in block (2 levels) in halting' /Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:601:inblock (2 levels) in default_terminator'
/Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:600:in catch' /Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:600:inblock in default_terminator'
/Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:198:in block in halting' /Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:507:inblock in invoke_before'
/Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:507:in each' /Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:507:ininvoke_before'
/Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/callbacks.rb:130:in run_callbacks' /Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/execution_wrapper.rb:108:inrun!'
/Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/execution_wrapper.rb:70:in block in run!' /Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/execution_wrapper.rb:67:intap'
/Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/execution_wrapper.rb:67:in run!' /Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/execution_wrapper.rb:83:inwrap'
/Users/david/.rvm/gems/ruby-2.5.0/gems/activesupport-5.1.6/lib/active_support/reloader.rb:67:in wrap' /Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/rails.rb:41:incall'
/Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:132:in block (4 levels) in dispatch' /Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:217:instats'
/Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:127:in block (3 levels) in dispatch' /Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/job_logger.rb:8:incall'
/Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:126:in block (2 levels) in dispatch' /Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/job_retry.rb:73:inglobal'
/Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:125:in block in dispatch' /Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/logging.rb:48:inwith_context'
/Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/logging.rb:42:in with_job_hash_context' /Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:124:indispatch'
/Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:165:in process' /Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:83:inprocess_one'
/Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/processor.rb:71:in run' /Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/util.rb:16:inwatchdog'
/Users/david/.rvm/gems/ruby-2.5.0/gems/sidekiq-5.2.1/lib/sidekiq/util.rb:25:in `block in safe_thread

но при повторной попытке это будет работать.Я делаю эту правильную конфигурацию? Есть ли проблема с пулом соединений?

...