Skip to content

Commit

Permalink
add okcomputer check for expected RabbitMQ queues
Browse files Browse the repository at this point in the history
suggested in #2291
  • Loading branch information
jmartin-sul committed Jan 11, 2024
1 parent cd5d2ea commit d177945
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
31 changes: 31 additions & 0 deletions config/initializers/okcomputer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,37 @@ def check
OkComputer::Registry.register "feature-#{name}-sdr2objects", DirectoryExistsCheck.new(sdrobjects_location, Settings.minimum_subfolder_count)
end

# Check that RabbitMQ queues exist
class RabbitQueueExistsCheck < OkComputer::Check
attr_reader :queue_names, :conn

def initialize(queue_names)
@queue_names = Array(queue_names)
@conn = Bunny.new(hostname: Settings.rabbitmq.hostname,
vhost: Settings.rabbitmq.vhost,
username: Settings.rabbitmq.username,
password: Settings.rabbitmq.password)
super()
end

def check
conn.start
status = conn.status
missing_queue_names = queue_names.reject { |queue_name| conn.queue_exists?(queue_name) }
if missing_queue_names.empty?
mark_message "'#{queue_names.join(', ')}' exists, connection status: #{status}"
else
mark_message "'#{missing_queue_names.join(', ')}' does not exist"
mark_failure
end
conn.close
rescue StandardError => e
mark_message "Error: '#{e}'"
mark_failure
end
end
OkComputer::Registry.register 'rabbit-queues', RabbitQueueExistsCheck.new('dsa.create-event')

OkComputer::Registry.register 'ruby_version', OkComputer::RubyVersionCheck.new

# ------------------------------------------------------------------------------
Expand Down
31 changes: 31 additions & 0 deletions spec/config/okcomputer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,35 @@
expect(described_class.new('i-do-not-exist')).not_to be_successful
end
end

describe RabbitQueueExistsCheck do
let(:bunny_conn) { instance_double(Bunny::Session) }
let(:queue_name1) { 'foo.first_queue' }
let(:queue_name2) { 'foo.second_queue' }

before do
allow(bunny_conn).to receive_messages(start: bunny_conn, status: :open, close: true)
allow(bunny_conn).to receive(:queue_exists?).with(queue_name1).and_return(queue_exists1)
allow(bunny_conn).to receive(:queue_exists?).with(queue_name2).and_return(queue_exists2)
allow(Bunny).to receive(:new).and_return(bunny_conn)
end

context 'when the expected queues are present' do
let(:queue_exists1) { true }
let(:queue_exists2) { true }

it 'succeeds if the expected queue exists' do
expect(described_class.new([queue_name1, queue_name2])).to be_successful
end
end

context 'when a queue is missing' do
let(:queue_exists1) { true }
let(:queue_exists2) { false }

it 'succeeds if the expected queue exists' do
expect(described_class.new([queue_name1, queue_name2])).not_to be_successful
end
end
end
end

0 comments on commit d177945

Please sign in to comment.