-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from alphagov/rabbit-mq-setup
Add basic ability to consume publishing queue
- Loading branch information
Showing
5 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class PublishedDocumentsQueueConsumer | ||
def process(message) | ||
Rails.logger.info(message.payload) | ||
message.ack | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace :message_queue do | ||
desc "Create RabbitMQ queue for development environment" | ||
task create_queue: :environment do | ||
# The exchange, queue, and binding are created via Terraform outside of local development: | ||
# https://github.com/alphagov/govuk-aws/blob/main/terraform/projects/app-publishing-amazonmq/ | ||
raise "This task should only be run in development" unless Rails.env.development? | ||
|
||
bunny = Bunny.new | ||
channel = bunny.start.create_channel | ||
exch = Bunny::Exchange.new(channel, :topic, "published_documents") | ||
channel.queue("search_api_v2_published_documents").bind(exch, routing_key: "*.*") | ||
end | ||
|
||
desc "Listens to and processes messages from the published documents queue" | ||
task consume_published_documents: :environment do | ||
GovukMessageQueueConsumer::Consumer.new( | ||
queue_name: "search_api_v2_published_documents", | ||
processor: PublishedDocumentsQueueConsumer.new, | ||
).run | ||
end | ||
end |
25 changes: 25 additions & 0 deletions
25
spec/queue_consumers/published_documents_queue_consumer_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require "govuk_message_queue_consumer/test_helpers" | ||
|
||
RSpec.describe PublishedDocumentsQueueConsumer do | ||
subject(:consumer) { described_class.new } | ||
|
||
it_behaves_like "a message queue processor" | ||
|
||
describe "when receiving an incoming message" do | ||
let(:message) { GovukMessageQueueConsumer::MockMessage.new(payload) } | ||
let(:payload) { { "hello" => "world" } } | ||
|
||
before do | ||
allow(Rails.logger).to receive(:info) | ||
consumer.process(message) | ||
end | ||
|
||
it "acks incoming messages" do | ||
expect(message).to be_acked | ||
end | ||
|
||
it "logs the payload of incoming messages" do | ||
expect(Rails.logger).to have_received(:info).with(payload) | ||
end | ||
end | ||
end |