Rails + Delayed jobs: SSH connections pool

Viewed 763

Current situation: I have a rails app with N delayed job workers. Whenever I want to send an SSH request to some machine, I create a task for the worker. Worker performs something like:

Net:SSH.start(hostname, username, :password => pass) do |ssh|
  ssh.exec!(command)
end

Sometimes I create e.g. 50 such tasks to be performed one by one, or within 5-10 minutes from each other. Each task in this case opens separate connection, which is not effective and sometimes is blocked by target servers due to too many connections.

What I want: Have opened connections stored somewhere and reused by each worker. So that each worker obtains connection somehow and then just run

ssh.exec!(command)

What I have tried:

  • storing connections in file/database/cache looks to be not possible as they are not serializable
  • have tried using singleton class with global variables instantiated under initializers. However class object is different for each worker (later have found that global variables can not be an option).

Are there way to solve that? Any other ideas? Thanks!!

1 Answers
Related