Skip to content

Commit

Permalink
Add a connection to the pool if there is only one in the EmbeddedAnsi…
Browse files Browse the repository at this point in the history
…ble worker

Before ManageIQ#6786 we used to
have one connection specified in the connection pool in database.yml

Installations which have been upgraded from before this change
still have that one connection pool in place.

The EmbeddedAnsible worker uses a thread rather than a new process
so it shares the connection pool with the server. When the pool
is set to only contain one connection, the EmbeddedAnsible worker
will not be able to start.

This commit adds a connection to the pool in the same way that we do
for workers which specify a specific connection pool size in their settings:
https://github.com/ManageIQ/manageiq/blob/f6f7120749d16fd7825f83001dfd875cdecb903c/app/models/miq_worker/runner.rb#L71-L78

https://bugzilla.redhat.com/show_bug.cgi?id=1484150
  • Loading branch information
carbonin committed Nov 14, 2017
1 parent f9f5aef commit a3f50f1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/models/embedded_ansible_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def start_runner
end

def start_monitor_thread
fix_connection_pool

t = Thread.new do
begin
self.class::Runner.start_worker(worker_options)
Expand Down Expand Up @@ -58,4 +60,13 @@ def status_update

# Base class methods we override since we don't have a separate process. We might want to make these opt-in features in the base class that this subclass can choose to opt-out.
def release_db_connection; end

private

def fix_connection_pool
# If we only have one connection in the pool, it will be being used by the server
# Add another so we can start the worker thread
current = ActiveRecord::Base.connection_pool.instance_variable_get(:@size)
ActiveRecord::Base.connection_pool.instance_variable_set(:@size, 2) if current == 1
end
end
15 changes: 14 additions & 1 deletion spec/models/embedded_ansible_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,26 @@
end

describe "#start_monitor_thread" do
it "sets worker class and id in thread object" do
let(:pool) { double("ConnectionPool") }

before do
allow(Thread).to receive(:new).and_return({})
allow(described_class::Runner).to receive(:start_worker)
end

it "sets worker class and id in thread object" do
thread = subject.start_monitor_thread
expect(thread[:worker_class]).to eq subject.class.name
expect(thread[:worker_id]).to eq subject.id
end

it "adds a connection to the pool if there is only one" do
allow(ActiveRecord::Base).to receive(:connection_pool).and_return(pool)

expect(pool).to receive(:instance_variable_get).with(:@size).and_return(1)
expect(pool).to receive(:instance_variable_set).with(:@size, 2)
subject.start_monitor_thread
end
end

describe "#find_worker_thread_object" do
Expand Down

0 comments on commit a3f50f1

Please sign in to comment.