Rails Resque workers fail with mysql "Too many connections"

We recently switched our (ruby) job queuing system from DelayedJob to Resque.

While our latency has decreased and we have eliminated the bottleneck in the database, we now see a new problem; one or more of our employees seems to leave the database connection open when it leaves. When we look at the list of processes, there are hundreds of connections in a state of "sleep". They eventually lose time after 90 seconds. We forced our employees to support the work of clients, but we really need to find that one (or more) of our work is not polite when it is disabled using the ruby ​​mysql2 client.

Any ideas on how we could (1) find the culprits or (2) measure our code so that we can make sure that we are actually disconnected before the task is completed?

  • Rails 4.0.x
  • Resque 1.25.2
  • mysql2 gem 0.3.16
+4
source share
2 answers

Make sure the Resque process disconnects from the database before forking and then re-establishing the connection. Create an initialization file config/initializers/resque.rb  that contains:

Resque.before_fork do
  defined?(ActiveRecord::Base) && ActiveRecord::Base.connection.disconnect!
end

Resque.after_fork do
  defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection
end
+1
source

I gave up on Resque and moved to Sidekiq. Now I am much happier.

0
source

Source: https://habr.com/ru/post/1548695/


All Articles